Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created October 19, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baweaver/1171a99aa272f4495bfb4862f22bc811 to your computer and use it in GitHub Desktop.
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...
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