Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Last active December 30, 2016 13:48
Show Gist options
  • Save dmmfll/bfa6fe20cf9ccf072784387945c4695e to your computer and use it in GitHub Desktop.
Save dmmfll/bfa6fe20cf9ccf072784387945c4695e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# print an return, print is side effect
identity_print = lambda { |x|
puts x
x
}
word = lambda {
# return a random word of minimum length 3
(3..(7..20).to_a.sample).to_a.sample.times.map {
('a'..'z').to_a.sample }.join
}
_quit = lambda { # quit is a keyword in Ruby?!
# return the string 'quit'
'quit'
}
# make a list of random strings and insert 'quit' randomly
random_words = lambda {
stop = (5..20).to_a.sample
samples = (3..stop).to_a.sample.times.map {
word[]
}
# samples.compact
samples.insert (1..samples.length).to_a.sample, _quit[]
}
# return a random word, emulating user input
_input = lambda {
random_words[].sample
}
# recursion
echo_FP = lambda {
identity_print[_input[]] == _quit[] or echo_FP[]
}
echo_FP[]
# FP version of "echo()"
def identity_print(x): # "identity with side-effect"
print(x)
return x
echo_FP = lambda: identity_print(input("FP -- "))=='quit' or echo_FP()
echo_FP()
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment