Skip to content

Instantly share code, notes, and snippets.

View joachifm's full-sized avatar
🌴
On vacation

Joachim F. joachifm

🌴
On vacation
  • Oslo, Norway
  • 10:19 (UTC +02:00)
View GitHub Profile
module Alternate where
import Control.Arrow (Arrow, (***))
import Test.QuickCheck
-- | Transform a list into a list of pairs.
-- Lazy consumer.
pairs :: [a] -> [(a, a)]
pairs (x:y:z) = (x, y) : pairs (y:z)
pairs _ = []
@joachifm
joachifm / A.hs
Created May 6, 2011 07:23
Problem A
import Control.Monad
import Data.List (find, unfoldr)
main = interact (unlines . map (uncurry fmt) . zip [1..] . map solve . parse)
where fmt n (Just (x, y)) = "Case #" ++ show n ++ ": " ++ unwords [show x, show y]
fmt _ Nothing = ""
solve (c, xs) = go $ zip [1..] xs
where go ((i, n):ys) =
case find (\(_, n') -> n' + n == c) ys of
@joachifm
joachifm / battle.R
Last active September 26, 2015 19:38
Match history scraper
library(XML)
url <- "http://eu.battle.net/sc2/en/profile/2007578/1/joachifm"
scrape.matchhistory <- function(profile.url) {
url <- paste(profile.url, "/matches", sep="")
doc <- htmlTreeParse(url, useInternalNodes=T)
thead <- xpathSApply(doc, "//*/table[@class='data-table']/thead/tr/th", xmlValue)
tbody <- xpathSApply(doc, "//*/table[@class='data-table']/tbody/tr/td", compose(trim, xmlValue))
m <- matrix(tbody, ncol=length(thead), byrow=T)
@joachifm
joachifm / Volume.hs
Created May 5, 2012 11:09
Volume values in the range 0-100
module Volume (runTest) where
import Control.Monad (forM_)
import qualified Test.QuickCheck as QC
-- | Volume values.
--
-- Values of this type are always in the range 0-100.
--
-- Arithmetic on volumes has the property that:
@joachifm
joachifm / FoldLength.hs
Created August 15, 2013 17:17
General length
{-|
General 'length' for any 'Foldable' structure.
> foldLength [undefined, undefined] = 2
-}
module FoldLength (foldLength1, foldLength2) where
import Data.Functor
import qualified Data.Foldable as F
@joachifm
joachifm / Timed.hs
Created August 15, 2013 17:19
Simplistic timed computations
module Timed (timed) where
import Control.Concurrent
-- | Fork a process that should return some value within some time limit.
--
-- > timed 10 (threadDelay 100 >> return "takes too long") = Nothing
-- > timed 100 (threadDelay 10 >> return "in time") = Just "in time"
timed :: Int -- microseconds
-> IO a
@joachifm
joachifm / HashChain.hs
Last active August 29, 2015 13:56
Naive hash chains
{-|
A naive implementation of hash chains; can be used for
one-time password systems.
See <https://en.wikipedia.org/wiki/S/KEY>.
-}
module HashChain (
HashChain(_size, _hash)
, next
, init_chain
@joachifm
joachifm / Sieve.hs
Last active August 29, 2015 13:56
Prime sieve
{-
A direct implementation of a prime sieve ...
-}
module ArrSieve
( Sieve(unSieve)
, isPrime
, unsafeIsPrime
, sieve
) where
@joachifm
joachifm / Fizzzzz.hs
Created September 10, 2014 05:26
Fizz
import Control.Monad (guard)
import Control.Applicative ((<|>), (*>), pure)
import Data.Maybe (mapMaybe)
import Data.Monoid ((<>))
fizzBuzz :: [Integer] -> [String]
fizzBuzz = mapMaybe (\x -> g 3 "Fizz" x <> g 5 "Buzz" x <|> pure (show x))
where g d s x = guard ((x `rem` d) == 0) *> pure s
@joachifm
joachifm / commitment.hs
Created November 29, 2014 22:02
Commit to a message without leaking information (?)
{-# LANGUAGE OverloadedStrings #-}
module Commitment ( Message, Commitment, Opening, commit, reveal ) where
import Crypto.Cipher.AES (initAES, encryptCTR, decryptCTR)
import qualified Data.ByteString.Lazy as LB
import qualified Data.ByteString as SB
--