Skip to content

Instantly share code, notes, and snippets.

@devpuppy
Last active November 19, 2019 21:50
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 devpuppy/d459cf657bb253c18521a71b787416f9 to your computer and use it in GitHub Desktop.
Save devpuppy/d459cf657bb253c18521a71b787416f9 to your computer and use it in GitHub Desktop.
RubyConf Kwurry Lightning Talk
# currying
add = ->(a, b) {
a + b
}
add_two = add.curry.(2)
# => #<Proc:0x00007f911d0962c0 (lambda)>
add_two.(1)
# => 3
foo = ->(a, b, c=0, *xs) {
[a, b, c] + xs
}
foo.parameters
# => [[:req, :a], [:req, :b], [:opt, :c], [:rest, :xs]]
foo.curry.parameters
# => [[:rest]]
bar = ->(a:, b:, c: 0, **xs) {
[a, b, c] + xs.values
}
bar.parameters
# => [[:keyreq, :a], [:keyreq, :b], [:key, :c], [:keyrest, :xs]]
bar.curry.parameters
# => [[:rest]]
foo.curry.(1)
# => #<Proc:0x00007fc5960c7890 (lambda)>
foo.curry.(1).(2)
# => [1, 2, 0]
foo.curry.(1).(2,3)
# => [1, 2, 3]
bar.curry.(a: 1)
# ArgumentError (missing keyword: b)
bar.curry.(a: 1, b: 2)
# => [1, 2, 0]
bar.curry.(a: 1, b: 2, x: 11, y: 12)
# => [1, 2, 0, 11, 12]
require 'kwurry'
require 'kwurry/proc'
bar.kwurry.(b: 2).(a: 1)
# => [1, 2, 0]
greet = ->(greeting, name) {
"#{greeting}, #{name}"
}
greet.("Hello", "World")
# => "Hello, World"
greet_with_hey = greet.curry.("Hey")
greet_with_hey.("World")
# => "Hey, World"
greet = ->(greeting:, name:) {
"#{greeting}, #{name}"
}
greet_rubyconf = greet.kwurry.(name: "RubyConf")
greet_rubyconf.(greeting: "Well hello there")
# => "Well hello there, RubyConf"
numberize = ->(a, b, operator:) {
([a, b]).reduce(operator)
}
numberize.(3, 5, operator: :+)
# => 8
multiply = numberize.kwurry.(operator: :*)
# ArgumentError (wrong number of arguments (given 1, expected 2; required keyword: operator))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment