Skip to content

Instantly share code, notes, and snippets.

View md2perpe's full-sized avatar

Per Persson md2perpe

View GitHub Profile
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 [] = []
@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
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 / 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();
@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,
@md2perpe
md2perpe / English
Created March 9, 2012 13:23
Class level dependency injection
I often find a need to let all objects of one class to have access to the same service. It then feels like waste of both time and memory to inject the service into each single object. Instead I'd like to inject the service into the whole class and let in be located on class level.
Example:
You have a web store with order and customer objects.
An order object can - via a customer repository - supply the customer who made the order.
If you fetch a sequence of orders, e.g. to show a list of orders and the customers who made them, every order needs access to the customer repository. At the same time the orders should be filled with information from the database.
I've never seen a discussion about class level DI. Is that never done? If not, how are cases like this handled?
class Safe
{
protected $value;
public function __construct($value) { $this->value = $value; }
public function __call($method, $args)
{
if (is_object($this->value) && method_exists($this->value, $method)) {
return new self(call_user_func_array([$this->value, $method], $args);
@md2perpe
md2perpe / sec2time.php
Created May 4, 2012 21:21
Convert seconds to years, days, hours, minutes and seconds
function sec2time($time)
{
if(!is_numeric($time))
return false;
$value = array(
'seconds' => 60,
'minutes' => 60,
'hours' => 24,
'days' => 365,
@md2perpe
md2perpe / shutdown.php
Created May 9, 2012 15:14
Making a destructor to be run even on fatal error
<?php
class Logger
{
protected $rows = array();
public function __construct()
{
register_shutdown_function(array($this, 'shutdown'));
}
<?php
if(isset($_POST['sendRequest']))
{
//Jag tar emot start-date och end-date via ett formular.
//Jag kor strtotime pa varderna for att omvandla dem till Unix timestamp
$startDate = strtotime($_POST['startDate']);
$endDate = strtotime($_POST['endDate']);
//Uppbyggnad av tabell
echo '<table width="100%" border="1">';