Last active
January 4, 2016 04:19
-
-
Save laser/8567691 to your computer and use it in GitHub Desktop.
Pure Functions
This file contains hidden or 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
# 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