This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE GADTs, EmptyDataDecls, ExistentialQuantification #-} | |
module RBTree where | |
-- Node color | |
data Red | |
data Black | |
-- Node depth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sec2time($time) | |
{ | |
if(!is_numeric($time)) | |
return false; | |
$value = array( | |
'seconds' => 60, | |
'minutes' => 60, | |
'hours' => 24, | |
'days' => 365, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Logger | |
{ | |
protected $rows = array(); | |
public function __construct() | |
{ | |
register_shutdown_function(array($this, 'shutdown')); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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">'; |