Created
April 27, 2014 18:31
-
-
Save dlisboa/11352380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'matchrb/global' | |
divisible_by = lambda {|number, value| value % number == 0}.curry | |
fizzbuzz = lambda do |i| | |
match i, | |
divisible_by[15] => "Fizz Buzz", | |
divisible_by[5] => "Buzz", | |
divisible_by[3] => "Fizz", | |
otherwise => i | |
end | |
cons = lambda { |x, y| | |
lambda { |m| | |
m[ x, y ] | |
} | |
} | |
car = lambda { |z| | |
z[ lambda { |p, q| p } ] | |
} | |
cdr = lambda { |z| | |
z[ lambda { |p, q| q } ] | |
} | |
null = lambda { |list| | |
list == nil | |
} | |
map = lambda { |fn, items| | |
if null[ items ] | |
nil | |
else | |
cons[ fn[ car[ items ] ], | |
map[ fn, cdr[ items ] ] | |
] | |
end | |
} | |
range = lambda { |start, finish| | |
if start == finish | |
cons[finish, nil] | |
else | |
cons[start, range[start + 1, finish]] | |
end | |
} | |
print_list = lambda { |list| | |
print = lambda { |el| puts el } | |
map[print, list] | |
} | |
print_list[ map[ fizzbuzz, range[ 1, 100 ] ] ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment