Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Enumerator::Lazy
Search
Erik Berlin
August 02, 2016
Programming
630
2
Share
Enumerator::Lazy
Presented at SF.rb on August 2, 2016.
Erik Berlin
August 02, 2016
More Decks by Erik Berlin
See All by Erik Berlin
Ruby Trivia 3
sferik
0
750
The Value of Being Lazy
sferik
3
850
Ruby Trivia 2
sferik
0
810
Ruby Trivia
sferik
2
1.4k
💀 Symbols
sferik
5
2k
Content Negotiation for REST APIs
sferik
8
1.1k
Writing Fast Ruby
sferik
630
63k
Mutation Testing with Mutant
sferik
5
1.1k
Other Decks in Programming
See All in Programming
Agentic AI: Evolution oder Revolution
mobilelarson
PRO
0
220
Codex CLIのSubagentsによる並列API実装 / Parallel API Implementation with Codex CLI Subagents
takatty
2
730
20260315 AWSなんもわからん🥲
chiilog
2
180
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
3
1.2k
RailsのValidatesをSwift Macrosで再現してみた
hokuron
0
140
PHPのバージョンアップ時にも役立ったAST(2026年版)
matsuo_atsushi
0
270
ローカルで稼働するAI エージェントを超えて / beyond-local-ai-agents
gawa
1
190
Geminiをパートナーに神社DXシステムを個人開発した話(いなめぐDX 開発振り返り)
fujiba
0
130
KagglerがMixSeekを触ってみた
morim
0
360
Ruby and LLM Ecosystem 2nd
koic
1
1.4k
ファインチューニングせずメインコンペを解く方法
pokutuna
0
220
GoのDB アクセスにおける 「型安全」と「柔軟性」の両立 - Bob という選択肢
tak848
0
290
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
150
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.5k
Technical Leadership for Architectural Decision Making
baasie
3
300
Un-Boring Meetings
codingconduct
0
240
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
330
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.6k
Bash Introduction
62gerente
615
210k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
460
The World Runs on Bad Software
bkeepers
PRO
72
12k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
330
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
920
Transcript
Enumerator::Lazy Erik Michaels-Ober @sferik
Imperative languages do iteration like this: int sum = 0;
for(i = 1; i < 10; i = i + 1) { sum = sum + i; }
Functional languages do iteration like this: rec_sum [] = 0
rec_sum (x:xs) = x + rec_sum xs rec_sum [1..9]
Object oriented languages (should) do iteration like this: sum =
0 (1..9).each do |i| sum += i end
Object oriented languages (should) do iteration like this: sum =
(1..9).inject(&:+)
Iterators Introduced in CLU by Barbara Liskov (1975) Copied in
Ruby by Yukihiro Matsumoto (1995)
Ruby’s iterator is called Enumerator
enum = Enumerator.new do |yielder| yielder.yield("sf") yielder.yield("dot") yielder.yield("rb") end
["sf", "dot", "rb"].each ["sf", "dot", "rb"].to_enum Enumerator.new(["sf", "dot", "rb"])
enum = Enumerator.new do |yielder| n = 0 loop do
yielder.yield(n) n += 1 end end
fib = Enumerator.new do |yielder| a = b = 1
loop do yielder.yield(a) a, b = b, a + b end end
module Enumerable def lazy_map(&block) Enumerator.new do |yielder| return to_enum(__method__) unless
block_given? each do |n| yielder.yield(block.call(n)) end end end end
module Enumerable def lazy_select(&block) Enumerator.new do |yielder| return to_enum(__method__) unless
block_given? each do |n| yielder.yield(n) if block.call(n) end end end end
Ruby 2.0 introduced Enumerator::Lazy
What are the first five even perfect squares over a
thousand?
lazy_integers = (1..Float::INFINITY).lazy lazy_integers.collect { |x| x ** 2 }.
select { |x| x.even? }. reject { |x| x < 1000 }. first(5) #=> [1024, 1156, 1296, 1444, 1600]
What are the first five twin primes?
require "prime" lazy_primes = Prime.lazy lazy_primes.select { |x| (x -
2).prime? }. collect { |x| [x - 2, x] }. first(5) #=> [[3, 5], [5, 7], [11, 13], [17, 19], [29, 31]]
module Enumerable def repeat_after_first return to_enum(__method__) unless block_given? each.with_index do
|*val, index| index.zero? ? yield(*val) : 2.times { yield(*val) } end end end
require "prime" lazy_primes = Prime.lazy lazy_primes.repeat_after_first. each_slice(2). select { |x,
y| x + 2 == y }. first(5) #=> [[3, 5], [5, 7], [11, 13], [17, 19], [29, 31]]
When are the next five Friday the 13ths?
require "date" lazy_dates = (Date.today..Date.new(9999)).lazy lazy_dates.select { |d| d.day ==
13 }. select { |d| d.friday? }. first(10)
Detect whether a text file contains a string? (without reading
the entire file into memory)
lazy_file = File.readlines("/path/to/file").lazy lazy_file.detect { |x| x =~ /regexp/ }
Being lazy is efficient.
Being lazy is elegant.
None
Thank you