ruby
35 lines · 7 steps
Currying lambdas and methods in Ruby
Ruby's Proc#curry turns a multi-argument callable into one that accepts its arguments one at a time.
Explained by
highlit
1# Curry a multi-argument lambda so it can be applied one argument at a time.
2add = ->(a, b, c) { a + b + c }
3
4# Proc#curry returns a curried version that collects arguments incrementally.
5curried_add = add.curry
6
7step1 = curried_add[1] # => #<Proc> still waiting for two more args
8step2 = step1[2] # => #<Proc> waiting for one more arg
9result = step2[3] # => 6, fully applied
10
11# You can also supply several arguments at once.
12curried_add[1][2, 3] # => 6
13curried_add[1, 2, 3] # => 6
14
15# Methods become curryable by first converting them to a Proc.
16class Discounter
17 def apply(rate, base_price)
18 base_price - (base_price * rate)
19 end
20end
21
22discounter = Discounter.new
23discount = discounter.method(:apply).to_proc.curry
24
25# Build specialized functions by partially applying the first argument.
26ten_percent_off = discount[0.10]
27half_off = discount[0.50]
28
29ten_percent_off[100] # => 90.0
30half_off[100] # => 50.0
31
32# curry(arity) lets you fix how many arguments to expect for variadic procs.
33join = ->(*parts) { parts.join('/') }
34path = join.curry(3)
35full = path['usr']['local']['bin'] # => "usr/local/bin"
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Currying lets you fix some arguments now and supply the rest later, building specialized functions from general ones.
- 2A curried proc returns either another proc (when under-applied) or the final result (when fully applied), so you can chain calls flexibly.
- 3Methods aren't procs, but method(:name).to_proc bridges the gap so any method can be curried too.
Related explainers
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
ruby
module DurationFormatter UNITS = [ ['week', 604_800], ['day', 86_400],
Turning seconds into human-readable durations in Ruby
greedy-decomposition
modular-arithmetic
formatting
Intermediate
7 steps
ruby
class Comment < ApplicationRecord belongs_to :post belongs_to :author, class_name: "User"
Live-updating comments with Turbo in Rails
turbo-streams
callbacks
associations
Intermediate
8 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
rust
use std::collections::HashMap; pub struct Memoizer<K, V, F> { cache: HashMap<K, V>,
A generic memoizer in Rust
memoization
generics
caching
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/currying-lambdas-and-methods-in-ruby-explained-ruby-315f/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.