Skip to content

Instantly share code, notes, and snippets.

View craigjbass's full-sized avatar
😁

Craig J. Bass craigjbass

😁
View GitHub Profile
@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
@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 / 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 / ProjectEuler14.hs
Last active December 30, 2015 10:29
Longest Collatz sequence (Problem 14) http://projecteuler.net/problem=14
module Main (
main
) where
import Data.List (maximumBy)
import Data.Function (on)
collatz 1 = [1]
collatz i = i : (if odd i then collatz $ 3*i + 1 else collatz $ i `div` 2)
main = putStrLn $ show $ maximumBy (compare `on` length) $ map collatz [1..1000000]
@craigjbass
craigjbass / Email_Service_Premailer.php
Last active January 2, 2016 00:18
Email_Service_Premailer
<?php
class Email_Service_Premailer {
/**
* Process using premailer
*
* requires:
* yum -y install ruby rubygems ruby-devel
* gem install premailer
* gem install hpricot
@craigjbass
craigjbass / Devls_Enum.php
Last active August 29, 2015 13:56
PHP Enums
<?php
/**
* Enum Abstract Class
*
* Warning: Uses Reflection and LSB. :D
* ==========================================================
*
* To use this class:
*
@craigjbass
craigjbass / ng-angular-element
Created July 11, 2014 13:25
Implements the converse to ng-non-bindable: "ng-bindable" (only compiles mark up within <angular> tags, this is useful for legacy applications which cannot have {'s and }'s escaped easily.)
angular.module( 'ng-angular-element', [] )
.directive( 'angular', function() { return { restrict: 'E' }; } )
.directive( 'html', ['$compile', '$rootScope', '$document',
function( $compile, $rootScope, $document ) {
return {
restrict: 'E',
compile: function(scope, element, attributes) {
angular.forEach( $document.find( 'angular' ), function( angularElement ) {
$compile(angularElement)($rootScope);
} );
@craigjbass
craigjbass / LukeAndCraig.kt
Last active August 26, 2016 10:42
Code Kata: WordWrap
package uk.co.devls.coffeebook.test.unit
import org.amshove.kluent.`should equal`
import kotlin.test.*
import org.jetbrains.spek.api.Spek
class CodeDojoTest : Spek({
describe("Wrapper") {
it("should return empty string when provided empty string") {
Wrapper().wrap("", 0).`should equal`("")

Create three implementations of a simple list data structure:

  • A singly linked list
  • A doubly linked list
  • A list with no form of linking

#Each node has:

Metadata: e.g. link to next node (Metadata can contain anything that you need to make the list work)

Data: a string payload (Data contains only the string payload)

class HTTPProductGateway
def find_by(id:)
@id = id
product_for(decode(fetch_product_from_api))
end
private
def product_for(product_payload)