Skip to content

Instantly share code, notes, and snippets.

@matthieubulte
matthieubulte / gist:fdc76857ff79e5fdb0e7
Created February 26, 2015 12:29
AngularJS directives partial application
var app = angular.module('test', []);
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
function id(a) {
return a;
}
function Left(left) {
this.isLeft = true;
this.isRight = false;
this.value = left;
}
function Right(right) {
this.isLeft = false;
this.isRight = true;
@matthieubulte
matthieubulte / parser.js
Last active August 30, 2016 18:05
parser combinator - hutton
let slice = Array.prototype.slice;
let str = s => slice.call(s)
let unstr = cs => cs.reduce((c, cs) => c + cs, "")
let concat = xss => xss.reduce((xs, ys) => xs.concat(ys), [])
let id = x => x
let negate = x => -x
let add = (x,y) => x + y
let sub = (x,y) => x - y
// type Parser a = () -> [Char] -> a
@matthieubulte
matthieubulte / pattern_matching.js
Last active August 29, 2015 14:22
Javascript pattern matching (Array, Maybe, Either)
// Array
let split_array = cases => array =>
array.length ?
cases.cons(array[0], Array.prototype.slice.call(array, 1)) :
cases.nil()
let map = f => split_array({
nil: () => [],
cons: (x, xs) => [f(x)].concat(map(f)(xs))
@matthieubulte
matthieubulte / lens.js
Last active August 29, 2015 14:25
Basic Lenses In Javascript
// Utilities functions
function _const(x) {
return function(_) {
return x;
};
}
function _comp(g, f) {
return function(x) {
-- Runnable version can be found at: https://glot.io/snippets/e6bg7n3hjs
--
-- Inspired by http://logicaltypes.blogspot.de/2011/04/attributed-variables-their-uses-and-one.html
-- Here, I try to solve the problem by reducing the 2d grid to a list of (Position, Int) contrained
-- by the rules of sudoku enforced by `guard`s.
import Data.List (sort, sortBy, nub, zip, (\\))
import Control.Monad (guard)
type Domain a = [a]
@matthieubulte
matthieubulte / p-a-t.md
Last active October 4, 2015 21:22
Propositions as Types

List of papers (with links to the PDF versions) mentioned in Philip Wadler's presentation on Propositions as Types at StrangeLoop 2015 (https://www.youtube.com/watch?v=IOiZatlZtGU)

@matthieubulte
matthieubulte / Reader.hs
Created February 25, 2016 08:57
Reader Functor/Applicative/Monad instances with laws proofs
module Reader where
newtype F r a = F { unF :: r -> a }
instance Functor (F r) where
fmap g f = F (g . unF f)
{-
identity : fmap id = id
-----------------------
@matthieubulte
matthieubulte / FoldlAsFoldr.hs
Created February 27, 2016 10:56
foldl in terms of foldr
foldl' :: (b -> a -> b) -> b -> [a] -> b
foldl' f z xs = foldr (\x fs z -> fs (f z x)) id xs z
@matthieubulte
matthieubulte / lazy.js
Created February 28, 2016 12:20
lazy functor / applicative / monad in js
"use strict";
let lazy = f => {
let empty = {};
let val = empty;
return () => val === empty ? val = f() : val;
};
// force :: lazy a -> a
let force = l => l();