Last active
June 9, 2017 13:33
-
-
Save muhammadyana/5a1bbdb176c302551da4a628fa83fc0b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#take every sentence | |
counts = Hash.new(0) | |
File.foreach("sample.txt") do | |
|line| | |
line.scan(/\w+/) do | |
|word| | |
word = word.downcase | |
counts[word] += 1 | |
end | |
end | |
counts.keys.sort.each{ | |
|k| | |
p "#{k}:#{counts[k]}" | |
} | |
#another way take every sentece in file | |
words = Fiber.new do | |
File.foreach("sample.txt") do | |
|line| | |
line.scan(/\w+/) do | |
|word| | |
Fiber.yield word.downcase | |
end | |
end | |
nil | |
end | |
counts = Hash.new(0) | |
while word = words.resume | |
counts[word] += 1 | |
end | |
counts.keys.sort.each { | |
|k| | |
p "#{k}:#{counts[k]}" | |
} | |
two = Fiber.new do | |
num = 2 | |
loop do | |
Fiber.yield(num) unless num % 3 == 8 | |
num += 2 | |
end | |
end | |
2.times {print two.resume, " "} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment