Class Prelude::List
In: lib/prelude/list.rb
Parent: Array

$Id: list.rb 7 2006-09-06 17:03:26Z prelude $

This eventually needs to be implemented with lazy lists, see lazylist.rubyforge.org for details

I used the signatures of Haskell’s List.hs file in order not to forget to implement the functions defined there and to remind of what was intended.

The following methods are implemented by Array with the same semantics:

  • last — :: [a] -> a
  • length — :: [a] -> Int
  • map — :: (a -> b) -> [a] -> [b]
  • reverse — :: [a] -> [a]
  • transpose — :: [[a]] -> [[a]]
  • find — :: (a -> Bool) -> [a] -> Maybe a
  • partition — :: (a -> Bool) -> [a] -> ([a], [a])
  • zip — :: [a] -> [b] -> [(a,b)]
  • sort — :: (Ord a) => [a] -> [a]

I do not know how to implement these in Ruby:

  • (!!) — :: [a] -> Int -> a
  • (\) — :: (Eq a) => [a] -> [a] -> [a]

Methods

+   all   and   any   break   concat   concatMap   concat_map   cycle   delete   drop   dropWhile   drop_while   elem   elemIndex   elemIndices   elem_index   elem_indices   f_foldl   filter   findIndex   findIndices   find_index   find_indices   foldl   foldl1   foldl1_   foldl_   foldr   foldr1   group   head   init   inits   insert   intersect   intersperse   isPrefixOf   isSuffixOf   is_prefix_of   is_suffix_of   iterate   lines   lookup   mapAccumL   mapAccumR   map_accum_l   map_accum_r   maximum   minimum   notElem   not_elem   nub   null   or   product   repeat   replicate   scanl   scanl1   scanr   scanr1   span   splitAt   split_at   sum   tail   tails   take   takeWhile   take_while   unfoldr   union   unlines   unwords   unzip   unzip3   unzip4   words   zip3   zip4   zipWith   zipWith3   zipWith4   zip_with   zip_with3   zip_with4  

External Aliases

+ -> array_plus
  Array compatibility functions

Public Instance methods

[Source]

    # File lib/prelude/list.rb, line 55
55:     def +(o)
56:       List.new(self.array_plus(o))
57:     end
all — :(a -> Bool) -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 159
159:     def all(&block)
160:       each{|e| return false unless block.call(e)}
161:       return true
162:     end
and — :[Bool] -> Bool

[Source]

     # File lib/prelude/list.rb, line 143
143:     def and
144:         foldr(true){|x,y| x && y}
145:     end
any — :(a -> Bool) -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 153
153:     def any(&block)
154:       each{|e| return true if block.call(e)}
155:       return false
156:     end
break — :(a -> Bool) -> [a] -> ([a], [a])

[Source]

     # File lib/prelude/list.rb, line 315
315:     def break
316:       warn "Method 'break' is not implemented yet." if $VERBOSE
317:       return []
318:     end

Implemented by Array but semantics is different

concat — :[[a]] -> [a]

[Source]

     # File lib/prelude/list.rb, line 131
131:     def concat
132:       flatten
133:     end
concatMap()

Alias for concat_map

concatMap — :(a -> [b]) -> [a] -> [b]

[Source]

     # File lib/prelude/list.rb, line 136
136:     def concat_map
137:       warn "Method 'concatMap' is not implemented yet." if $VERBOSE
138:       return []
139:     end
cycle — :[a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 252
252:     def cycle
253:       warn "Method 'cycle' is not implemented yet." if $VERBOSE
254:       return []
255:     end
delete — :(Eq a) => a -> [a] -> [a]

Implemented by Array but semantics is different

[Source]

     # File lib/prelude/list.rb, line 516
516:     def delete(o)
517:       warn "Method 'delete' is not implemented yet." if $VERBOSE
518:       return []
519:     end
drop — :Int -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 277
277:     def drop(n)
278:       self[n..-1]
279:     end
dropWhile()

Alias for drop_while

dropWhile — :(a -> Bool) -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 299
299:     def drop_while
300:       r = []
301:       each{ |e|
302:         next if yield(e)
303:         r << e
304:       }
305:       r
306:     end
elem — :a -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 359
359:     def elem
360:       warn "Method 'elem' is not implemented yet." if $VERBOSE
361:       return []
362:     end
elemIndex()

Alias for elem_index

elemIndices()

Alias for elem_indices

elemIndex — :(Eq a) => a -> [a] -> Maybe Int

[Source]

     # File lib/prelude/list.rb, line 393
393:     def elem_index
394:       warn "Method 'elem_index' is not implemented yet." if $VERBOSE
395:       return []
396:     end
elemIndices — :(Eq a) => a -> [a] -> [Int]

[Source]

     # File lib/prelude/list.rb, line 400
400:     def elem_indices
401:       warn "Method 'elem_indices' is not implemented yet." if $VERBOSE
402:       return []
403:     end

Classic recursive functional definition causes stack overflow for arrays of any usable size… So don’t use it, it’s here for demonstration purposes only. Use foldl.

[Source]

    # File lib/prelude/list.rb, line 90
90:     def f_foldl(s, &block)
91:       empty? ? s : tail.f_foldl(block.call(s, head), &block)
92:     end
filter — :(a -> Bool) -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 380
380:     def filter
381:       warn "Method 'filter' is not implemented yet." if $VERBOSE
382:       return []
383:     end
findIndex()

Alias for find_index

findIndices()

Alias for find_indices

findIndex — :(a -> Bool) -> [a] -> Maybe Int

[Source]

     # File lib/prelude/list.rb, line 407
407:     def find_index
408:       warn "Method 'find_index' is not implemented yet." if $VERBOSE
409:       return []
410:     end
findIndices — :(a -> Bool) -> [a] -> [Int]

[Source]

     # File lib/prelude/list.rb, line 414
414:     def find_indices
415:       warn "Method 'find_indices' is not implemented yet." if $VERBOSE
416:       return []
417:     end

This is a more pedestrian iterative version of f_foldl

foldl — :(a -> b -> a) -> a -> [b] -> a

[Source]

    # File lib/prelude/list.rb, line 96
96:     def foldl(s, &block)
97:       inject(s){ |a,b| block.call(a,b) }
98:     end
foldl1 — :(a -> a -> a) -> [a] -> a

[Source]

     # File lib/prelude/list.rb, line 107
107:     def foldl1(&block)
108:       tail.foldl(head, &block)
109:     end
foldl1’ — :(a -> a -> a) -> [a] -> a

[Source]

     # File lib/prelude/list.rb, line 112
112:     def foldl1_
113:       warn "Method 'foldl1_' is not implemented yet." if $VERBOSE
114:       return []
115:     end
foldl’ — :(a -> b -> a) -> a -> [b] -> a

[Source]

     # File lib/prelude/list.rb, line 101
101:     def foldl_
102:       warn "Method 'foldl_' is not implemented yet." if $VERBOSE
103:       return []
104:     end
foldr — :(a -> b -> b) -> b -> [a] -> b

[Source]

     # File lib/prelude/list.rb, line 118
118:     def foldr(s, &block)
119:       inject(s){ |a,b| block.call(b, a) }
120:     end
foldr1 — :(a -> a -> a) -> [a] -> a

[Source]

     # File lib/prelude/list.rb, line 123
123:     def foldr1(&block)
124:       tail.foldr(head, &block)
125:     end
group — :Eq a => [a] -> [[a]]

[Source]

     # File lib/prelude/list.rb, line 321
321:     def group
322:       warn "Method 'group' is not implemented yet." if $VERBOSE
323:       return []
324:     end
head — :[a] -> a

[Source]

    # File lib/prelude/list.rb, line 60
60:     def head
61:       self[0]
62:     end
init — :[a] -> [a]

[Source]

    # File lib/prelude/list.rb, line 70
70:     def init
71:       self[0..-2]
72:     end
inits — :[a] -> [[a]]

[Source]

     # File lib/prelude/list.rb, line 327
327:     def inits
328:       warn "Method 'inits' is not implemented yet." if $VERBOSE
329:       return []
330:     end

Implemented by Array but semantics is different

insert — :(Ord a) => a -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 537
537:     def insert(o)
538:       warn "Method 'insert' is not implemented yet." if $VERBOSE
539:       return []
540:     end
intersect — :(Eq a) => [a] -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 528
528:     def intersect
529:       warn "Method 'intersect' is not implemented yet." if $VERBOSE
530:       return []
531:     end
intersperse — :a -> [a] -> [a]

[Source]

    # File lib/prelude/list.rb, line 80
80:     def intersperse
81:       warn "Method 'intersperse' is not implemented yet." if $VERBOSE
82:       return []
83:     end
isPrefixOf()

Alias for is_prefix_of

isSuffixOf()

Alias for is_suffix_of

isPrefixOf — :(Eq a) => [a] -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 341
341:     def is_prefix_of
342:       warn "Method 'is_prefix_of' is not implemented yet." if $VERBOSE
343:       return []
344:     end
isSuffixOf — :(Eq a) => [a] -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 348
348:     def is_suffix_of
349:       warn "Method 'is_suffix_of' is not implemented yet." if $VERBOSE
350:       return []
351:     end
iterate — :(a -> a) -> a -> [a]

[Source]

     # File lib/prelude/list.rb, line 234
234:     def iterate
235:       warn "Method 'iterate' is not implemented yet." if $VERBOSE
236:       return []
237:     end
lines — :String -> [String]

[Source]

     # File lib/prelude/list.rb, line 482
482:     def lines
483:       warn "Method 'lines' is not implemented yet." if $VERBOSE
484:       return []
485:     end
lookup — :(Eq a) => a -> [(a,b)] -> Maybe b

[Source]

     # File lib/prelude/list.rb, line 372
372:     def lookup
373:       warn "Method 'lookup' is not implemented yet." if $VERBOSE
374:       return []
375:     end
mapAccumL()

Alias for map_accum_l

mapAccumR()

Alias for map_accum_r

mapAccumL — :(a -> b -> (a,c)) -> a -> [b] -> (a,[c])

[Source]

     # File lib/prelude/list.rb, line 217
217:     def map_accum_l
218:       warn "Method 'map_accum_l' is not implemented yet." if $VERBOSE
219:       return []
220:     end
mapAccumR — :(a -> b -> (a,c)) -> a -> [b] -> (a,[c])

[Source]

     # File lib/prelude/list.rb, line 224
224:     def map_accum_r
225:       warn "Method 'map_accum_r' is not implemented yet." if $VERBOSE
226:       return []
227:     end
maximum — :(Ord a) => [a] -> a

[Source]

     # File lib/prelude/list.rb, line 177
177:     def maximum
178:       warn "Method 'maximum' is not implemented yet." if $VERBOSE
179:       return []
180:     end
minimum — :(Ord a) => [a] -> a

[Source]

     # File lib/prelude/list.rb, line 183
183:     def minimum
184:       warn "Method 'minimum' is not implemented yet." if $VERBOSE
185:       return []
186:     end
notElem()

Alias for not_elem

notElem — :a -> [a] -> Bool

[Source]

     # File lib/prelude/list.rb, line 365
365:     def not_elem
366:       warn "Method 'not_elem' is not implemented yet." if $VERBOSE
367:       return []
368:     end
nub — :(Eq a) => [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 509
509:     def nub
510:       warn "Method 'nub' is not implemented yet." if $VERBOSE
511:       return []
512:     end
null — :[a] -> Bool

[Source]

    # File lib/prelude/list.rb, line 75
75:     def null
76:       size == 0
77:     end
or — :[Bool] -> Bool

[Source]

     # File lib/prelude/list.rb, line 148
148:     def or
149:         foldr(false){|x,y| (x || y)}
150:     end
product — :(Num a) => [a] -> a

[Source]

     # File lib/prelude/list.rb, line 171
171:     def product
172:       warn "Method 'product' is not implemented yet." if $VERBOSE
173:       return []
174:     end
repeat — :a -> [a]

[Source]

     # File lib/prelude/list.rb, line 240
240:     def repeat
241:       warn "Method 'repeat' is not implemented yet." if $VERBOSE
242:       return []
243:     end
replicate — :Int -> a -> [a]

[Source]

     # File lib/prelude/list.rb, line 246
246:     def replicate
247:       warn "Method 'replicate' is not implemented yet." if $VERBOSE
248:       return []
249:     end
scanl — :(a -> b -> a) -> a -> [b] -> [a]

[Source]

     # File lib/prelude/list.rb, line 194
194:     def scanl(s, &block)
195:       inject([s]){ |a,b| a << block.call(a.last,b) }
196:     end
scanl1 — :(a -> a -> a) -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 199
199:     def scanl1(&block)
200:       tail.scanl(head, &block)
201:     end
scanr — :(a -> b -> b) -> b -> [a] -> [b]

[Source]

     # File lib/prelude/list.rb, line 204
204:     def scanr(s, &block)
205:       inject([s]){ |a,b| a << block.call(b, a.last) }
206:     end
scanr1 — :(a -> a -> a) -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 209
209:     def scanr1(&block)
210:       tail.scanr(head, &block)
211:     end
span — :(a -> Bool) -> [a] -> ([a], [a])

[Source]

     # File lib/prelude/list.rb, line 310
310:     def span(&block)
311:       [take_while(&block), drop_while(&block)]
312:     end
splitAt(n)

Alias for split_at

splitAt — :Int -> [a] -> ([a], [a])

[Source]

     # File lib/prelude/list.rb, line 282
282:     def split_at(n)
283:       [take(n), drop(n)]
284:     end
sum — :(Num a) => [a] -> a

[Source]

     # File lib/prelude/list.rb, line 165
165:     def sum
166:       warn "Method 'sum' is not implemented yet." if $VERBOSE
167:       return []
168:     end
tail — :[a] -> [a]

[Source]

    # File lib/prelude/list.rb, line 65
65:     def tail
66:       self[1..-1]
67:     end
tails — :[a] -> [[a]]

[Source]

     # File lib/prelude/list.rb, line 333
333:     def tails
334:       warn "Method 'tails' is not implemented yet." if $VERBOSE
335:       return []
336:     end
take — :Int -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 272
272:     def take(n)
273:       self[0..(n-1)]
274:     end
takeWhile()

Alias for take_while

takeWhile — :(a -> Bool) -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 288
288:     def take_while
289:       r = []
290:       each{ |e|
291:         break unless yield(e)
292:         r << e
293:       }
294:       r
295:     end
unfoldr — :(b -> Maybe (a, b)) -> b -> [a]

[Source]

     # File lib/prelude/list.rb, line 261
261:     def unfoldr
262:       warn "Method 'unfoldr' is not implemented yet." if $VERBOSE
263:       return []
264:     end
union — :(Eq a) => [a] -> [a] -> [a]

[Source]

     # File lib/prelude/list.rb, line 522
522:     def union
523:       warn "Method 'union' is not implemented yet." if $VERBOSE
524:       return []
525:     end
unlines — :[String] -> String

[Source]

     # File lib/prelude/list.rb, line 494
494:     def unlines
495:       warn "Method 'unlines' is not implemented yet." if $VERBOSE
496:       return []
497:     end
unwords — :[String] -> String

[Source]

     # File lib/prelude/list.rb, line 500
500:     def unwords
501:       warn "Method 'unwords' is not implemented yet." if $VERBOSE
502:       return []
503:     end
unzip — :[(a,b)] -> ([a],[b])

[Source]

     # File lib/prelude/list.rb, line 459
459:     def unzip
460:       warn "Method 'unzip' is not implemented yet." if $VERBOSE
461:       return []
462:     end

unzip3

[Source]

     # File lib/prelude/list.rb, line 465
465:     def unzip3
466:       warn "Method 'unzip3' is not implemented yet." if $VERBOSE
467:       return []
468:     end

unzip4, unzip5, unzip6, unzip7

[Source]

     # File lib/prelude/list.rb, line 471
471:     def unzip4
472:       warn "Method 'unzip4' is not implemented yet." if $VERBOSE
473:       return []
474:     end
words — :String -> [String]

[Source]

     # File lib/prelude/list.rb, line 488
488:     def words
489:       warn "Method 'words' is not implemented yet." if $VERBOSE
490:       return []
491:     end

zip3

[Source]

     # File lib/prelude/list.rb, line 424
424:     def zip3
425:       warn "Method 'zip3' is not implemented yet." if $VERBOSE
426:       return []
427:     end

zip4, zip5, zip6, zip7

[Source]

     # File lib/prelude/list.rb, line 430
430:     def zip4
431:       warn "Method 'zip4' is not implemented yet." if $VERBOSE
432:       return []
433:     end
zipWith()

Alias for zip_with

zipWith3()

Alias for zip_with3

zipWith4()

Alias for zip_with4

zipWith — :(a -> b -> c) -> [a] -> [b] -> [c]

[Source]

     # File lib/prelude/list.rb, line 437
437:     def zip_with
438:       warn "Method 'zip_with' is not implemented yet." if $VERBOSE
439:       return []
440:     end

zipWith3

[Source]

     # File lib/prelude/list.rb, line 444
444:     def zip_with3
445:       warn "Method 'zip_with3' is not implemented yet." if $VERBOSE
446:       return []
447:     end

zipWith4, zipWith5, zipWith6, zipWith7

[Source]

     # File lib/prelude/list.rb, line 451
451:     def zip_with4
452:       warn "Method 'zip_with4' is not implemented yet." if $VERBOSE
453:       return []
454:     end