Skip to content

Instantly share code, notes, and snippets.

@cheecheeo
Last active August 29, 2015 14:02
Show Gist options
  • Save cheecheeo/7e7f46a797c7aabbe0e0 to your computer and use it in GitHub Desktop.
Save cheecheeo/7e7f46a797c7aabbe0e0 to your computer and use it in GitHub Desktop.
Maybe in Ruby
nothing = lambda {|default, junk| default}
just = lambda {|x| lambda{|junk, f| f[x]}}
maybe = lambda {|default, f, m| m[default, f]}
show_maybe = lambda {|m| maybe['nothing', lambda {|x| "just[#{x}]"}, m]}
plus_2 = lambda {|x| maybe[nothing, lambda {|x| just[x + 2]}, x]}
# >>> x = nothing
# >>> y = just[5]
# >>> maybe[false, lambda {|x| true}, y]
# true
# >>> maybe[false, lambda {|x| true}, x]
# false
# >>> show_maybe[plus_2[nothing]]
# "nothing"
# show_maybe[plus_2[just[7]]]
# "just[9]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment