Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 270 lines (228 sloc) 5.385 kB
29469b0 @vic Updated all .fy files to open class with 'class Some {}' Closes #26
vic authored
1 class FancyEnumerable {
81f8041 @bakkdoor Added docstrings & comments to more lib/ files.
authored
2 """
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
3 Mixin-Class with useful methods for collections that implement an @each:@ method.
81f8041 @bakkdoor Added docstrings & comments to more lib/ files.
authored
4 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
5
3eab421 @bakkdoor Renamed include?: to includes?: since it's much nicer to read and gra…
authored
6 def includes?: item {
0578628 @bakkdoor optimized some docstrings for lib/enumerable.fy
authored
7 """
8 @item Item to check if it's included in @self.
9 @return @true, if @item in @self, otherwise @false.
10
11 Indicates, if a collection includes a given element.
12 """
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
13 any?: |x| { item == x }
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
14 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
15
b77a481 @bakkdoor Moved Array#each:in_between: to FancyEnumerable class, as it can be u…
authored
16 def each: each_block in_between: between_block {
17 """
18 Similar to @each:@ but calls an additional @Block@ between
19 calling the first @Block@ for each element in self.
20 """
21
22 count = 0
23 size = self size
24 each: |x| {
25 each_block call: [x]
26 unless: (count == (size - 1)) do: {
27 between_block call
28 }
29 count = count + 1
30 }
31 }
32
ccff427 @bakkdoor fixed bugs in Enumerable#all?: and Enumerable#any?: - they now take b…
authored
33 def any?: condition {
0578628 @bakkdoor optimized some docstrings for lib/enumerable.fy
authored
34 """
35 @condition @Block@ (or @Callable) that is used to check if any element in @self yields true for it.
36 @return @true, if @condition yields @true for any element, @false otherwise.
37
38 Indicates, if any element meets the condition.
39 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
40
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
41 each: |x| {
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
42 if: (condition call: [x]) then: {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
43 return true
47ba5c2 @bakkdoor added . & $ operators. operator-methods are by default binary (take 1…
authored
44 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
45 }
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
46 nil
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
47 }
9cd9cdf @bakkdoor Added some stdlib code
authored
48
ccff427 @bakkdoor fixed bugs in Enumerable#all?: and Enumerable#any?: - they now take b…
authored
49 def all?: condition {
0578628 @bakkdoor optimized some docstrings for lib/enumerable.fy
authored
50 """
51 Similar to @FancyEnumerable#any?:@ just checking for all elements.
52 Indicates, if all elements meet the condition.
53 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
54
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
55 each: |x| {
b0a5814 @bakkdoor Use unless:do: instead of if_false: in lib/
authored
56 unless: (condition call: [x]) do: {
1f90930 @bakkdoor More work on getting tests to work. Commented out non-working tests f…
authored
57 return false
47ba5c2 @bakkdoor added . & $ operators. operator-methods are by default binary (take 1…
authored
58 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
59 }
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
60 true
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
61 }
9cd9cdf @bakkdoor Added some stdlib code
authored
62
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
63 def find: item {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
64 "Returns @nil, if the given object isn't found, or the object, if it is found."
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
65
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
66 if: (item is_a?: Block) then: {
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
67 find_by: item
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
68 } else: {
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
69 each: |x| {
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
70 if: (item == x) then: {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
71 return x
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
72 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
73 }
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
74 nil
9cd9cdf @bakkdoor Added some stdlib code
authored
75 }
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
76 }
9cd9cdf @bakkdoor Added some stdlib code
authored
77
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
78 def find_by: block {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
79 """
80 Similar to @find:@ but takes a block that is called for each element to find it.
81 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
82
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
83 each: |x| {
49b3eb6 @bakkdoor Always use Array as argument for Block#call:, especially within Array…
authored
84 block call: [x] . if_do: |item| {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
85 return item
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
86 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
87 }
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
88 nil
2f1b175 @bakkdoor added Enumerable#compact, Array#remove_at:, Array#compact! and Array#…
authored
89 }
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
90
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
91 def map: block {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
92 "Returns a new @Array@ with the results of calling a given block for every element"
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
93
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
94 coll = []
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
95 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
96 coll << (block call: [x])
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
97 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
98 coll
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
99 }
100
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
101 def select: condition {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
102 "Returns a new @Array@ with all elements that meet the given condition block."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
103
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
104 coll = []
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
105 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
106 { coll << x } if: $ condition call: [x]
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
107 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
108 coll
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
109 }
110
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
111 def reject: condition {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
112 "Returns a new @Array@ with all elements that don't meet the given condition block."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
113
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
114 coll = []
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
115 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
116 { coll << x } unless: $ condition call: [x]
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
117 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
118 coll
2f1b175 @bakkdoor added Enumerable#compact, Array#remove_at:, Array#compact! and Array#…
authored
119 }
7842ca3 @bakkdoor removed trailing whitespace from lib/ files
authored
120
9cd9cdf @bakkdoor Added some stdlib code
authored
121 def take_while: condition {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
122 "Returns a new @Array@ by taking elements from the beginning as long as they meet the given condition block."
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
123 coll = []
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
124 each: |x| {
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
125 if: (condition call: [x]) then: {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
126 coll << x
127 } else: {
128 return coll
9cd9cdf @bakkdoor Added some stdlib code
authored
129 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
130 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
131 coll
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
132 }
9cd9cdf @bakkdoor Added some stdlib code
authored
133
134 def drop_while: condition {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
135 "Returns a new @Array@ by skipping elements from the beginning as long as they meet the given condition block."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
136
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
137 coll = []
138 drop = nil
139 first_check = true
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
140 each: |x| {
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
141 if: (drop or: first_check) then: {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
142 drop = condition call: [x]
ccff427 @bakkdoor fixed bugs in Enumerable#all?: and Enumerable#any?: - they now take b…
authored
143 first_check = nil
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
144 # check, if we actually have to insert his one:
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
145 unless: drop do: {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
146 coll << x
147 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
148 } else: {
149 coll << x
f2a436f @bakkdoor small improvements to lib/Enumerable.fnc
authored
150 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
151 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
152 coll
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
153 }
9cd9cdf @bakkdoor Added some stdlib code
authored
154
6cd9d3c @bakkdoor Added FancyEnumerable#drop: & FancyEnumerable#take:
authored
155 def take: amount {
156 i = 0
157 self take_while: {
158 i = i + 1
159 i <= amount
160 }
161 }
162
163 def drop: amount {
164 i = 0
165 self drop_while: {
166 i = i + 1
167 i <= amount
168 }
169 }
170
36404d9 @bakkdoor Renamed Enumerable#reduce:with: to Enumerable#reduce:init_val:
authored
171 def reduce: block init_val: init_val {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
172 "Calculates a value based on a given block to be called on an accumulator value and an initial value."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
173
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
174 acc = init_val
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
175 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
176 acc = (block call: [acc, x])
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
177 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
178 acc
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
179 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
180
181 def uniq {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
182 "Returns a new Array with all unique values (double entries are skipped)."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
183
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
184 uniq_vals = []
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
185 each: |x| {
b0a5814 @bakkdoor Use unless:do: instead of if_false: in lib/
authored
186 unless: (uniq_vals includes?: x) do: {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
187 uniq_vals << x
188 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
189 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
190 uniq_vals
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
191 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
192
193 def size {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
194 "Returns the size of an Enumerable."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
195
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
196 i = 0
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
197 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
198 i = i + 1
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
199 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
200 i
a9e17b2 @bakkdoor simplified class definitions (no need for semicolons after method def…
authored
201 }
9cd9cdf @bakkdoor Added some stdlib code
authored
202
c969e48 @bakkdoor lib/: moved empty? method from Array to Enumerable. Also removed empt…
authored
203 def empty? {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
204 "Indicates, if the Enumerable is empty (has no elements)."
c969e48 @bakkdoor lib/: moved empty? method from Array to Enumerable. Also removed empt…
authored
205 self size == 0
206 }
207
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
208 def first {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
209 self each: |x| {
210 return x
211 }
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
212 }
7842ca3 @bakkdoor removed trailing whitespace from lib/ files
authored
213
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
214 def last {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
215 "Returns the last element in an Enumerable."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
216
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
217 item = nil
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
218 each: |x| {
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
219 item = x
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
220 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
221 item
222 }
2f1b175 @bakkdoor added Enumerable#compact, Array#remove_at:, Array#compact! and Array#…
authored
223
224 def compact {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
225 "Returns a new @Array@ with all values removed that are @nil ( return @true on @nil? )."
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
226
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
227 reject: |x| { x nil? }
2f1b175 @bakkdoor added Enumerable#compact, Array#remove_at:, Array#compact! and Array#…
authored
228 }
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
229
fafa726 @bakkdoor lib/enumerable.fnc: Rewrote Enumerable#min & Enumerable#max with use …
authored
230 def superior_by: comparison_block {
b573f80 @vic Using RDiscount as markdown impl. Implemented DocString in fancy.
vic authored
231 "Returns the superiour element in the @Enumerable that has met the given comparison block with all other elements."
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
232
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
233 retval = self first
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
234 each: |x| {
e7df0b5 @bakkdoor Use if:then:else: instead of if_true:else: since it's more familiar t…
authored
235 if: (comparison_block call: [x, retval]) then: {
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
236 retval = x
237 }
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
238 }
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
239 retval
240 }
7842ca3 @bakkdoor removed trailing whitespace from lib/ files
authored
241
fafa726 @bakkdoor lib/enumerable.fnc: Rewrote Enumerable#min & Enumerable#max with use …
authored
242 def max {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
243 "Returns the maximum value in the Enumerable (via the '>' comparison message)."
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
244 superior_by: '>
fafa726 @bakkdoor lib/enumerable.fnc: Rewrote Enumerable#min & Enumerable#max with use …
authored
245 }
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
246
247 def min {
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
248 "Returns the minimum value in the Enumerable (via the '<' comparison message)."
c20d848 @bakkdoor Added support for sending keyword messages to self without needing to…
authored
249 superior_by: '<
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
250 }
fdd6534 @bakkdoor Added FancyEnumerable#partition_by:
authored
251
252 def partition_by: block {
253 last = block call: [self first]
254 coll = []
255 tmp_coll = []
256 self each: |x| {
257 tmp = block call: [x]
258 if: (tmp != last) then: {
259 coll << tmp_coll
260 tmp_coll = [x]
261 } else: {
262 tmp_coll << x
263 }
264 last = tmp
265 }
266 coll << tmp_coll
267 coll
268 }
9cd9cdf @bakkdoor Added some stdlib code
authored
269 }
Something went wrong with that request. Please try again.