Last active
June 8, 2017 04:08
-
-
Save muhammadyana/61f775d3d6d598129826b9a5140e9b71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 3, "cat"] | |
b = { | |
dog: "ben", | |
cat: "tom" | |
} | |
enum_a = a.to_enum #untuk | |
enum_b = b.to_enum | |
enum_a1 = a.each | |
#puts "Enumator is #{enum_a1.next[2]}" | |
# puts enum_a1.next | |
# puts enum_a1.next | |
# | |
# puts enum_a.next | |
# p enum_b.next | |
# puts enum_a.next | |
# p enum_b.next | |
short_enum = (1..100).to_enum | |
long_enum = ('a'..'z').to_enum | |
# loop do | |
# puts "#{short_enum.next} - #{long_enum.next}" | |
# end | |
# result = [] | |
# index = 1 | |
# [ 'a', 'b', 'c', 'd' ].each_with_index { | |
# |item, index| result << [item, index] | |
# } | |
# p result | |
enum = "cat as".enum_for(:each_char) | |
#p enum.to_a | |
# result = [] | |
# "cat".each_char.each_with_index { | |
# |item, index| result << [item, index] | |
# } | |
# p result | |
# | |
# enum_in_three = (1..12).enum_for(:each_slice, 3) | |
# p enum_in_three.to_a | |
triangular_numbers = Enumerator.new do |yielder| | |
number = 0 | |
count = 1 | |
loop do | |
number +=count | |
count +=1 | |
yielder.yield number | |
end | |
end | |
# | |
# 5.times { | |
# print triangular_numbers.next, "," | |
# } | |
# puts | |
puts "Result 5 from triangular_numbers is #{triangular_numbers.first(5)}" | |
def infinite_select(enum, &block) | |
Enumerator.new do |yielder| | |
enum.each do |value| | |
yielder.yield(value) if block.call(value) | |
end | |
end | |
end | |
p infinite_select(triangular_numbers) {|val| val % 10 == 0}.first(5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment