Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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).
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 / Bool.lps
Last active August 29, 2015 14:12
Lapis Design Notes
module
exports Bool
protocol Bool
False
True
and Bool => Bool
or Bool => Bool
not => Bool
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Click-Through Circle</title>
<style type="text/css">
html, body{
margin:0;
padding:0;
}
@mlhaufe
mlhaufe / AD.cs
Created July 17, 2014 19:04
Active Directory from LINQPad
var searchRoot = new System.DirectoryServices.DirectoryEntry(
@"LDAP://server:389/DC=foo,DC=com",
@"DOMAIN\USERNAME",
"PASSWORD"
){ AuthenticationType = AuthenticationTypes.ReadonlyServer };
using(var searcher = new System.DirectoryServices.DirectorySearcher(searchRoot)) {
searcher.Filter = "(&(objectCategory=group))";
var results = searcher.FindAll();
results.Dump();