Skip to content

Instantly share code, notes, and snippets.

@laser
Last active January 4, 2016 04:19
Show Gist options
  • Save laser/8567691 to your computer and use it in GitHub Desktop.
Save laser/8567691 to your computer and use it in GitHub Desktop.
Pure Functions
# lambda is impure: mutates shared state (the fruits hash)
fruits = {}
hashify = lambda do |fruit|
fruits[fruit] = fruit
end
["apple", "pear"].each &hashify
# lambda is pure: operates only on its arguments
to_nv_pair = lambda do |fruit|
[fruit, fruit]
end
fruits = Hash[["apple", "pear"].map &to_nv_pair]
# lambda is impure: writes to STDOUT as side-effect
to_nv_pair = lambda do |fruit|
puts "building a pair..."
[fruit, fruit]
end
fruits = Hash[["apple", "pear"].map &to_nv_pair]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment