X-Git-Url: http://nitlanguage.org diff --git a/lib/for_abuse.nit b/lib/for_abuse.nit index 1e06ec3..eeb8ed7 100644 --- a/lib/for_abuse.nit +++ b/lib/for_abuse.nit @@ -25,13 +25,15 @@ module for_abuse # 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. +# 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] - super Collection[E] + # Starts and control the service + fun iterator: Iterator[E] is abstract end # Abuser to read a file, see `file_open` @@ -44,9 +46,10 @@ end # Abuser iterator to read a file, see `file_open` private class ReadFileForAbuserIterator super Iterator[IFStream] - redef var item: IFStream + var path: String + redef var item: IFStream is noinit redef var is_ok = true - init(path: String) + init do # start of service is to open the file, and return in item = new IFStream.open(path) @@ -77,7 +80,7 @@ class CompareQuery[E] # The second element to compare var b: E # The result of the comparison (according to the user) - var res writable = 0 + var res = 0 is writable end # Abuser for sorting array, see `sort_fa` @@ -92,21 +95,18 @@ end private class SortAbuserIterator[E] super Iterator[CompareQuery[E]] # The index of the big loop - var i: Int + var i: Int = 0 # The index of the small loop - var j: Int + var j: Int = 0 # The array to sort var array: Array[E] # The query used to communicate with the user. # For ecological concerns, a unique CompareQuery is instatiated. - var query: nullable CompareQuery[E] + var query: nullable CompareQuery[E] = null redef fun item do return query.as(not null) - init(array: Array[E]) + init do - self.array = array # Initialize the algorithm, see `next` for the rest - i = 0 - j = 0 if not is_ok then return query = new CompareQuery[E](array[i], array[j]) end @@ -139,8 +139,8 @@ redef class Array[E] # The user uses the provided query (item) to implements its own comparison # # var a = [1, 3, 2] - # for q in a do q.res = q.a <=> q.b - # print a # => 123 + # for q in a.sort_fa do q.res = q.a <=> q.b + # assert a == [1, 2, 3] # # Implements a sort by permutation. fun sort_fa: ForAbuser[CompareQuery[E]] @@ -154,8 +154,10 @@ end # Open and read a file trough a `for` abuse. # The abuse just ensures that the file is closed after the reading. # -# for f in file_open(path) do -# print path.read_line +# for f in file_open("/etc/issue") do +# var l = f.read_line +# print l +# assert not l.is_empty # end # f is automatically closed here fun file_open(path: String): ForAbuser[IFStream] do