ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Array, Hash, Range တွေက ဘာကြောင့် map, select, sort, min လို method တွေ အတူတူ ရှိလဲဆိုတော့ သူတို့အားလုံးက Enumerable ဆိုတဲ့ method အစုအဝေးကို ယူထားလို့ပါ။ Enumerable က class ဆီက တောင်းတာ တစ်ခုတည်းပါ၊ element တစ်ခုချင်းစီကို တစ်ခုပြီးတစ်ခု ပေးမယ့် each method ပါ။ အဲဒီ each ကို ရေးပေးလိုက်တာနဲ့ ကျန်တဲ့ method တွေ အားလုံးကို ကိုယ့် class က ရသွားပါတယ်။
Enumerator ကတော့ 'ဒီ method ကို သုံးပြီး ဒီအရာကို လျှောက်ကြည့်ပါ' ဆိုတာကို ကိုယ်စားပြုတဲ့ object ပါ။ Block မပါဘဲ iterator တစ်ခုကို ခေါ်ရင် ရလာတာပါ။ Enumerator ကို နောက်ထပ် item တစ်ခုကို လိုအပ်မှ တောင်းယူလို့ ရတာမို့ အဆုံးမရှိ စာရင်းတွေကိုတောင် ဘေးကင်းစွာ ကိုင်တွယ်နိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: ကိုယ့် class က collection မဟုတ်သေးဘူး
သီချင်းတွေကို array ထဲ သိမ်းထားတဲ့ Playlist class တစ်ခု ရေးထားတယ်ဆိုပါစို့။ Playlist ပေါ်မှာ map, select, count ခေါ်ချင်ပေမယ့် Ruby က object တစ်ခုက collection ကို ကိုင်ထားရုံနဲ့ collection အဖြစ် မမြင်ပါဘူး။ ဒါကြောင့် ကိုယ်တိုင် count, select, sort ကို တစ်ခုချင်းစီ ရေးရမလား၊ ဒါမှမဟုတ် အတွင်းက array ကို အပြင်ကို ပေးလိုက်ရမလားဆိုပြီး ရွေးစရာ နှစ်ခုပဲ ကျန်တာမျိုး ကြုံရပါတယ်။ ဒီ code က map ကို ခေါ်ကြည့်ပါတယ်။
class Playlist
def initialize(*songs)
@songs = songs
end
end
playlist = Playlist.new("Alpha", "Delta Wave")
playlist.map(&:upcase)
# NoMethodError: undefined method 'map' for an instance of PlaylistMethod တစ်ခုက ၅၀ ကျော် ဘာကြောင့် ပေးနိုင်လဲ
map, select, sort စတာတွေအားလုံးက 'element တွေကို တစ်ခုပြီးတစ်ခု ပေးပါ' ဆိုတဲ့ တစ်ချက်တည်းကို အားကိုးပြီး ရေးထားတာပါ။ ဒါကြောင့် Ruby က ဒီ method တွေကို Enumerable module ထဲ တစ်ကြိမ်တည်း ရေးထားပြီး class တိုင်းက each ပေးရုံနဲ့ ရယူစေတာပါ။ Playlist မှာ each ရေးပြီး include Enumerable ထည့်လိုက်ရင် ပြဿနာ ပျောက်ပါတယ်။
Core concept: each ကို ရေး၊ ကျန်တာ ရယူ
Enumerable က include နဲ့ class ထဲ ရောနှောထည့်တဲ့ method အစုအဝေး (module) တစ်ခုပါ။ Class ဆီက တောင်းတာ တစ်ခုတည်းပါ၊ element တိုင်းကို yield လုပ်ပေးတဲ့ each method ပါ။ တခြား Enumerable method အားလုံး (map, select, reduce, sort, min, max, first, include?, each_slice, group_by အပါအဝင် ဆယ်ဂဏန်းများစွာ) ကို ကိုယ့် each ပေါ်မှာ အခြေခံပြီး ရေးထားတာပါ။ Array, Hash, Range, Struct နဲ့ File အားလုံး ဒီလိုလုပ်ထားလို့ vocabulary အတူတူ ရှိတာပါ။
ကိုယ့် each က စည်းမျဉ်း နှစ်ခု လိုက်နာရပါမယ်။ element တစ်ခုချင်းစီကို block ဆီ yield လုပ်ရမယ်၊ ပြီးတော့ block မပေးရင် Enumerator ပြန်ပေးသင့်ပါတယ် (return to_enum(:each) unless block_given?)။ Enumerable ရဲ့ ကိုယ်ပိုင် method တွေက အမြဲ block ပေးပြီး ခေါ်လို့ ဒီ guard မပါလည်း အလုပ်လုပ်ပါတယ်၊ guard က playlist.each.with_index(1) ဒါမှမဟုတ် playlist.each.next လို block မပါဘဲ each ကို တိုက်ရိုက်ခေါ်တဲ့ caller တွေအတွက်ပါ။ guard မပါရင် အဲဒီ call တွေက LocalJumpError: no block given (yield) နဲ့ ကျိုးပါတယ်။
Enumerator ဆိုတာ 'ဒီ object ကို ဒီ method နဲ့ လျှောက်ပါ' ဆိုတာကို ကိုယ်စားပြုတဲ့ object ပါ။ [1, 2, 3].each, 5.times, 'abc'.each_char လို block မပါတဲ့ iterator ခေါ်တာတိုင်းနီးပါးက တစ်ခုပေးပါတယ်။ Enumerator တွေကိုယ်တိုင်က Enumerable ဖြစ်လို့ ဆက်ပြီး chain လုပ်လို့ရပါတယ်။ ပုံစံနှစ်မျိုးလည်း ရှိပါတယ်၊ အခုထိ သုံးခဲ့တဲ့ internal iteration မှာ method က element တွေကို ကိုယ့် block ထဲ တွန်းထည့်ပါတယ်။ External iteration ကတော့ ပြောင်းပြန်ပါ၊ next နဲ့ element တစ်ခုချင်းစီကို ကိုယ်တိုင် ဆွဲယူပြီး peek က မစားဘဲ နောက်တစ်ခုကို ကြည့်ပါတယ်။ Element ကုန်ရင် next က StopIteration ကို raise လုပ်ပြီး loop က အဲဒီ error ကို တိတ်တိတ်ဖမ်းလို့ loop { puts e.next } က enumerator ကို တစ်ခါပဲ လျှောက်ပါတယ်။
Enumerator.new ကတော့ yielder ထဲကို y << value နဲ့ တန်ဖိုးတွေ တွန်းထည့်တဲ့ block တစ်ခုရေးပြီး sequence တစ်ခုကို အစကနေ တည်ဆောက်ခွင့် ပေးပါတယ်။ Block က နောက်တန်ဖိုးကို တောင်းမှပဲ run လို့ အဆုံးမရှိ sequence ကို ဘေးကင်းစွာ ဖော်ပြနိုင်ပါတယ်။ lazy ထပ်ထည့်ရင် Enumerator::Lazy ရပြီး သူ့ map, select, reject တွေက ချက်ချင်း မ run ဘဲ pipeline တစ်ခုကိုပဲ တည်ဆောက်ပါတယ်။ first(n)၊ take(n).to_a ဒါမှမဟုတ် force လို method က တန်ဖိုးတွေကို ဆွဲထုတ်မှပဲ တွက်ချက်ပါတယ်။ ဒါကြောင့် (1..Float::INFINITY).lazy.select(&:even?).first(4) လို့ ရေးရင် ရပ်သွားမယ့်အစား ရလဒ်ရတာပါ။
- each ကို ရေး (element တိုင်း yield)
- Enumerable က each ပေါ်မှာ method တည်ဆောက်
- Enumerator က each ကို next/peek ပြောင်း
- lazy က အလုပ်ကို တောင်းမှ လုပ်
each တစ်ခုတည်းက စာချုပ်ပါ၊ ကျန်တာအားလုံးကို ငှားထားတာပါ။
အထွေထွေ ပုံစံ
class Collection
include Enumerable
def each
return to_enum(:each) unless block_given?
# yield every element, one at a time
end
end
sequence = Enumerator.new { |y| y << value }include Enumerable က method အစုအဝေးကို ရောနှောထည့်ပြီး each က ဒီ class ရဲ့ element တွေကို ဘယ်လို လျှောက်ရမလဲ ပြောပါတယ်။ guard line က block မပါရင် Enumerator ပြန်ပေးပါတယ်။ နောက်ဆုံး line က class မလိုဘဲ Enumerator.new နဲ့ sequence တစ်ခု ဖန်တီးတဲ့ပုံစံပါ၊ y << value က နောက်တန်ဖိုးကို ပေးလိုက်တာပါ။
Example: collection ဖြစ်လာတဲ့ Playlist
ဒီ Playlist class က each တစ်ခုတည်း ရေးထားပြီး include Enumerable ထည့်ထားပါတယ်။ ပြီးတော့ playlist ပေါ်မှာ sort, map, select, min_by စတဲ့ method တွေကို သူ့ကိုယ်ပိုင်အနေနဲ့ ခေါ်လို့ ရသွားတာကို ကြည့်ပါ။ initialize ကို object တည်ဆောက်ချိန်မှာ songs ကို သိမ်းဖို့ သုံးထားပါတယ်၊ အဲဒါကို Initialize lesson မှာ အသေးစိတ် လေ့လာပါမယ်။
Playlist ထဲမှာ song ဘယ်လောက်ရှိလဲ၊ ဘယ်လို sort လုပ်မလဲဆိုတာကို class က တစ်ကြောင်းမှ မရေးထားပါဘူး။ ဒါတွေ အားလုံးက each ကနေ လာတာပါ။
class Playlist
include Enumerable
def initialize(*songs)
@songs = songs
end
def each
return to_enum(:each) unless block_given?
@songs.each { |song| yield song }
self
end
end
playlist = Playlist.new("Blue Hour", "Alpha", "Delta Wave", "Charlie")
puts playlist.count
p playlist.sort
p playlist.map(&:length)
p playlist.select { |song| song.length > 6 }
puts playlist.min_by(&:length)
p playlist.first(2)
puts playlist.include?("Alpha")
p playlist.each_slice(2).to_a
p playlist.each_with_index.map { |song, i| "#{i + 1}. #{song}" }Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- include
include Enumerableဒီ line တစ်ကြောင်းက Enumerable ရဲ့ method တွေကို Playlist ထဲ ရောနှောထည့်ပါတယ်။ ဒါပေမယ့် each မရှိသေးရင် ဘာမှ အလုပ်မလုပ်သေးပါဘူး၊ each ကို ခေါ်ရမယ့်သူပဲ ဖြစ်လို့ပါ။
- guard
return to_enum(:each) unless block_given?Block မပါဘဲ playlist.each လို့ ခေါ်ရင် Enumerator ပြန်ပေးပါတယ်၊ ဒါကြောင့် playlist.each.next ဒါမှမဟုတ် playlist.each.with_index(1) ကို သုံးလို့ ရပါတယ်။
- yield
@songs.each { |song| yield song }အရေးအကြီးဆုံး line ပါ။ အတွင်းက array ကို လျှောက်ပြီး song တစ်ခုချင်းစီကို Playlist ကို ခေါ်ထားတဲ့ block ဆီ yield လုပ်ပေးပါတယ်။ ဒီအချက်ကြောင့်ပဲ map, select, sort တို့ အလုပ်လုပ်တာပါ။
- sort
p playlist.sortEnumerable က each နဲ့ element တွေကို စုပြီး String ရဲ့ <=> နဲ့ နှိုင်းယှဉ်ကာ စီပေးပါတယ်၊ ဒါကြောင့် ရလဒ်က alphabet အစဉ် ဖြစ်ပါတယ်။ ဒီမှာ sort က Array အသစ်ကို ပြန်ပေးပါတယ် (Playlist မဟုတ်ပါ)။
- chain
playlist.each_with_index.map { |song, i| "#{i + 1}. #{song}" }each_with_index ကို block မပါဘဲ ခေါ်လို့ Enumerator ရပြီး map က အဲဒီ [song, index] pair တွေကို ဆက်လက် ပြောင်းပါတယ်။ Enumerator တွေက Enumerable ဖြစ်လို့ chain လုပ်လို့ ရတာပါ။
4
["Alpha", "Blue Hour", "Charlie", "Delta Wave"]
[9, 5, 10, 7]
["Blue Hour", "Delta Wave", "Charlie"]
Alpha
["Blue Hour", "Alpha"]
true
[["Blue Hour", "Alpha"], ["Delta Wave", "Charlie"]]
["1. Blue Hour", "2. Alpha", "3. Delta Wave", "4. Charlie"]External enumerator: next, peek နဲ့ Enumerator.new
next နဲ့ element တစ်ခုချင်းစီကို ကိုယ်တိုင် ဆွဲယူပါ။ peek က မစားဘဲ နောက်တစ်ခုကို ကြည့်ပြီး rewind က အစကို ပြန်ပို့ပါတယ်။ Element ကုန်သွားပြီးနောက် next ခေါ်ရင် StopIteration: iteration reached an end ဆိုတဲ့ error ကို raise လုပ်ပါတယ်၊ loop do ... end က အဲဒါကို တိတ်တိတ်ဖမ်းလို့ ရပ်သွားပါတယ်။ Enumerator.new ကတော့ yielder y ကို ပေးပြီး y << value နဲ့ တန်ဖိုး ထုတ်ပေးတဲ့ block ရေးခွင့်ရှိပါတယ်၊ အောက်က Fibonacci generator မှာ block ထဲက loop က အဆုံးမရှိ ပတ်ပေမယ့် တောင်းမှပဲ run လို့ ပြဿနာ မရှိပါဘူး။
e = [10, 20, 30].each
puts e.next # 10
puts e.peek # 20 (peek does not move forward)
puts e.next # 20
puts e.next # 30
begin
e.next
rescue StopIteration => err
puts "done: #{err.message}" # done: iteration reached an end
end
e.rewind
puts e.next # 10 again
letters = %w[a b c].each
loop { puts letters.next } # loop swallows StopIteration and ends quietly
puts "finished"
fib = Enumerator.new do |y|
a, b = 0, 1
loop do
y << a
a, b = b, a + b
end
end
p fib.take(8) # [0, 1, 1, 2, 3, 5, 8, 13]
p fib.lazy.select(&:even?).first(5) # [0, 2, 8, 34, 144]Lazy enumerator: တောင်းမှ တွက်တာ
ရိုးရိုး (eager) chain မှာ map က element အားလုံးကို အရင် လုပ်ပြီး ရလဒ်အစုအဝေးကို select ဆီ ပို့ပါတယ်။ Lazy chain မှာတော့ element တစ်ခုချင်းစီက map, select ကို တစ်ခုပြီးတစ်ခု ဖြတ်ပြီး first(1) လို အဆုံးသတ် method က လိုတာ ရတာနဲ့ ရပ်ပါတယ်။ အောက်က log မှာ အဲဒါကို တိုက်ရိုက် မြင်ရပါမယ်။ အဆုံးမရှိ range ကို lazy မပါဘဲ map လုပ်ရင်တော့ ဘယ်တော့မှ မပြီးပါဘူး။
Eager versus lazy work for first(1)
-----------------------------------
Eager map 1..6 -> [10,20,30,40,50,60]
select all 6 -> [30,40,50,60]
first(1) -> [30] 12 steps of work
Lazy 1 -> map -> select (10: no)
2 -> map -> select (20: no)
3 -> map -> select (30: yes) -> first(1) is satisfied, stop
6 steps of workevens_squared = (1..Float::INFINITY).lazy.select(&:even?).map { |n| n * n }
p evens_squared.first(4) # [4, 16, 36, 64]
p [1, 2, 3, 4, 5].lazy.select(&:odd?).map { |n| n * 10 }.first(2) # [10, 30]
log = []
result = (1..6).lazy
.map { |n| log << "map #{n}"; n * 10 }
.select { |n| log << "select #{n}"; n > 20 }
.first(1)
p result # [30]
puts log.join(", ") # map 1, select 10, map 2, select 20, map 3, select 30
puts log.size # 6 (an eager chain would log 12)ရလဒ်ကို ဆွဲထုတ်ဖို့ lazy ပေါ်မှာ first(n)၊ take(n).to_a ဒါမှမဟုတ် force ကို ခေါ်ရပါတယ်။ Lazy enumerator ကို တိုက်ရိုက် print ရင် #<Enumerator::Lazy: ...> လို့ ထွက်တာဖြစ်ပြီး တန်ဖိုးတွေ မဟုတ်ပါဘူး။ Lazy က element တစ်ခုစီအတွက် အပိုကုန်ကျစရိတ် အနည်းငယ် ရှိတာမို့ array သေးသေးလေးတွေအတွက်တော့ eager version က ပိုရိုးရှင်းပြီး မြန်လေ့ရှိပါတယ်။
Comparable: ကိုယ့် object တွေကို အစဉ်ပေးခြင်း
Enumerable ရဲ့ sort, min, max တို့က element တွေကို <=> နဲ့ နှိုင်းယှဉ်လို့ ရမှ အလုပ်လုပ်ပါတယ်။ ကိုယ့် class အတွက် <=> တစ်ခု ရေးပြီး include Comparable လုပ်လိုက်ရင် <, <=, ==, >, >=, between?, clamp အားလုံး ရပါတယ်။ <=> က နံပါတ် -1, 0, 1 ကို ပြန်ရမယ်၊ နှိုင်းယှဉ်လို့ မရတဲ့ object ဆိုရင် nil ပြန်ရမယ်၊ အဲဒီအခါ Comparable က ArgumentError ပစ်ပါတယ်။ String အဖြစ် version တွေကို sort လုပ်ရင် "1.10" က "1.2" ရှေ့ကို ရောက်တတ်လို့ (စာလုံးအလိုက် နှိုင်းယှဉ်လို့ပါ) အောက်ကလို major နဲ့ minor ကို နံပါတ်အဖြစ် နှိုင်းတာက ပိုမှန်ပါတယ်။
class Version
include Comparable
attr_reader :major, :minor
def initialize(major, minor)
@major = major
@minor = minor
end
def <=>(other)
return nil unless other.is_a?(Version)
[major, minor] <=> [other.major, other.minor]
end
def to_s
"#{major}.#{minor}"
end
end
versions = [Version.new(1, 10), Version.new(1, 2), Version.new(2, 0)]
puts versions.min # 1.2
puts versions.max # 2.0
puts versions.sort.join(", ") # 1.2, 1.10, 2.0
puts Version.new(1, 2) < Version.new(1, 10) # true
puts Version.new(1, 5).between?(Version.new(1, 2), Version.new(1, 10)) # true
puts Version.new(3, 0).clamp(Version.new(1, 0), Version.new(2, 0)) # 2.0
puts Version.new(1, 2) == Version.new(1, 2) # true: == is built from <=>
begin
Version.new(1, 2) < 5
rescue ArgumentError => e
puts e.message # comparison of Version with 5 failed
endattr_reader နဲ့ initialize ကို ဒီမှာ အရှိကိုက်သာ သုံးထားပါတယ်၊ နောက် lesson တွေမှာ အသေးစိတ် ရှင်းပါမယ်။ အရေးကြီးတာက <=> တစ်ခုက နှိုင်းယှဉ်တဲ့ operator ခုနစ်ခုလောက်ကို ပေးတာပါ။ == ကိုတောင် ဒီ <=> ကနေ ရတာပါ၊ ဒါပေမယ့် equal? က object တစ်ခုတည်းလားကို စစ်တာမို့ ဆက်ပြီး false ပေးပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Real project တွေမှာ တွေ့ရတဲ့ collection တွေက Enumerable ချည်းပါပဲ၊ Set, Struct, Range, Hash, CSV::Table, Dir နဲ့ File ရဲ့ စာကြောင်းတွေပါ။ OrderBook, Inventory, ShoppingCart လို ကိုယ်ပိုင် class က include Enumerable နဲ့ each ခြောက်ကြောင်းလောက်ရေးရုံနဲ့ တူညီတဲ့ စွမ်းအားကို ရပြီး caller တွေက cart.select(&:taxable?).sum(&:price) လို့ method နှစ်ခုလုံး ကိုယ်တိုင် မရေးရဘဲ ရေးလို့ ရပါတယ်။ version, ငွေပမာဏ, ရက်စွဲလို value တူတဲ့ object တွေမှာ Comparable ထပ်ထည့်ရင် sort, min, max, between? တွေ အလိုအလျောက် အလုပ်လုပ်ပါတယ်။
Source က ကြီးတာ ဒါမှမဟုတ် အဆုံးမရှိတာဆိုရင် lazy enumerator တွေက အရေးပါလာပါတယ်။ File.foreach('app.log').lazy.select { |line| line.include?('ERROR') }.first(20) က gigabyte ပေါင်းများစွာ ရှိတဲ့ log ကို လိုတဲ့ပမာဏပဲ ဖတ်ပါတယ်၊ Enumerator.new ကလည်း page ခွဲထားတဲ့ API call ဒါမှမဟုတ် database cursor ကို နောက် page ကို တောင်းမှပဲ ယူတဲ့ stream အဖြစ် ထုပ်ပိုးပေးနိုင်ပါတယ်။ အကုန်အကျကတော့ element တစ်ခုစီအတွက် overhead အနည်းငယ်ပါ၊ ဒါကြောင့် array သေးသေးတွေမှာ ရိုးရိုး eager version က ပိုရိုးရှင်းပြီး မြန်လေ့ရှိပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. each ထဲမှာ yield မလုပ်တာ
def each; @songs; end လို့ ရေးရင် array ကို ပြန်ပေးရုံပဲ ရှိပြီး block ကို ဘယ်တော့မှ မခေါ်ပါဘူး။ ဒါကြောင့် map က ဘာမှ မစုမိဘဲ error မပေးဘဲ [] ကို ပြန်ပေးပါတယ်၊ count က 0 ပြန်ပါတယ်။ ဒါက တိတ်တဆိတ် bug မျိုးပါ။ each က element တိုင်းကို yield လုပ်ရပါမယ်၊ အများအားဖြင့် @songs.each { |s| yield s } လို့ ပို့လွှတ်ရေးပါတယ်။ Enumerable ထည့်ပြီး each လုံးဝ မရေးရင်တော့ NoMethodError: undefined method 'each' for an instance of Playlist ပေါ်ပါတယ်။
2. Lazy chain ကို မဆွဲထုတ်ဘဲ print တာ
(1..Float::INFINITY).lazy.map { |n| n * 2 } ကို print ရင် #<Enumerator::Lazy: ...> လို့ ထွက်ပြီး နံပါတ်တွေ မဟုတ်ပါဘူး၊ lazy method တွေက recipe တစ်ခုကိုပဲ တည်ဆောက်လို့ပါ။ first(n) ဒါမှမဟုတ် take(n).to_a / force နဲ့ run ပါ။ ပြောင်းပြန်လည်း ရှိပါတယ်၊ အဆုံးမရှိ range မှာ .lazy မေ့ပြီး (1..Float::INFINITY).map { ... } လို့ ရေးရင် ဘယ်တော့မှ မပြီးပါဘူး။
3. အဆုံးရောက်ပြီးမှ next ခေါ်တာ
Enumerator ကို element အားလုံး သုံးပြီးသွားတဲ့နောက် next ခေါ်ရင် StopIteration: iteration reached an end ကို raise လုပ်ပါတယ်၊ loop ဒါမှမဟုတ် rescue မပါရင် ပရိုဂရမ် ရပ်သွားပါမယ်။ အစကပြန်ချင်ရင် rewind ခေါ်ပါ၊ ဒါမှမဟုတ် while ထဲမှာ ကိုယ်တိုင် next ခေါ်မယ့်အစား loop { e.next } ကို သုံးပါ။
4. Version လို့ပဲ string အဖြစ် sort တာ
["1.10", "1.2"].sort က ["1.10", "1.2"] ကို ပြန်ပေးပါတယ်၊ String နှိုင်းယှဉ်ချက်က စာလုံးတစ်လုံးချင်း နှိုင်းလို့ "1.1" က "1.2" ထက် ငယ်နေလို့ပါ။ နံပါတ်အဖြစ် နှိုင်းချင်ရင် <=> ကို major နဲ့ minor နံပါတ်တွေပေါ်မှာ ရေးပြီး Comparable ကို include လုပ်ပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ကိုယ်ပိုင် collection class တိုင်းမှာ include Enumerable နဲ့ each ကိုပဲ ရေးပါ၊ map, select, count တွေကို ကိုယ်တိုင် ထပ်မရေးပါနဲ့။
- each ကို yield နဲ့ ရေးပြီး to_enum guard ထည့်ပါ၊ ဒါမှ playlist.each.with_index(1) လို caller တွေ အလုပ်လုပ်ပါမယ်။
- အဆုံးမရှိ ဒါမှမဟုတ် ကြီးမားတဲ့ source တွေအတွက် lazy ကို သုံးပြီး first(n) ဒါမှမဟုတ် take(n).to_a နဲ့ ပိတ်ပါ။
- နှိုင်းယှဉ်လို့ရမယ့် value-like object တွေမှာ <=> တစ်ခု ရေးပြီး Comparable ကို include လုပ်ပါ၊ နှိုင်းလို့ မရတဲ့ object အတွက် nil ပြန်ပါ။
Mini challenge
Mini challenge: လေ့လာချိန် StudyLog
နေ့စဉ် လေ့လာတဲ့ မိနစ်တွေကို သိမ်းတဲ့ StudyLog class တစ်ခု ရေးပါ။ each တစ်ခုတည်း ရေးပြီး ကျန်တာကို Enumerable ကနေ ရယူပါ၊ နောက်ဆုံးမှာ lazy Enumerator တစ်ခုလည်း ထည့်ပါ။
- StudyLog က Enumerable ကို include လုပ်ပြီး each က မိနစ်တန်ဖိုး တစ်ခုချင်းစီကို yield လုပ်ရမယ် (to_enum guard ပါရမယ်)
- sum, max နဲ့ count (block နဲ့) ကို သုံးပြီး စုစုပေါင်း၊ အရှည်ဆုံး၊ ပျမ်းမျှထက် များတဲ့ ရက် အရေအတွက်ကို print ထုတ်ရမယ်
- each_cons(2) နဲ့ အရင်ရက်ထက် မိနစ်တိုးတဲ့ ရက်အရေအတွက်ကို print ထုတ်ရမယ်
- Enumerator.new နဲ့ 7 ရဲ့ ဆတိုးကိန်းတွေ ထုတ်ပြီး lazy ပေါ်မှာ select လုပ်ကာ 50 ထက်ကြီးတဲ့ ပထမ ၃ ခုကို print ထုတ်ရမယ်
class StudyLog
include Enumerable
# TODO: initialize(minutes) stores the array
# TODO: each yields every value, with the to_enum guard
end
log = StudyLog.new([30, 45, 20, 50, 50])
# TODO: print the total, the longest session and the days above average
# TODO: print how many days rose compared with the day before (each_cons)
# TODO: an Enumerator.new that yields 7, 14, 21, ... and a lazy select for > 50ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: Total: 195 minutes Longest: 50 minutes Days above average: 3 Rising days: 2 Multiples of 7 above 50: [56, 63, 70]
အရိပ်အမြွက် ကြည့်ရန်
ပျမ်းမျှအတွက် average = log.sum.fdiv(log.count) ကို သုံးပါ။ log.count { |m| m > average } ကတော့ ကိုက်တာတွေကိုပဲ ရေပါတယ်။ Enumerator.new ထဲမှာ n = 0 နဲ့ စပြီး loop { y << (n += 7) } လို့ ရေးလို့ ရပါတယ်၊ ပြီးရင် .lazy.select { ... }.first(3) ကို သုံးပါ။
အဖြေ ကြည့်ရန်
class StudyLog
include Enumerable
def initialize(minutes)
@minutes = minutes
end
def each
return to_enum(:each) unless block_given?
@minutes.each { |value| yield value }
self
end
end
log = StudyLog.new([30, 45, 20, 50, 50])
puts "Total: #{log.sum} minutes"
puts "Longest: #{log.max} minutes"
average = log.sum.fdiv(log.count)
puts "Days above average: #{log.count { |minutes| minutes > average }}"
puts "Rising days: #{log.each_cons(2).count { |before, after| after > before }}"
multiples = Enumerator.new do |y|
n = 0
loop { y << (n += 7) }
end
puts "Multiples of 7 above 50: #{multiples.lazy.select { |n| n > 50 }.first(3)}"Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Enumerable က each တစ်ခုတည်းကို တောင်းပြီး map, select, sort, min, first စတဲ့ method ဆယ်ဂဏန်းများစွာ ပေးပါတယ်၊ each က element တိုင်းကို yield လုပ်ရမယ်။
- Block မပါရင် Enumerator ပြန်ပေးဖို့ return to_enum(:each) unless block_given? ကို ထည့်ပါ၊ Enumerator တွေကိုယ်တိုင်က Enumerable ဖြစ်လို့ chain လုပ်လို့ ရပါတယ်။
- External enumerator မှာ next, peek, rewind ကို သုံးပြီး element ကုန်ရင် StopIteration ပေါ်ပါတယ်၊ loop က အဲဒါကို တိတ်တိတ်ဖမ်းပါတယ်၊ Enumerator.new က sequence ကို အစကနေ တည်ဆောက်ပါတယ်။
- lazy က အလုပ်ကို first(n) လို အဆုံးသတ် method က တောင်းမှ လုပ်လို့ အဆုံးမရှိ sequence တွေကို ကိုင်နိုင်ပါတယ်၊ Comparable က <=> တစ်ခုကနေ နှိုင်းယှဉ်တဲ့ method တွေ ပေးပါတယ်။
Ruby Enumerator Documentation — Ruby Documentation