Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
@md2perpe
md2perpe / mail-pdf.php
Created January 17, 2012 13:45
Mail a PDF from PHP
<?php
// Läs in PDF-dokument och konvertera till base64
$pdf = file_get_contents('test.pdf');
$b64 = base64_encode($pdf);
// Läs in mail-mall
ob_start();
include 'mail-tpl.php';
$mail = ob_get_clean();
pages :: Int -> Int -> [(Int, Int)]
pages 1 n = [(1, n)]
pages m n = (m, n `div` m) : pages (m `div` 2) (n `mod` m)
result :: Int -> [(Int, Int)]
result n = pages 16 n
@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
import Data.List(find)
findSum :: Num a => a -> [a] -> Bool
findSum s ns = find (sumIs s) (pairs ns)
where
sumIs :: Num a => a -> (a, a) -> Bool
sumIs s (x, y) = x+y == s
pairs :: [a] -> [(a, a)]
pairs [] = []
firstNonRep :: String -> Maybe Char
firstNonRep cs = fmap fst $ find (\(c, n) -> n == 1) $ foldl count [] cs
where
count [] c = [(c, 1)]
count (p@(c', n) : ps) c | c == c' = (c', n+1) : ps
| otherwise = p : count ps c
@md2perpe
md2perpe / lazyload.php
Created June 27, 2011 18:55
Lazyloading in PHP
<?php
abstract class Loader
{
abstract public function load();
}
class PaymentLoader extends Loader
{
protected $id;
data Tree t = EmptyTree
| Node t (Tree t) (Tree t)
deriving Show
nivel :: Int -> Int
nivel n = nivel' n 0
where
nivel' 0 _ = 0
nivel' 1 x = x
nivel' n x = nivel' (n `div` 2) (x + 1)
@md2perpe
md2perpe / gist:1027615
Created June 15, 2011 17:32
Flyborg har fel
Att stödja en regeringen är inte samma sak som att sitta i regeringen. Flyborg påstod att MP var "i regeringsställning", vilket betyder att man verkligen sitter i regeringen.
@md2perpe
md2perpe / measurable.js
Created June 12, 2011 21:15 — forked from dachev/measurable.js
Is Measurable
// Recursive version
function isMeasurable(target, weights)
{
if (weights.length == 0)
return target == 0;
var first = weights.shift();
return
isMeasurable(target-first, weights) ||
@md2perpe
md2perpe / mvf.js
Created June 1, 2011 22:05 — forked from fogus/gist:1002886
Methods and fields in Javascript
var M = function() { };
M.prototype = {
m : function() { return 42 }
};
var inst = new M();
inst.f = function() { return 42 };