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
月単位でイテレーションする
Search
osyo
June 03, 2021
Programming
380
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
月単位でイテレーションする
osyo
June 03, 2021
More Decks by osyo
See All by osyo
5分で話せる Ruby 3.1
osyo
0
200
AST を使って ActiveRecord の where の条件式をブロックで記述しよう
osyo
2
1.3k
Vim の開発環境自慢
osyo
5
3.1k
Use Macro all the time ~ マクロを使いまくろ ~ 感想戦
osyo
0
340
Use Macro all the time ~ マクロを使いまくろ ~ (English)
osyo
3
460
Use Macro all the time ~ マクロを使いまくろ ~ (日本語)
osyo
0
2.4k
Ruby 3.0 で変わった private と attr_xxx
osyo
1
820
Ruby 2.0 から Ruby 3.0 を駆け足で振り返る
osyo
0
2.5k
12月25日にリリースされる Ruby 3.0 に備えよう!
osyo
1
3.5k
Other Decks in Programming
See All in Programming
yield再入門 #phpcon
o0h
PRO
0
880
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
0
120
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
1
110
信頼性について考えてみる(SRE NEXT 2026 miniLT)
hayama17
0
240
メールのエイリアス機能を履き違えない
isshinfunada
0
210
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
9
4.5k
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
200
Android CLI
fornewid
0
200
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
340
What's New in Android 2026
veronikapj
0
240
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
630
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.8k
Featured
See All Featured
Building Applications with DynamoDB
mza
96
7.2k
Writing Fast Ruby
sferik
630
63k
Music & Morning Musume
bryan
47
7.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
The SEO Collaboration Effect
kristinabergwall1
1
510
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Git: the NoSQL Database
bkeepers
PRO
432
67k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
240
Building Adaptive Systems
keathley
44
3.2k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.6k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
330
How to Ace a Technical Interview
jacobian
281
24k
Transcript
Omotesando.rb #63 Omotesando.rb #63 月単位でイテレーションする 月単位でイテレーションする
自己紹介 自己紹介 名前:osyo Twitter : github : ブログ : 書いたので気になる人は見
てね! @pink_bangbi osyo-manga Secret Garden(Instrumental) 趣味で気になった bugs.ruby のチケットをブログにまとめてる 令和時代の Ruby 基礎文法最速マスター
月単位でイテレーションする 月単位でイテレーションする
背景 背景 特定の Date ~ Date 間で日、月、年の日付を取得したかった 日、年の日付はシュッと取得できた 月はちょっと難しかった 今回はどうやって月単位でイテレーションする事ができたのかの思
考を追ってみる話
前提条件 前提条件 日付は1 日で固定 2020/04/01 みたに1 日で固定する 2020/04/14 みたいな日付は考慮しない 年をまたぐ可能性もあるよ
2020/04/01 ~ 2022/04/01 間 次の月は Date#next_month で取得できる require "date" puts Date.parse("2020/01/01").next_month # => 2020-02-01
Range#step でできないか考えてみる Range#step でできないか考えてみる Date は Range として扱うことができる で特定の日単位でイテレーションできる ただし、月の日数は一定ではないのでこれで対応するのは難しい
Range#step require "date" first = Date.parse("2020/01/01") last = Date.parse("2020/01/04") p (first..last).map(&:to_s) # => ["2020-01-01", "2020-01-02", "2020-01-03", "2020-01-04"] # 10 日ごとにイテレーションする first = Date.parse("2020/01/01") last = Date.parse("2020/02/01") p (first..last).step(10).map(&:to_s) # => ["2020-01-01", "2020-01-11", "2020-01-21", "2020-01-31"]
Enumerator.new で自前でイテレーションする Enumerator.new で自前でイテレーションする で任意のイテレーションを作る事ができる これを利用して月ごとにイテレーションする Enumerator.new enum = Enumerator.new
{ |y| # y << を呼ぶたびにイテレーションのブロックを呼び出す # これは 1 〜3 をブロックに渡すような例 y << 1 y << 2 y << 3 } enum.each { |it| p it } # => 1 # 2 # 3
いけるやん!!! いけるやん!!! require "date" first = Date.parse("2020/01/01") last = Date.parse("2020/04/01")
# 開始の月から終了の月までをイテレーションする enum = Enumerator.new { |y| date = first while date <= last y << date date = date.next_month end } p enum.map(&:to_s) # => ["2020-01-01", "2020-02-01", "2020-03-01", "2020-04-01"]
Enumerator.produce を使う Enumerator.produce を使う Enumerator.new を使うと実現できるがやや冗長 そこで という便利メソッドを使う これは与えられたブロックを呼び出し続ける Enumerator
を返す Enumerator.produce # 0, 2, 4, 6, 8... と無限に続ける Enumerator を定義 nums = Enumerator.produce(0) { |it| it + 2 } # take を使って先頭から4 つの要素を取り出す pp nums.take(4) # => [0, 2, 4, 6] # ランダムな要素を返す Enumerator を定義 rand = Enumerator.produce { rand(10) } # 10 個のランダムな要素を取り出す pp rand.take(10) # => [1, 1, 6, 3, 4, 0, 1, 9, 1, 1]
これを利用して月単位のイテレーションも定義する require "date" first = Date.parse("2020/01/01") last = Date.parse("2020/04/01") #
これで開始日から次の月まで永遠とイテレーションする Enumerator を定義する # これだけだと無限ループになる enum = Enumerator.produce(first) { |it| it.next_month } # 試しに先頭5 月分を取得する puts enum.take(5) # => 2020-01-01 # 2020-02-01 # 2020-03-01 # 2020-04-01 # 2020-05-01
で終了条件を指定する で終了条件を指定する Enumerator.produce だと無限ループになる を使うことで特定の条件までの要素を取得 完成!!! 完成!!! Enumerable#take_while Enumerable#take_while Enumerable#take_while
require "date" first = Date.parse("2020/01/01") last = Date.parse("2020/04/01") # take_while の条件になるまでの値を取得する result = Enumerator.produce(first, &:next_month).take_while { |date| date <= last } puts result # => 2020-01-01 # 2020-02-01 # 2020-03-01 # 2020-04-01
まとめ まとめ Enumerator.produce べんり
ご清聴 ご清聴 ありがとうございました ありがとうございました