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
# One copy of any of the five books costs 8 EUR. If, however, you buy two | |
# different books from the series, you get a 5% discount on those two books. | |
# If you buy 3 different books, you get a 10% discount. With 4 different | |
# books, you get a 20% discount. If you go the whole hog, and buy all 5, you | |
# get a huge 25% discount. | |
# Note that if you buy, say, four books, of which 3 are different titles, you | |
# get a 10% discount on the 3 that form part of a set, but the fourth book | |
# still costs 8 EUR. |
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
module Enumerable | |
def sum | |
self.inject(0) {|acc, i| acc+i} | |
end | |
end | |
module Harry | |
class << self | |
def price(books) | |
nslots = 5 |
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
import Network.HTTP.Enumerator | |
import Data.ByteString.Lazy.UTF8 as BS | |
import Text.HTML.TagSoup | |
import Control.Monad | |
import Data.Maybe | |
import Network.URI | |
download :: String -> IO String | |
download uri = liftM BS.toString $ simpleHttp uri |
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
module Enumerable | |
# like inject, but returns accumulator instead. Instead of writing | |
# [1, 2].inject({}) {|h, i| h[i] = 2*i; h } | |
# just say | |
# [1, 2].infuse({}) {|h, i| h[i] = 2*i } # -> {1 => 2, 2 => 4} | |
def infuse(init, &block) | |
inject(init) { |t, i| block.call(t, i); t } | |
end | |
end |
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
import Data.List | |
import Text.ParserCombinators.Parsec | |
w3cLog = endBy line eol | |
line = do | |
[date, time] <- count 2 $ prg field | |
[ip, meth, uri, status, bytes, elapsedms] <- count 6 $ prg field | |
[referer, ua] <- count 2 $ prg quotedField | |
cookie <- quotedField | |
let datetime = intercalate " " [date, time] |
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
require 'rspec' | |
class Array | |
include Comparable | |
def <=>(other) | |
if self[0] == other[0] | |
self[1..-1] <=> other[1..-1] | |
else | |
self[0] <=> other[0] | |
end |
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
module Traversable | |
module InstanceMethods | |
def map_recursive(*args, &block) | |
Traversable.map_recursive(self, *args, &block) | |
end | |
end | |
class << self | |
def included(other) |
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
module BlockHelper | |
def map_args(method_name) | |
map(&method(method_name.to_sym)) | |
end | |
def each_args(method_name) | |
each(&method(method_name.to_sym)) | |
end | |
end |
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
require 'RMagick' | |
ARGV.each do |file| | |
puts file | |
orig = Magick::Image::read(file).first | |
img = orig.quantize(256, Magick::GRAYColorspace) | |
puts " Format: #{img.format}" | |
puts " Geometry: #{img.columns}x#{img.rows}" | |
puts " Class: " + case img.class_type | |
when Magick::DirectClass | |
"DirectClass" |
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
class Memo | |
def initialize; @memo = {}; end | |
def ized(key, &block); (k = @memo[key]) ? k : @memo[key] = block.call; end | |
end | |
class Harry | |
NSLOTS = 5 | |
DISCOUNTS = [0, 0, 0.05, 0.10, 0.20, 0.25] | |
UNIT_PRICE = 8 |
OlderNewer