gamnit: make `SpriteSet` public so clients can use its services
[nit.git] / lib / for_abuse.nit
index ab469a6..f19bd0f 100644 (file)
@@ -8,7 +8,7 @@
 # You  are  allowed  to  redistribute it and sell it, alone or is a part of
 # another product.
 
-# Service management trough the `for` control structure.
+# Service management through the `for` control structure.
 #
 # The module is a proof-of-concept to investigate the abuse of
 # the `for` structure to implement various services.
@@ -34,35 +34,25 @@ module for_abuse
 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]
+       super ForAbuser[FileReader]
        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
+       super Iterator[FileReader]
+       var path: String
+       redef var item: FileReader 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)
+               item = new FileReader.open(path)
        end
        redef fun next
        do
@@ -90,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`
@@ -105,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
@@ -148,12 +135,12 @@ private class SortAbuserIterator[E]
 end
 
 redef class Array[E]
-       # Sort an array trough a `for` abuse.
+       # Sort an array through a `for` abuse.
        # 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
-       #     assert 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]]
@@ -164,13 +151,15 @@ end
 
 ####
 
-# Open and read a file trough a `for` abuse.
+# Open and read a file through a `for` abuse.
 # The abuse just ensures that the file is closed after the reading.
 #
 #     for f in file_open("/etc/issue") do
-#       print f.read_line
+#         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]
+fun file_open(path: String): ForAbuser[FileReader]
 do
        return new ReadFileForAbuser(path)
 end