Skip to content

Instantly share code, notes, and snippets.

@domgetter
Last active March 27, 2017 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domgetter/45d72196f2fa29ace17e0b17238aa819 to your computer and use it in GitHub Desktop.
Save domgetter/45d72196f2fa29ace17e0b17238aa819 to your computer and use it in GitHub Desktop.
struct Int
def palindrome?
self.to_s == self.to_s.reverse
end
end
def nums
(100..9999).each do |i|
(i..9999).each do |j|
yield i*j
end
end
end
result = 0
nums do |product|
if product > result && product.palindrome?
result = product
end
end
puts result
nums.enum_for.lazy.select(&.palindrome?).max
struct Int
def palindrome?
self.to_s == self.to_s.reverse
end
end
chan = Channel(Int32 | Iterator::Stop).new(1)
spawn do
(100..999).each do |i|
(i..999).each do |j|
chan.send i * j
end
end
chan.send Iterator::Stop::INSTANCE
end
class Products
include Iterator(Int32)
def initialize(chan : Channel(Int32 | Iterator::Stop))
@chan = chan
end
def next
@chan.receive
end
end
products = Products.new(chan)
puts products.select(&.palindrome?).max
struct Int
def palindrome?
self.to_s == self.to_s.reverse
end
end
result = 0
(100..999).each do |i|
(i..999).each do |j|
if i*j > result && (i*j).palindrome?
result = i*j
end
end
end
puts result
# What I'd like to write is...
nums = Iterator.new do |yielder|
(100..999).each do |i|
(i..999).each do |j|
yielder << i * j
end
end
end
nums.select(&.palindrome?).max
# As this would separate the work I'm doing from the things I'm doing the work on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment