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
1
440
Enumerator::Lazy
Presented at SF.rb on August 2, 2016.
Erik Berlin
August 02, 2016
Tweet
Share
More Decks by Erik Berlin
See All by Erik Berlin
Ruby Trivia 3
sferik
0
590
The Value of Being Lazy
sferik
3
640
Ruby Trivia 2
sferik
0
650
Ruby Trivia
sferik
2
1.2k
💀 Symbols
sferik
5
1.7k
Content Negotiation for REST APIs
sferik
8
870
Writing Fast Ruby
sferik
623
60k
Mutation Testing with Mutant
sferik
5
1k
Other Decks in Programming
See All in Programming
I/O Extended Android in Korea 2024 ~ Whats new in Android development tools
pluu
0
250
AHC035解説
terryu16
0
710
「2024年版 Kotlin サーバーサイドプログラミング実践開発」の補講 〜O/Rマッパー編〜
n_takehata
2
260
Composing an API the *right* way (Droidcon Berlin 2024)
zsmb
1
450
リハビリmruby
kishima
1
160
生成AIをkintoneに連携してみた
hideg
0
230
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
Exploring the Gradually Lost Technical Skills in the Cloud Native Era
hwchiu
2
3.9k
入社1ヶ月でここまでやった!Findy Toolsインフラ支援の最適化
rvirus0817
6
1.4k
SDCon2024: Enabling DevOps and Team Topologies thru architecture: architecting for fast flow
cer
PRO
0
780
Introduction to GitOps
hwchiu
0
110
日付と正規化
megmogmog1965
0
140
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
262
13k
What's in a price? How to price your products and services
michaelherold
239
11k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
44
4.7k
Robots, Beer and Maslow
schacon
PRO
157
8.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
Learning to Love Humans: Emotional Interface Design
aarron
269
39k
Thoughts on Productivity
jonyablonski
64
4.1k
Build your cross-platform service in a week with App Engine
jlugia
227
17k
WebSockets: Embracing the real-time Web
robhawkes
59
7.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
245
1.2M
Rails Girls Zürich Keynote
gr2m
93
13k
Designing with Data
zakiwarfel
96
5k
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