Wordpress
I initially copied all my blogger entries through the API. After a lot of fighting with the formatting, I concentrated on my entries (3) since January 1st, 2007.
So, this is where it happens now.
No Magic - only tricks
def stream_from(value)
lambda {value = value.succ}
end
class Proc
def filter
f = lambda do
value = self.call
return (yield(value) ? value : f.call)
end
end
def take(n)
result = []
n.times {result << self.call}
return result
end
end
As for "filter", it takes a predicate passed as block. The filter becomes a "decorator" for the stream.
s = stream_from(Date.today).filter {|date| date.cwday < 6}
puts s.take(20)
s = stream_from(4)
puts s.take(20)
s = stream_from(Date.today).filter {|date| date.day == 17}.filter {|date| date.cwday == 1}
puts s.call.to_s
def monday?(date)
date.cwday == 1
end
s = stream_from(Date.today).filter {|date| date.day == 17}.filter(&method(:monday?))
s = stream_from(5_000_000_000).filter{|x| x.prime?}.filter{|x| x.mod(10) == 3}
puts s.take(3)
function include(value, list) {
for (var i=0; i < list.length; i++) {
if(list[i] == value) return true;
};
return false;
}
function succ(date) {
return new Date(date.getTime() + 1000 * 60 * 60 * 24);
}
function is_business_day(date) {
return include(date.getDay(), [1,2,3,4,5]); // mon-fri
}
function business_succ(date) {
var result = succ(date);
return is_business_day(result) ? result : business_succ(result);
}
function repeat(f, x, n) {
if(n == 1)
return f(x);
return f(repeat(f, x, n-1));
}
function days(date, n) {
return repeat(succ, date, n);
}
function business_days(date, n) {
return repeat(business_succ, date, n);
}
Personally, the moment of enlightenment came when I realized that iterating for days or business days was the same code and that it could be made even more generic by the repeat function.
#!/usr/bin/env ruby
$VERBOSE = true
pattern = ARGV[0]
pattern ||= '*'
latest = Dir.glob(pattern).sort_by {|f| File.mtime(f)}.last
puts latest unless latest.nil?
...we really don’t know something until we use it. Understanding that we can’t really separate knowing from doing explains why people who love to code are so much better at it than those who don’t: they try stuff out.
The cycle of learning is to learn a little, then use what you’ve learned, then learn some more.
puts "before: #{Time.now}"
class Time
def self.now
'mock answer'
end
end
puts "after: #{Time.now}"
before: Thu Sep 14 13:02:46 EDT 2006
after: mock answer