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:
I do not know how to implement these in Ruby:
+ | -> | array_plus |
Array compatibility functions |
all — : | (a -> Bool) -> [a] -> Bool |
# 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 |
# File lib/prelude/list.rb, line 143 143: def and 144: foldr(true){|x,y| x && y} 145: end
any — : | (a -> Bool) -> [a] -> Bool |
# 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]) |
# 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] |
# File lib/prelude/list.rb, line 131 131: def concat 132: flatten 133: end
concatMap — : | (a -> [b]) -> [a] -> [b] |
# 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] |
# 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
# 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] |
# File lib/prelude/list.rb, line 277 277: def drop(n) 278: self[n..-1] 279: end
dropWhile — : | (a -> Bool) -> [a] -> [a] |
# 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 |
# 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 — : | (Eq a) => a -> [a] -> Maybe Int |
# 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] |
# 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.
# 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] |
# 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 — : | (a -> Bool) -> [a] -> Maybe Int |
# 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] |
# 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
foldl1 — : | (a -> a -> a) -> [a] -> a |
# File lib/prelude/list.rb, line 107 107: def foldl1(&block) 108: tail.foldl(head, &block) 109: end
foldl1’ — : | (a -> a -> a) -> [a] -> a |
# 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 |
# 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 |
# 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 |
# File lib/prelude/list.rb, line 123 123: def foldr1(&block) 124: tail.foldr(head, &block) 125: end
group — : | Eq a => [a] -> [[a]] |
# File lib/prelude/list.rb, line 321 321: def group 322: warn "Method 'group' is not implemented yet." if $VERBOSE 323: return [] 324: end
inits — : | [a] -> [[a]] |
# 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] |
# 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] |
# 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] |
# 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 — : | (Eq a) => [a] -> [a] -> Bool |
# 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 |
# 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] |
# 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] |
# 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 |
# 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 — : | (a -> b -> (a,c)) -> a -> [b] -> (a,[c]) |
# 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]) |
# 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 |
# 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 |
# 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 — : | a -> [a] -> Bool |
# 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] |
# File lib/prelude/list.rb, line 509 509: def nub 510: warn "Method 'nub' is not implemented yet." if $VERBOSE 511: return [] 512: end
or — : | [Bool] -> Bool |
# File lib/prelude/list.rb, line 148 148: def or 149: foldr(false){|x,y| (x || y)} 150: end
product — : | (Num a) => [a] -> a |
# 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] |
# 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] |
# 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] |
# 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] |
# 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] |
# 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] |
# File lib/prelude/list.rb, line 209 209: def scanr1(&block) 210: tail.scanr(head, &block) 211: end
span — : | (a -> Bool) -> [a] -> ([a], [a]) |
# File lib/prelude/list.rb, line 310 310: def span(&block) 311: [take_while(&block), drop_while(&block)] 312: end
splitAt — : | Int -> [a] -> ([a], [a]) |
# File lib/prelude/list.rb, line 282 282: def split_at(n) 283: [take(n), drop(n)] 284: end
sum — : | (Num a) => [a] -> a |
# File lib/prelude/list.rb, line 165 165: def sum 166: warn "Method 'sum' is not implemented yet." if $VERBOSE 167: return [] 168: end
tails — : | [a] -> [[a]] |
# 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] |
# File lib/prelude/list.rb, line 272 272: def take(n) 273: self[0..(n-1)] 274: end
takeWhile — : | (a -> Bool) -> [a] -> [a] |
# 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] |
# 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] |
# 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 |
# 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 |
# 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]) |
# 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
# 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
# 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] |
# 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
# 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
# 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 — : | (a -> b -> c) -> [a] -> [b] -> [c] |
# 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
# 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