Skip to content

Instantly share code, notes, and snippets.

View edvardm's full-sized avatar

Edvard Majakari edvardm

  • Rakettitiede Oy
  • Finland
  • 15:27 (UTC +02:00)
View GitHub Profile
# 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.
module Enumerable
def sum
self.inject(0) {|acc, i| acc+i}
end
end
module Harry
class << self
def price(books)
nslots = 5
@edvardm
edvardm / webCrawler.hs
Created October 3, 2011 09:36
My take on Haskell: Functional Programming, Solid Code, Big Data by Bryan O'Sullivan @ Strange Loop 2011
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
@edvardm
edvardm / enumerable_extension.rb
Created October 27, 2011 05:56
Small convenience method to build stuff on the fly
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
@edvardm
edvardm / w3clogparser.hs
Created November 4, 2011 20:42
naive access log parser in haskell
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]
@edvardm
edvardm / poker.rb
Created April 23, 2012 18:36
My first attempt at poker kata /w Ruby
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
@edvardm
edvardm / traversable.rb
Created October 9, 2012 20:10
Generic traverse method for hash-ish structures
module Traversable
module InstanceMethods
def map_recursive(*args, &block)
Traversable.map_recursive(self, *args, &block)
end
end
class << self
def included(other)
@edvardm
edvardm / gist:4038157
Created November 8, 2012 10:56
map helper
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
@edvardm
edvardm / img_pixels.rb
Created November 14, 2012 14:53
rmagick sample
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"
@edvardm
edvardm / harry.rb
Created December 10, 2012 11:33
potter kata
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