Created
October 19, 2019 07:09
-
-
Save baweaver/1171a99aa272f4495bfb4862f22bc811 to your computer and use it in GitHub Desktop.
Stealing some vague ideas from Applicatives in Haskell, probably wrongly but it amuses me so...
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 Symbol | |
def ap(*applied_args) | |
-> *block_args { | |
if applied_args.empty? | |
receiver, *new_args = block_args[0] | |
receiver.send(self, *new_args) | |
else | |
self.to_proc.call(*block_args, *applied_args) | |
end | |
} | |
end | |
end | |
(1..3).map(&:+.ap(5)) | |
=> [6, 7, 8] | |
[1,2,3].zip([2, 3, 4]).map(&:+.ap) | |
=> [3, 5, 7] | |
{ 'a' => 'b', 'b' => 'a' }.map(&:join.ap(' : ')) | |
=> ["a : b", "b : a"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment