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
-- http://www.randomhacks.net.s3-website-us-east-1.amazonaws.com/2007/02/08/haskell-queues-without-pointers/ | |
{-# OPTIONS_GHC -Wall #-} | |
module Queue where | |
-- A queue holding values of type 'a'. | |
data Queue a = Queue [a] [a] | |
deriving (Show) | |
-- Create a new queue. |
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 | |
/** | |
* Run it: php r30.php | |
* Run it with a custom number of rows: php r30.php <number> | |
* Have fun :) | |
*/ | |
function createRows($numRows, $rule) { | |
$getNextRow = function ($currRow) use ($rule) { | |
$nextRow = [0, 0]; |
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 | |
declare(strict_types = 1); | |
class ANTree | |
{ | |
const TERMINAL_NODE = 'LEAF'; | |
private $routeTree = []; | |
public function add(array $segments): void | |
{ |
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 | |
declare(strict_types=1); | |
$trampoline = function (callable $f) { | |
return function (...$args) use ($f) { | |
while (is_callable($f)) { | |
$f = $f(...$args); | |
} | |
return $f; | |
}; |
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
-- read string into list | |
input = map (\x -> read [x]::Int) | |
----------- | |
-- Day 1 -- | |
----------- | |
-- part 1 | |
f xs = sum $ map fst $ filter (\(x,y) -> x == y) $ zip xs $ tail $ (\xs -> xs ++ [head xs]) xs |
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 | |
declare(strict_types=1); | |
class DisjointSet | |
{ | |
private $parent = []; | |
private $rank = []; | |
public function makeSet(int $i): void | |
{ |
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 | |
// https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes | |
function sieve(int $n): array { | |
$a = array_fill(2, $n - 1, true); | |
for ($i = 2, $iMax = floor(sqrt($n)); $i <= $iMax; $i++) { | |
if ($a[$i] === true) { | |
for ($j = $i ** 2; $j <= $n; $j += $i) { | |
$a[$j] = false; | |
} | |
} |
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
{-# OPTIONS_GHC -Wall #-} | |
module Main (main) where | |
import Data.List (unfoldr) | |
bs :: [Int] -> Int -> Int | |
bs ys key = binarySearch ys 0 (length ys - 1) key where | |
binarySearch xs l h k | |
| h < l = -1 | |
| k == x = m |
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 | |
declare(strict_types=1); | |
class X { | |
private $s; | |
public function __construct(string $s) { $this->s = $s; } | |
public function getS(): string { return $this->s; } | |
} | |
class Y { | |
private $x; |
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 | |
declare(strict_types=1); | |
// ((a, ...) -> z) -> a -> ... -> z | |
function curry(callable $f, ...$args) { | |
return function (...$partialArgs) use ($f, $args) { | |
return (function ($args) use ($f) { | |
return \count($args) < (new \ReflectionFunction($f))->getNumberOfRequiredParameters() | |
? curry($f, ...$args) | |
: $f(...$args); |