Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
#!/bin/bash
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
@staltz
staltz / introrx.md
Last active May 7, 2024 09:38
The introduction to Reactive Programming you've been missing
@md2perpe
md2perpe / jag-skams-inte.md
Last active April 27, 2021 21:38
Jag skäms inte

Jag skäms inte

Jag skäms inte över att vara svensk.
Men jag skäms över hur vissa ser ned på dem som vill bli svenskar.

Jag skäms inte över den svenska flaggan.
Men jag skäms över hur den används mot dem som vill hissa den.

Jag skäms inte över Sverige.
Men jag skäms över hur vissa anser sig ha ensamrätt till landet.

@md2perpe
md2perpe / gist:1886070
Created February 22, 2012 17:04
Binary trees in database with interval halving
For binary trees I've found a variant of nested sets.
The idea is to let
- the root node be represented by the interval [0, 1],
- the two children of the root node be represented by the intervals [0, 1/2] and [1/2, 1] respectively,
- the grandchild nodes by [0, 1/4], [1/4, 1/2], [1/2, 3/4] and [3/4, 1].
Then it's easy to find:
- the left and right children (one or both),
- all descendents of a node,
@bitonic
bitonic / cout.hs
Created November 1, 2011 19:23
cout in Haskell
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, IncoherentInstances #-}
class Cout a r where
(<<) :: IO String -> a -> IO r
instance Cout [Char] [Char] where
lm << x = fmap (++ x) lm
instance Cout [Char] () where
lm << x = lm >>= putStr . (++ x)
@sjoerdvisscher
sjoerdvisscher / ZigZagPlate.hs
Created October 1, 2011 16:33
Multiplate for the ZigZag data type.
import Control.Applicative
import Data.Functor.Constant
import Data.Generics.Multiplate
data ZigZag a b = ZNil | Zig a (ZigZag a b) | Zag b (ZigZag b a)
data ZigZagPlate a b f = ZigZagPlate
{ znilab :: f (ZigZag a b)
, zigab :: a -> ZigZag a b -> f (ZigZag a b)
, zagab :: b -> ZigZag b a -> f (ZigZag a b)
@md2perpe
md2perpe / RBTree.hs
Created September 16, 2011 15:58
Using GADTs, phantom types, quantification and empty data declarations to put constraints on nodes in red-black trees
{-# LANGUAGE GADTs, EmptyDataDecls, ExistentialQuantification #-}
module RBTree where
-- Node color
data Red
data Black
-- Node depth
@lest
lest / lolcat.hs
Created September 5, 2011 20:45
simple lolcat powered by haskell
import Data.Word
freq = 0.3
spread = 8.0
unbase :: Integral int => int -> Word8 -> Word8 -> Word8 -> int
unbase base r g b = (fi r*base+fi g)*base+fi b
where fi = fromIntegral
-- | Approximate a 24-bit Rgb colour with a colour in the xterm256 6x6x6 colour cube, returning its index.
@folone
folone / gist:1188506
Created September 2, 2011 12:36
Factorial with type magic. There's more here: http://www.willamette.edu/~fruehr/haskell/evolution.html
-- static Peano constructors and numerals
data Zero
data Succ n
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three
-- playing with the adaptive library.
import Control.Monad.Adaptive
printMod mod = do
r <- inCh $ readMod mod
inM (putStrLn $ show r)
main = run $ do
i1 <- newMod (return (2 :: Integer))