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
Fun with Enumerator
Search
cyclotron3k
August 14, 2018
Programming
1
13
Fun with Enumerator
Fun with Ruby's Enumerator class
cyclotron3k
August 14, 2018
Tweet
Share
More Decks by cyclotron3k
See All by cyclotron3k
Managing concurrent workloads in Ruby
cyclotron3k
0
26
Other Decks in Programming
See All in Programming
データアナリストが行うDatabricksを活用したETLの自動化事例
shinoa
0
270
Compose-View Interop in Practice (mDevCamp 2024)
stewemetal
0
130
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
140
try! Swift Tokyo 2024 参加報告 / try! Swift Tokyo 2024 Report
hironytic
0
200
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
160
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
4.1k
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
1
910
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
Hanami and htmx
bkuhlmann
0
210
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
Changed Rules: Architectures with Lightweight Stores
manfredsteyer
PRO
0
240
コーンフレークから始める モデリング会話入門
ogurotakayuki
0
370
Featured
See All Featured
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
Adopting Sorbet at Scale
ufuk
68
8.6k
Teambox: Starting and Learning
jrom
128
8.4k
Design by the Numbers
sachag
274
18k
Done Done
chrislema
178
15k
Automating Front-end Workflow
addyosmani
1356
200k
Designing Experiences People Love
moore
136
23k
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Building an army of robots
kneath
300
41k
The Cult of Friendly URLs
andyhume
74
5.7k
RailsConf 2023
tenderlove
4
540
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
Transcript
Fun* with Enumerator (*may not be fun)
Enumerator != Enumerable [1, 2, 3].each => #<Enumerator: [1, 2,
3]:each> [1, 2, 3].map => #<Enumerator: [1, 2, 3]:map> [1, 2, 3].select => #<Enumerator: [1, 2, 3]:select>
It gets weird enum = [1, 2, 3].map => #<Enumerator:
[1, 2, 3]:map> enum.each { |n| "<#{n}>" } => ["<1>", "<2>", "<3>"]
None
We can make our own Enumerators enum = Enumerator.new do
|yielder| yielder.yield 1 yielder.yield 2 end enum.to_a => [1, 2]
enum = Enumerator.new do |yielder| puts "Beginning" yielder.yield 1 puts
"Middle" yielder.yield 2 puts "End" end enum.next Beginning => 1 enum.next Middle => 2 enum.next End StopIteration: iteration reached an end
Infinite sequences! fib = Enumerator.new do |yielder| a = 0
b = 1 loop do yielder.yield b tmp = a a = b b = tmp + b end end fib.first 10 => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Extending Enumerable module Enumerable def my_with_index Enumerator.new do |yielder| i
= 0 self.each do |e| yielder.yield e, i i += 1 end end end end [:foo, :bar, :baz].my_with_index do |e, i| puts "#{i}: #{e}" end.to_a => [[:foo, 0], [:bar, 1], [:baz, 2]]
Miscellany • Use .lazy when working with infinite series •
Useful for managing expensive operations • Does not implement a cache (See CachingEnumerator gem)
Questions?