<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# This file is free software, which comes along with NIT.  This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
# is kept unaltered, and a notification of the changes is added.
# You  are  allowed  to  redistribute it and sell it, alone or is a part of
# another product.

# Service management trough the `for` control structure.
#
# The module is a proof-of-concept to investigate the abuse of
# the `for` structure to implement various services.
#
# The idea is that, with a `for`, the service-provider can:
# * control the end of the service (thus can finalize things
#   like releasing ressources)
# * communicate data with the user on each iteration; the used can
#   also communicate results to the provider.
module for_abuse

# Encapsulation of service in a `for`-compatible interface.
#
# The service is not effectively started until the iterate method
# is called. Then, each step of the iteration is a step in the service.
#
# While, for a typing point of view, abusers are just collections,
# the point of this class is to tag services that return a ForAbuser
# object.
# Note that using abuser as genuine collection should work but is not
# recommended since it may cause mental health issues.
interface ForAbuser[E]
	super Collection[E]
end

# Abuser to read a file, see `file_open`
private class ReadFileForAbuser
	super ForAbuser[IFStream]
	var path: String
	redef fun iterator do return new ReadFileForAbuserIterator(path)
end

# Abuser iterator to read a file, see `file_open`
private class ReadFileForAbuserIterator
	super Iterator[IFStream]
	redef var item: IFStream
	redef var is_ok = true
	init(path: String)
	do
		# start of service is to open the file, and return in
		item = new IFStream.open(path)
	end
	redef fun next
	do
		# end of service is to close the file
		# there is only one iteration
		is_ok = false
		item.close
	end
end

####<commit_msg>lib: detach ForAbuser from Collection<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# This file is free software, which comes along with NIT.  This software is
# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without  even  the implied warranty of  MERCHANTABILITY or  FITNESS FOR A 
# PARTICULAR PURPOSE.  You can modify it is you want,  provided this header
# is kept unaltered, and a notification of the changes is added.
# You  are  allowed  to  redistribute it and sell it, alone or is a part of
# another product.

# Service management trough the `for` control structure.
#
# The module is a proof-of-concept to investigate the abuse of
# the `for` structure to implement various services.
#
# The idea is that, with a `for`, the service-provider can:
# * control the end of the service (thus can finalize things
#   like releasing ressources)
# * communicate data with the user on each iteration; the used can
#   also communicate results to the provider.
module for_abuse

# Encapsulation of service in a `for`-compatible interface.
#
# The service is not effectively started until the iterate method
# is called. Then, each step of the iteration is a step in the service.
#
# While, for a typing point of view, abusers are just classes with an
# iterator method, the point of this class is to tag services that return
# a ForAbuser object.
#
# Note that using having `ForAbuser` as a genuine subclass of `Collection`
# works but is not recommended since it may cause mental health issues.
interface ForAbuser[E]
	# Starts and control the service
	fun iterator: Iterator[E] is abstract

	# Starts and contol the service (for `nitc`)
	fun iterate
		!each(e: E)
	do
		var i = iterator
		while i.is_ok do
			each(i.item)
			i.next
		end
	end
end

# Abuser to read a file, see `file_open`
private class ReadFileForAbuser
	super ForAbuser[IFStream]
	var path: String
	redef fun iterator do return new ReadFileForAbuserIterator(path)
end

# Abuser iterator to read a file, see `file_open`
private class ReadFileForAbuserIterator
	super Iterator[IFStream]
	redef var item: IFStream
	redef var is_ok = true
	init(path: String)
	do
		# start of service is to open the file, and return in
		item = new IFStream.open(path)
	end
	redef fun next
	do
		# end of service is to close the file
		# there is only one iteration
		is_ok = false
		item.close
	end
end

####
{'fraction_non_alphanumeric': 0.05766279324709494, 'fraction_numerical': 0.0, 'mean_word_length': 3.9640914036996735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6048936', 'n_tokens_mistral': 1328, 'n_tokens_neox': 1305, 'n_words': 673}