From: hipster Date: 2000-11-14T20:28:40+09:00 Subject: [ruby-talk:6357] Re: Another Smalltalk control structure idea On Tue, 14 Nov 2000 19:36:21 +0900, Hugh Sasse Staff Elec Eng wrote: > > > Hmm. What's this here? Your might want to consider "reduce: a > > > generalized inject function" > > > (http://www.xs4all.nl/~hipster/lib/ruby/reduce) as a starting > > > point. It's already written in Ruby. (What a coincidence! :-) > > > And it happens to have the name I prefer. > > > > Credits go to Dave Thomas, see [ruby-talk:03178]. I merely archived > > it for posterity. > > Is it any help to remind people that Python has > map > filter > reduce > which operate on lists? See: > http://www.python.org/doc/current/lib/built-in-funcs.html > for what they do. The following list contains all standard list operations as defined in the Haskell prelude (i.e. std. library), including examples. It would be nice to have some of them in a std. Ruby lib, like take, drop, takeWhile, zip etc. Talking about Haskell: infinite lists and lazy evaluation, maybe an idea for Ruby 1.8? 8) Michel -- prelude list functions list = [1..10] e1 = head list --> 1 e2 = last list --> 10 e3 = tail list --> [2..10] e4 = init list --> [1..9] e5 = null [] --> True e6 = null list --> False e7 = list ++ list --> [1..10,1..10] e8 = map (*2) list --> [2,4..20] e9 = filter (>7) list --> [8,9,10] e10 = concat [list,list] --> [1..10,1..10] e11 = length list --> 10 e12 = list!!0 --> 1 e13 = list!!3 --> 4 e14 = list!!(length list - 1) --> 10 -- (fold-left f x0 '(x1 x2 x3)) = (f (f (f x0 x1) x2) x3) e15 = foldl (+) 0 list --> 55 (0+(1+(2+..(10)))) e16 = foldl (*) 1 list --> 3628800 (1*1*2*..*10 = 10!) e17 = foldl1 (+) list --> 55 (1+(2+(3+..(10)))) e18 = scanl (+) 0 list --> [0,1,3,6,10,15,21,28,36,45,55] e19 = scanl1 (+) list --> [1,3,6,10,15,21,28,36,45,55] -- (fold-right f x0 '(x1 x2 x3)) = (f x1 (f x2 (f x3 x0))) e20 = foldr (+) 0 list --> 55 (1+(2+(3+..(10+0)))) e21 = foldr1 (+) list --> 55 (1+(2+(3+..(10)))) e22 = scanr (+) 0 list --> [55,54,52,49,45,40,34,27,19,10,0] e23 = scanr1 (+) list --> [55,54,52,49,45,40,34,27,19,10] e24 = iterate (cos) 4 --> [4,cos(4),cos(cos(4)),..] e25 = repeat 1 --> [1,1,1,..] e26 = replicate 4 'a' --> "aaaa" e27 = cycle [1,2,3] --> [1,2,3,1,2,3,..] e28 = take 4 list --> [1,2,3,4] e29 = drop 4 list --> [5,6,7,8,9,10] e30 = splitAt 4 list --> ([1,2,3,4],[5,6,7,8,9,10]) e31 = takeWhile (<4) list --> [1,2,3] e32 = dropWhile (<4) list --> [4,5,6,7,8,9,10] e33 = span (<4) list --> ([1,2,3],[4,5,6,7,8,9,10]) e34 = break (==7) list --> ([1,2,3,4,5,6],[7,8,9,10]) e35 = lines "A\nB\nC" --> ["A","B","C"] e36 = words "ab cd ef" --> ["ab","cd","ef"] e37 = unlines ["A","B","C"] --> "A\nB\nC" e38 = unwords ["ab","cd","ef"] --> "ab cd ef" e39 = reverse list --> [10,9..1] e40 = and [True,False] --> False e41 = or [True,False] --> True e42 = any (even) list --> True e43 = all (even) list --> False e44 = elem 4 list --> True e45 = notElem 11 list --> True e46 = lookup 2 [(1,2),(2,4)] --> Just 4 e47 = sum list --> 55 e48 = product list --> 3628800 e49 = maximum list --> 10 e50 = minimum list --> 1 --e51 = concatMap e52 = zip [1,2] [3,4] --> [(1,3),(2,4)] e53 = zip3 [1,2] [3,4] [5,6] --> [(1,3,5),(2,4,6)] e54 = unzip [(1,3),(2,4)] --> ([1,2],[3,4]) e55 = unzip3 [(1,3,5),(2,4,6)] --> ([1,2],[3,4],[5,6])