Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 637 lines (514 sloc) 14.814 kB
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
1 class Fancy {
2 class Enumerable {
de40259 @bakkdoor Moved Array#each_with_index: into FancyEnumerable.
authored
3 """
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
4 Mixin-Class with useful methods for collections that implement an @each:@ method.
de40259 @bakkdoor Moved Array#each_with_index: into FancyEnumerable.
authored
5 """
6
1eb7c5f @bakkdoor Improved Array#[] and String#[] and updates tests
authored
7 def at: index {
8 """
9 @index @Fixnum@ that is the 0-based index into @self.
10 @return Value in @self at 0-based position defined by @index.
11
12 Example:
13 \"foo\” at: 2 # => \"o\"
14 \"foo\” at: 3 # => nil
15 """
16
17 i = 0
18 each: |x| {
19 { return x } if: $ i == index
20 i = i + 1
21 }
391762b @bakkdoor Moved Array#first - #fourth to Enumerable
authored
22 return nil
1eb7c5f @bakkdoor Improved Array#[] and String#[] and updates tests
authored
23 }
24
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
25 def each_with_index: block {
26 """
27 @block @Block@ to be called with each element and its index in the @self.
28 @return @self
29
30 Iterate over all elements in @self.
31 Calls a given @Block@ with each element and its index.
32 """
33
34 i = 0
35 each: |x| {
36 block call: [x, i]
37 i = i + 1
38 }
de40259 @bakkdoor Moved Array#each_with_index: into FancyEnumerable.
authored
39 }
40
391762b @bakkdoor Moved Array#first - #fourth to Enumerable
authored
41 def first {
42 """
43 @return The first element in the @Fancy::Enumerable@.
44 """
45 at: 0
46 }
47
48 def second {
49 """
50 @return The second element in the @Fancy::Enumerable@.
51 """
52 at: 1
53 }
54
55 def third {
56 """
57 @return The third element in the @Fancy::Enumerable@.
58 """
59 at: 2
60 }
61
62 def fourth {
63 """
64 @return The fourth element in the @Fancy::Enumerable@.
65 """
66 at: 3
67 }
68
69 def last {
70 """
71 @return Last element in @self or @nil, if empty.
72
73 Returns the last element in a @Fancy::Enumerable@.
74 """
75
76 item = nil
77 each: |x| {
78 item = x
79 }
80 item
81 }
82
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
83 def includes?: item {
84 """
85 @item Item to check if it's included in @self.
86 @return @true, if @item in @self, otherwise @false.
0578628 @bakkdoor optimized some docstrings for lib/enumerable.fy
authored
87
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
88 Indicates, if a collection includes a given element.
89 """
5906d4e @bakkdoor Added newline after docstring in FancyEnumerable.
authored
90
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
91 any?: @{ == item }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
92 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
93
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
94 def each: each_block in_between: between_block {
95 """
96 Similar to @each:@ but calls an additional @Block@ between
97 calling the first @Block@ for each element in self.
98 """
b77a481 @bakkdoor Moved Array#each:in_between: to FancyEnumerable class, as it can be u…
authored
99
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
100 between = { between = between_block }
101 each: |x| {
102 between call
103 each_block call: [x]
104 }
b77a481 @bakkdoor Moved Array#each:in_between: to FancyEnumerable class, as it can be u…
authored
105 }
106
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
107 def join: str {
108 """
109 @str Value (usually a @String@) to be used for the joined @String@.
110 @return @String@ containing all elements in @self interspersed with @str.
196a896 @bakkdoor Added FancyEnumerable#join:
authored
111
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
112 Joins a collection with a @String@ between each element, returning a new @String@.
196a896 @bakkdoor Added FancyEnumerable#join:
authored
113
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
114 \"hello, world\" join: \"-\" # => \"h-e-l-l-o-,- -w-o-r-l-d\"
115 """
196a896 @bakkdoor Added FancyEnumerable#join:
authored
116
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
117 s = ""
118 each: |c| {
119 s << c
120 } in_between: {
121 s << str
122 }
123 s
196a896 @bakkdoor Added FancyEnumerable#join:
authored
124 }
125
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
126 def any?: condition {
127 """
128 @condition @Block@ (or @Callable) that is used to check if any element in @self yields true for it.
129 @return @true, if @condition yields @true for any element, @false otherwise.
0578628 @bakkdoor optimized some docstrings for lib/enumerable.fy
authored
130
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
131 Indicates, if any element meets the condition.
132 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
133
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
134 each: |x| {
135 if: (condition call: [x]) then: {
136 return true
137 }
47ba5c2 @bakkdoor added . & $ operators. operator-methods are by default binary (take 1…
authored
138 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
139 false
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
140 }
9cd9cdf @bakkdoor Added some stdlib code
authored
141
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
142 def all?: condition {
143 """
144 @block Predicate @Block@ to be called for each element until it returns @false for any one of them.
145 @return @true if all elements in @self yield @true for @block, @false otherwise.
4cb535c @bakkdoor Small changes to Array methods.
authored
146
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
147 Takes condition-block and returns @true if all elements meet it.
148 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
149
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
150 each: |x| {
151 unless: (condition call: [x]) do: {
152 return false
153 }
47ba5c2 @bakkdoor added . & $ operators. operator-methods are by default binary (take 1…
authored
154 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
155 true
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
156 }
9cd9cdf @bakkdoor Added some stdlib code
authored
157
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
158 def find: item {
159 """
160 @item Item to be found in @self.
161 @return The first element that is equal to @item or @nil, if none found.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
162
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
163 Returns @nil, if @item (or anything that returns @true when comparing to @item) isn't found.
164 Otherwise returns that element that is equal to @item.
165 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
166
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
167 if: (item is_a?: Block) then: {
168 find_by: item
169 } else: {
170 each: |x| {
171 if: (item == x) then: {
172 return x
173 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
174 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
175 nil
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
176 }
9cd9cdf @bakkdoor Added some stdlib code
authored
177 }
178
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
179 def find_by: block {
180 """
181 Similar to @find:@ but takes a block that is called for each element to find it.
182 """
0550117 @bakkdoor added more docstrings to core classes in lib/ directory.
authored
183
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
184 each: |x| {
ad5d10d @bakkdoor Fixed bug in FancyEnumerable#find:
authored
185 if: (block call: [x]) then: {
186 return x
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
187 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
188 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
189 nil
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
190 }
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
191
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
192 def map: block {
193 """
194 @block A @Block@ that gets called with each element in @self.
195 @return An @Array@ containing all values of calling @block with each element in @self.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
196
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
197 Returns a new @Array@ with the results of calling a given block for every element.
198 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
199
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
200 coll = []
201 each: |x| {
202 coll << (block call: [x])
203 }
204 coll
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
205 }
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
206
966b5c2 @bakkdoor Added Enumerable#map_with_index:
authored
207 def map_with_index: block {
208 """
209 @block A @Block@ that gets called with each element and its index in @self.
210 @return An @Array@ containing all values of calling @block with each element and its index in @self.
211
212 Returns a new @Array@ with the results of calling a given block for every element and its index.
213 """
214
215 coll = []
216 each_with_index: |x i| {
217 coll << (block call: [x, i])
218 }
219 coll
220 }
221
be0b1fe @bakkdoor added Enumerable#map_chained:
authored
222 def map_chained: blocks {
223 """
224 @blocks Collection of @Block@s to be called sequentially for every element in @self.
225 @return Collection of all values in @self successively called with all blocks in @blocks.
226
227 Example:
228 (1,2,3) map_chained: (@{ + 1 }, 'to_s, @{ * 2 })
229 # => [\"22\", \"33\", \"44\"]
230 """
231
232 map: |v| {
233 blocks inject: v into: |acc b| {
234 b call: [acc]
235 }
236 }
237 }
238
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
239 def select: condition {
240 """
241 @condition A @Block@ that is used as a filter on all elements in @self.
242 @return An @Array@ containing all elements in @self that yield @true when called with @condition.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
243
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
244 Returns a new @Array@ with all elements that meet the given condition block.
245 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
246
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
247 coll = []
248 each: |x| {
249 { coll << x } if: $ condition call: [x]
250 }
251 coll
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
252 }
39e4b43 @bakkdoor lib/enumerable: removed whitespace and unneeded semicolons
authored
253
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
254 def reject: condition {
255 """
256 Similar to @select:@ but inverse.
257 Returns a new @Array@ with all elements that don't meet the given condition block.
258 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
259
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
260 coll = []
261 each: |x| {
262 { coll << x } unless: $ condition call: [x]
263 }
264 coll
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
265 }
7842ca3 @bakkdoor removed trailing whitespace from lib/ files
authored
266
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
267 def take_while: condition {
268 """
269 @condition A @Block@ that is used as a condition for filtering.
270 @return An @Array@ of all elements from the beginning until @condition yields @false.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
271
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
272 Returns a new @Array@ by taking elements from the beginning
273 as long as they meet the given condition block.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
274
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
275 Example:
276 [1,2,3,4,5] take_while: |x| { x < 4 } # => [1,2,3]
277 """
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
278
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
279 coll = []
280 each: |x| {
281 if: (condition call: [x]) then: {
282 coll << x
283 } else: {
284 return coll
285 }
9cd9cdf @bakkdoor Added some stdlib code
authored
286 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
287 coll
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
288 }
9cd9cdf @bakkdoor Added some stdlib code
authored
289
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
290 def drop_while: condition {
291 """
292 Similar to @take_while:@ but inverse.
293 Returns a new @Array@ by skipping elements from the beginning
294 as long as they meet the given condition block.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
295
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
296 Example:
297 [1,2,3,4,5] drop_while: |x| { x < 4 } # => [4,5]
298 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
299
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
300 coll = []
301 drop = nil
302 first_check = true
303 each: |x| {
304 if: (drop or: first_check) then: {
305 drop = condition call: [x]
306 first_check = nil
307 # check, if we actually have to insert this one:
308 unless: drop do: {
309 coll << x
310 }
311 } else: {
eba7d2f @bakkdoor Fixed Enumerable#drop_while:, refactored some Enumerable methods & ad…
authored
312 coll << x
313 }
f2a436f @bakkdoor small improvements to lib/Enumerable.fnc
authored
314 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
315 coll
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
316 }
9cd9cdf @bakkdoor Added some stdlib code
authored
317
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
318 def take: amount {
319 """
320 @amount Amount of elements to take from @self.
321 @return First @amount elements of @self in an @Array@.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
322
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
323 Example:
324 [1,2,3,4] take: 2 # => [1,2]
325 """
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
326
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
327 i = 0
328 take_while: {
329 i = i + 1
330 i <= amount
331 }
6cd9d3c @bakkdoor Added FancyEnumerable#drop: & FancyEnumerable#take:
authored
332 }
333
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
334 def drop: amount {
335 """
336 @amount Amount of elements to skip in @self.
337 @return An @Array@ of all but the first @amount elements in @self.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
338
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
339 Example:
340 [1,2,3,4,5] drop: 2 # => [3,4,5]
341 """
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
342
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
343 i = 0
344 drop_while: {
345 i = i + 1
346 i <= amount
347 }
6cd9d3c @bakkdoor Added FancyEnumerable#drop: & FancyEnumerable#take:
authored
348 }
349
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
350 alias_method: 'skip: for: 'drop:
0a87397 @bakkdoor added Enumerable#skip: as alias for Enumerable#drop:
authored
351
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
352 def reduce: block init_val: init_val {
353 """
354 Calculates a value based on a given block to be called on an accumulator
355 value and an initial value.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
356
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
357 Example:
358 [1,2,3] reduce: |sum val| { sum + val } init_val: 0 # => 6
359 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
360
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
361 acc = init_val
362 each: |x| {
363 acc = (block call: [acc, x])
364 }
365 acc
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
366 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
367
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
368 def inject: val into: block {
369 """
370 Same as reduce:init_val: but taking the initial value as first
371 and the reducing block as second parameter.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
372
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
373 Example:
374 [1,2,3] inject: 0 into: |sum val| { sum + val } # => 6
375 """
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
376
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
377 reduce: block init_val: val
378 }
a65383a @bakkdoor added FancyEnumerable#inject:into: as alternative to reduce:init_val:
authored
379
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
380 def uniq {
381 """
382 @return @Array@ of all unique elements in @self.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
383
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
384 Returns a new Array with all unique values (double entries are skipped).
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
385
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
386 Example:
387 [1,2,1,2,3] uniq # => [1,2,3]
388 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
389
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
390 uniq_vals = []
391 each: |x| {
392 unless: (uniq_vals includes?: x) do: {
393 uniq_vals << x
394 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
395 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
396 uniq_vals
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
397 }
e3956b1 @bakkdoor added support for mixins. it's now possible to include classes into o…
authored
398
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
399 def size {
400 """
401 @return Amount of elements in @self.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
402
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
403 Returns the size of an Enumerable.
404 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
405
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
406 i = 0
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
407 each: {
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
408 i = i + 1
409 }
410 i
0e81073 @bakkdoor HUUGE CHANGE: Semicolons are history - or at least completely optiona…
authored
411 }
9cd9cdf @bakkdoor Added some stdlib code
authored
412
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
413 def empty? {
414 """
415 @return @true, if size of @self is 0, @false otherwise.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
416
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
417 Indicates, if the Enumerable is empty (has no elements).
418 """
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
419
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
420 size == 0
421 }
c969e48 @bakkdoor lib/: moved empty? method from Array to Enumerable. Also removed empt…
authored
422
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
423 def compact {
424 """
425 @return @Array@ with all non-nil elements in @self.
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
426
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
427 Returns a new @Array@ with all values removed that are @nil ( return @true on @nil? ).
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
428
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
429 Example:
430 [1,2,nil,3,nil] compact # => [1,2,3]
431 """
d841825 @bakkdoor lib/: added missing docstrings to methods
authored
432
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
433 reject: @{ nil? }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
434 }
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
435
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
436 def superior_by: comparison_block taking: selection_block ('identity) {
437 """
438 @comparison_block @Block@ to be used for comparison.
439 @selection_block @Block@ to be used for selecting the values to be used for comparison by @comparison_bock.
440 @return Superior element in @self in terms of @comparison_block.
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
441
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
442 Returns the superior element in the @Enumerable that has met
443 the given comparison block with all other elements,
444 applied to whatever @selection_block returns for each element.
445 @selection_block defaults to @identity.
446
447 Examples:
448 [1,2,5,3,4] superior_by: '> # => 5
449 [1,2,5,3,4] superior_by: '< # => 1
450 [[1,2], [2,3,4], [], [1]] superior_by: '> taking: 'size # => [2,3,4]
451 [[1,2], [2,3,4], [-1]] superior_by: '< taking: 'first # => [-1]
452 """
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
453
454
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
455 pairs = self map: |val| {
456 (val, selection_block call: [val])
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
457 }
7842ca3 @bakkdoor removed trailing whitespace from lib/ files
authored
458
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
459 retval = pairs first
460 pairs each: |p| {
461 if: (comparison_block call: [p second, retval second]) then: {
462 retval = p
463 }
464 }
465 retval first
466 }
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
467
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
468 def max {
469 """
470 @return Maximum value in @self.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
471
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
472 Returns the maximum value in the Enumerable (via the '>' comparison message).
473 """
ec8dc56 @bakkdoor lib/enumerable.fnc: Added Enumerable#max & Enumerable#min methods.
authored
474
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
475 superior_by: '>
476 }
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
477
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
478 def min {
479 """
480 @return Minimum value in @self.
90724ed @bakkdoor Added examples to docstrings in FancyEnumerable and refactored superi…
authored
481
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
482 Returns the minimum value in the Enumerable (via the '<' comparison message).
483 """
fdd6534 @bakkdoor Added FancyEnumerable#partition_by:
authored
484
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
485 superior_by: '<
486 }
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
487
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
488 def sum {
489 """
490 Calculates the sum of all the elements in the @Enumerable
491 (assuming them to be @Number@s (implementing '+' & '*')).
492 """
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
493
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
494 reduce: '+ init_val: 0
495 }
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
496
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
497 def product {
498 """
499 Calculates the product of all the elements in the @Enumerable
500 (assuming them to be @Number@s (implementing @+ & @*)).
501 """
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
502
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
503 reduce: '* init_val: 1
504 }
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
505
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
506 def average {
507 """
508 @return Average value in @self (expecting @Number@s or Objects implementing @+ and @*).
509 """
836ab35 @bakkdoor Moved FancyEnumerable#sum and #product to lib/enumerable.fy and also …
authored
510
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
511 { return 0 } if: (size == 0)
512 sum to_f / size
513 }
63914af @bakkdoor Fixed docstrings for FancyEnumerable
authored
514
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
515 def partition_by: block {
516 """
517 @block @Block@ that gets used to decide when to partition elements in @self.
518 @return @Array@ of @Array@s, partitioned by equal return values of calling @block with each element
519
520 Example:
521 0 upto: 10 . partition_by: |x| { x < 3 } # => [[0, 1, 2], [3, 4, 5, 6, 7, 8, 9, 10]]
522 """
523 last = block call: [first]
524 coll = []
525 tmp_coll = []
526 each: |x| {
527 tmp = block call: [x]
528 if: (tmp != last) then: {
529 coll << tmp_coll
530 tmp_coll = [x]
531 } else: {
532 tmp_coll << x
533 }
534 last = tmp
fdd6534 @bakkdoor Added FancyEnumerable#partition_by:
authored
535 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
536 coll << tmp_coll
537 coll
fdd6534 @bakkdoor Added FancyEnumerable#partition_by:
authored
538 }
aa816bf @bakkdoor Added FancyEnumerable#random which returns a random element in a coll…
authored
539
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
540 def random {
541 """
542 @return Random element in @self.
543 """
aa816bf @bakkdoor Added FancyEnumerable#random which returns a random element in a coll…
authored
544
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
545 at: $ size random
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
546 }
0fd95d0 @bakkdoor More ruby aliases for comparison operators and added generic Object#<…
authored
547
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
548 def sort_by: block {
549 """
550 @block @Block@ taking 2 arguments used to compare elements in a collection.
551 @return Sorted @Array@ of elements in @self.
0fd95d0 @bakkdoor More ruby aliases for comparison operators and added generic Object#<…
authored
552
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
553 Sorts a collection by a given comparison block.
554 """
0fd95d0 @bakkdoor More ruby aliases for comparison operators and added generic Object#<…
authored
555
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
556 if: (block is_a?: Symbol) then: {
557 sort() |a b| {
558 a receive_message: block . <=> (b receive_message: block)
559 }
560 } else: {
561 sort(&block)
0fd95d0 @bakkdoor More ruby aliases for comparison operators and added generic Object#<…
authored
562 }
563 }
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
564
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
565 def in_groups_of: size {
566 """
567 @size Maximum size of each group.
568 @return @Array@ of @Array@s with a max size of @size (grouped).
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
569
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
570 Example usage:
571 [1,2,3,4,5] in_groups_of: 3 # => [[1,2,3],[4,5]]
572 """
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
573
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
574 groups = []
575 tmp = []
576 enum = to_enum
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
577
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
578 loop: {
579 size times: {
580 tmp << (enum next)
581 }
582
583 if: (enum ended?) then: {
584 { groups << tmp } unless: $ tmp empty?
585 break
586 }
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
587
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
588 groups << tmp
589 tmp = []
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
590 }
591
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
592 groups
1f15b36 @bakkdoor Added FancyEnumerator#ended? and FancyEnumerable#in_groups_of:
authored
593 }
594
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
595 def reverse {
596 """
597 @return @self in reverse order.
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
598
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
599 Returns @self in reverse order.
600 This only makes sense for collections that have an ordering.
601 In either case, it simply converts @self to an @Array@ and returns it in reversed order.
602 """
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
603
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
604 self to_a reverse
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
605 }
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
606
862fec8 @bakkdoor added Enumerable#to_hash:
authored
607 def to_hash: block {
608 """
609 @block @Block@ to be called to get the key for each element in @self.
610 @return @Hash@ of key/value pairs based on values in @self.
611
612 Example:
613 [\"foo\", \”hello\", \"ok\", \"\"] to_hash: @{ size }
614 # => <[3 => \"foo\", 5 => \"hello\", 2 => \"ok\", 0 => \"\"]>
615 """
616
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
617
618 inject: <[]> into: |h val| {
862fec8 @bakkdoor added Enumerable#to_hash:
authored
619 key = block call: [val]
620 h[key]: val
d734ad5 @bakkdoor some cleanups to Enumerable class
authored
621 h
862fec8 @bakkdoor added Enumerable#to_hash:
authored
622 }
623 }
624
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
625 def reverse_each: block {
626 """
627 @block @Block@ to be called for each element in reverse order.
628 @return @self
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
629
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
630 Runs @block for each element on reversed version of self.
631 If @self is not a sorted collection, no guarantees about the reverse order can be given.
632 """
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
633
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
634 reverse each: block
635 }
1f1be4b @bakkdoor Added more methods and tests to Array and Tuple.
authored
636 }
a9ec751 @bakkdoor moves FancyEnumerable and FancyEnumerator into Fancy namespace instea…
authored
637 }
Something went wrong with that request. Please try again.