Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
mlhaufe / highlight.js
Last active December 17, 2015 12:19
Highlight text in an html document
function selected(){
var sel = window.getSelection(),
cnt = sel.rangeCount,
ranges=[];
for(var i = 0; i < sel.rangeCount; i++)
ranges[i] = sel.getRangeAt(i);
return ranges
}
function highlight(node,sels){
@mlhaufe
mlhaufe / Bool def.lps
Last active December 18, 2015 08:29
Primarily a brainstorming exercise to test out the feel of the language design thus far.
module
exports #Bool #False #True
def Bool
def False <| Bool
and False -> False
and True -> True
or False -> False
or True -> True
@mlhaufe
mlhaufe / emailspider.js
Last active December 19, 2015 07:09
quick and dirty WScript email crawler usage: from command prompt: "cscript foo.js"
var url = "http://thenewobjective.com/blog/",
email = /\b[A-Z0-9+_.-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}\b/gi,
delay = 2000,
visited = {}, hostname,
baseDocument = new ActiveXObject("htmlfile"),
a = baseDocument.createElement("a");
a.href = url;
hostname = a.hostname;
@mlhaufe
mlhaufe / makeChange.js
Created April 9, 2014 04:41
Greedy Change Making Algorithm
var qapik = [50,20,10,5,3,1],
usCoins = [25,10,5,1];
//denominations (ds) must be in descending order
function makeChange(coins,ds){
var result = {}
for(var i = 0, d = ds[i]; i < ds.length; d = ds[++i]){
result['n'+d] = Math.floor(coins/d) // a div d
coins = coins % d
}
@mlhaufe
mlhaufe / maxSubsequence.js
Created April 16, 2014 05:06
Max Subsequence Sum
var xs = [-4,-3,-7,+2,+1,-1,-4], //[2,1] = 3
ys = [-2,11,-4,13,-5,-2], //[11,-4,13] = 20
zs = [5,15,-30,10,-5,40,10,-8] // [...] = 55
function sum(j,as){
var max = Math.max;
function tabulate(i,runMax,curMax){
if(i > j) return curMax
var newMax = max(runMax+as[i],as[i])
return tabulate(i + 1, newMax, max(curMax,newMax))
@mlhaufe
mlhaufe / countInversions.js
Created April 16, 2014 08:40
Count inversions in an array
function countInversions(xs){
var count = 0;
function mergeSort(xs){
var size = xs.length
if (size < 2) return xs
var mid = Math.floor(size / 2),
left = xs.slice(0, mid),
right = xs.slice(mid)
@mlhaufe
mlhaufe / TFS1
Last active August 29, 2015 14:01
Tiny File System
04425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E2500000004425248450000000000001E25000000
@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();
<!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 / 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