Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.