Jump to content

User:Ruud Koot/First-class function

From Wikipedia, the free encyclopedia

Haskell[edit]

module Main where

f :: [[Integer] -> [Integer]]
f = let a = 1
        b = 3
     in [map (\x -> a * x + b), map (\x -> b * x + a)]

main = print (map ($ [1, 2, 3, 4, 5]) f)

Python[edit]

from functools import partial

def f():
    a = 1
    b = 3
    return [ partial(map, lambda x: a * x + b)
           , partial(map, lambda x: b * x + a) ]
    
print map(lambda g: g([1, 2, 3, 4, 5]), f())

Ruby[edit]

def f
    a = 1
    b = 3
    [ proc {|xs| xs.map {|x| a * x + b}} \
    , proc {|xs| xs.map {|x| b * x + a}} ]
end

puts f.map {|g| g.call [1, 2, 3, 4, 5]}