Created
May 24, 2019 00:30
-
-
Save baweaver/003d3c33b10ccb65588f223a1d1ee354 to your computer and use it in GitHub Desktop.
This file contains 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
class Proc | |
def contramap(&fn) self << fn end | |
end | |
adds = -> a, b { a + b }.curry | |
[1, 2, 3].map(&adds[2]) | |
# => [3, 4, 5] | |
# What if the type on the left doesn't work with `adds`? | |
%w(1 2 3).map(&adds[2]) | |
# TypeError: String can't be coerced into Integer | |
# ...well we can always transform them first: | |
%w(1 2 3).map(&:to_i).map(&adds[2]) | |
# => [3, 4, 5] | |
# ...or apply a transformation to the input before it | |
# gets to the function with contramap: | |
%w(1 2 3).map(&adds[2].contramap(&:to_i)) | |
# => [3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment