Skip to content

Instantly share code, notes, and snippets.

insert(X, Xs, [X|Xs]).
insert(E, [X|Xs], [X|Ys]) :- insert(E,Xs,Ys).
permute([],[]).
permute(L,[H|T]) :- insert(H,R,L), permute(R,T).
sudoku([[X11,X12,X13,X14,X15,X16,X17,X18,X19],
[X21,X22,X23,X24,X25,X26,X27,X28,X29],
[X31,X32,X33,X34,X35,X36,X37,X38,X39],
[X41,X42,X43,X44,X45,X46,X47,X48,X49],
@mlhaufe
mlhaufe / pathfinding.pl
Created June 22, 2015 00:55
Pathfinding
adjacent(ems,phy).
adjacent(ems,soccer).
adjacent(soccer,cunn).
adjacent(ems,chem).
adjacent(chem,lap).
adjacent(lap,bridge).
adjacent(lap,arch).
adjacent(arch,engel).
adjacent(cunn,engel).
adjacent(bridge,union1).
@mlhaufe
mlhaufe / sqrt.js
Last active February 20, 2016 17:30
Square Root : Newton's Method
var sqrt = x => {
let improve = y => (y + x/y)/2
let satisfied = y => Math.abs(y*y - x) < 1e-14 //Number.EPSILON
let until = (pred,f,x) => pred(x) ? x : until(pred,f,f(x))
return until(satisfied,improve,x)
};
@mlhaufe
mlhaufe / Object-walk.js
Last active October 27, 2015 16:09
Object walk
// <https://twitter.com/sroucheray/status/658913220292452352>
Object.walk = (o, path)=>path.split('.').reduce((o,k)=>o && o[k], o);
@mlhaufe
mlhaufe / cookieManager.js
Last active January 10, 2017 22:49
Basic Cookie Management
var cookieManager = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
@mlhaufe
mlhaufe / Term.ts
Created October 19, 2016 04:46
Term Evaluation in Pure OO style
abstract class Term<T> {
abstract eval() : T
}
class Lit extends Term<number> {
constructor(public value: number) { super() }
eval() : number { return this.value }
}
class Inc extends Term<number> {
@mlhaufe
mlhaufe / Color.cs
Last active November 17, 2016 19:35
simple color shading
using System;
public class Program
{
public static void Main()
{
Func<string,double,string> shade = (hex, pct) => {
Func<int,int,int,int> clamp =
(value,min,max) => value < min ? min : value > max ? max : value;
var num = Convert.ToUInt32(hex.Replace("#",""),16);
@mlhaufe
mlhaufe / index.html
Last active March 6, 2020 14:59
strict mode efficiency comparison w/ Newtons Method (http://jsbench.github.io/#7e323ab9310ec4fc933db0883c96ce0c) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>strict mode efficiency comparison w/ Newtons Method</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
@mlhaufe
mlhaufe / Either.ts
Last active January 16, 2017 19:10
Coproducts
// data Either a b = Left a | Right b
abstract class Either<A, B> {
constructor(public value: A | B) { }
}
// Inject Left
// inl a
class Left<A> extends Either<A, never>{
constructor(value: A) { super(value) }
}
// Inject Right