Skip to content

Instantly share code, notes, and snippets.

View craigjbass's full-sized avatar
😁

Craig J. Bass craigjbass

😁
View GitHub Profile
@craigjbass
craigjbass / ImpureDatabaseWrapper.php
Last active December 26, 2015 17:39
"Monadic" Models for improved unit-testability. (Pure - Impure separation of control)
<?php
class ImpureDatabaseWrapper {
private $sql;
private $pureBinding;
/**
* @var \Zend_Db_Select $sql
* @var \Closure $pureBinding (Closure that contains "pure" code - i.e. it doesn't cause any side-effects!)
*/
@craigjbass
craigjbass / constraint_permutations.hs
Last active December 26, 2015 17:09
Give all possible permutations given a list of lists such that each list defines the set of characters for that particular index of the key. Whereby the top level list is the same length as the overall key.
-- The following holds true permuteAll [ [ 'a','b' ], [ 'c','d' ] ] => [ ['a','c'],['a','d'], ['b','c'], ['b','d'] ]
permuteAll :: [[a]] -> [[a]]
permuteAll [] = []
permuteAll (x:[]) = splitEvery 1 x
permuteAll (x:xs) = foldr (++) [] $ map f x
where
f :: a -> [[a]]
f x' = map ( \y -> (x':y) ) (permuteAll xs)
@craigjbass
craigjbass / splicer.hs
Last active December 26, 2015 13:59
splicer :: [[a]] -> [[a]] such that the following holds true: splicer [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ] => [ ['a', 'c', 'e'], ['b', 'd', 'f'] ] Useful for use in Vigenere cipher breakers.
-- such that the following holds true (where => indicates function output)
-- splicer [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ] => [ ['a', 'c', 'e'], ['b', 'd', 'f'] ]
splicer :: [[a]] -> [[a]]
splicer [] = []
splicer (x:[]) = splitEvery 1 x
splicer (x:xs) = zipWith (:) x $ splicer xs