Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active June 19, 2018 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/09fe980ee58046db12c7616dac116299 to your computer and use it in GitHub Desktop.
Save havenwood/09fe980ee58046db12c7616dac116299 to your computer and use it in GitHub Desktop.
Help fill in the missing two missing functions TODO! https://www.partiallyapplied.com/blog/church/
module Church
module_function
def encode number
-> function do
-> value do
number.times.reduce value do |accumulator|
function.call accumulator
end
end
end
end
def decode function
function.(-> n { n + 1 }).(0)
end
end
include Church
decode encode 0
#=> 0
decode encode 42
#=> 42
incrementer = -> n { n + 1 }
encode(3).(incrementer).(1)
#=> 4
increment = -> function { -> value { "???" } } # TODO
decode increment.(encode(3))
#=> 4
oneself = -> function { -> value { function.(value) } }
decode oneself.(encode(3))
#=> 3
square = -> function { -> value { function.(function.(value)) } }
decode square.(encode(3))
#=> 9
first = -> x { x[0] }
encode(1).(first).(%i[foo bar])
#=> :foo
encode(2).(first).(%i[foo bar])
#=> "f"
multiply = -> f1, f2 { -> value { f1.(f2.(value)) } }
decode multiply.(encode(3), encode(4))
#=> 12
add = -> f1, f2 { -> value { "???" } } # TODO
decode add.(encode(3), encode(4))
#=> 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment