Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
mlhaufe / cyclicPerms.js
Last active May 12, 2021 14:07
Permutations
// cyclicPerms(5)
// "1,2,3,4,5
// 2,3,4,5,1
// 3,4,5,1,2
// 4,5,1,2,3
// 5,1,2,3,4"
const cyclicPerms = (n) =>
Array.from({length: n},(_,i) => i + 1)
.map((_, i, xs) => [...xs, ...xs].slice(i, i+n))
.join('\n')
@mlhaufe
mlhaufe / htmlTitleList.vbs
Created January 6, 2012 00:48
Getting html details (VBScript)
'Reference: <http://www.visualbasicscript.com/tm.aspx?high=&m=95638&mpage=1>
Option Explicit
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim files : Set files = fso.GetFolder("C:\test\").Files
Dim file
For Each file In files
If file.Type = "Firefox Document" Then '<-- update for your system
Dim doc : Set doc = CreateObject("htmlfile")
doc.write fso.OpenTextFile(file.Path).ReadAll()
@mlhaufe
mlhaufe / bool.ts
Created July 21, 2020 19:13
Boolean control structures
interface Boolean {
ifTrue<T>(fn: () => T): T | undefined
ifFalse<T>(fn: () => T): T | undefined
}
Object.assign(Boolean.prototype, {
ifTrue(fn: Function) {
return this ? fn.apply(this) : undefined
},
ifFalse(fn: Function) {
@mlhaufe
mlhaufe / databinding.ts
Last active July 3, 2020 22:50
TypeScript databinding
function Bindable(proto: any, name: PropertyKey) {
const desc = Object.getOwnPropertyDescriptor(proto, name)
delete proto.name
if ((proto['_dispatchEvent']) == undefined) {
Object.defineProperty(proto, '_dispatchEvent', {
value(event: Event) {
}
@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 / uMVC.ts
Last active October 5, 2019 18:18
TypeScript uMVC based on <https://github.com/petermichaux/uMVC>
abstract class Observer {
abstract update(data?: any): void
}
abstract class Observable {
protected _observers: Observer[] = []
observe(observer: Observer) {
if (this._observers.indexOf(observer) > -1)
throw new Error('Observer already added')
@mlhaufe
mlhaufe / simulating-generics.ts
Last active July 8, 2019 01:37
Simulating Generics w/ Inheritance in TypeScript.
abstract class Comparable {
abstract le(other: this): boolean
min(other: this): Comparable {
return this.le(other) ? this : other
}
}
abstract class Ring {
constructor(
readonly zero: Ring,
@mlhaufe
mlhaufe / arith-bool.js
Created January 8, 2019 06:20
Booleans with arithmetic
// x, y :: {0,1}
var and = (x,y) => x * y,
not = (x) => 1 - x,
or = (x,y) => 1 - (1 - x) * (1 - y)
and(0,0) // 0
and(0,1) // 0
and(1,0) // 0
and(1,1) // 1
@mlhaufe
mlhaufe / README.txt
Created December 10, 2018 05:39
A half assed, buggy implementation of a Tiny File System. Not useful for anything as is.
- The application is currently single threaded
- The following commands have been implemented:
- exit
- create <PATH>
- open <PATH>
- display
- ls
- cd <TFS> (partial)
- mkdir <TFS> (partial). currently buggy due to nibble manipulation
- import <PATH> <TFS> (partial) stub + error checking only
function main() {
console.log("question1(0) == 0");
console.assert(question1(0) == 0, `${question1(0)}`);
console.log("question1(1) == 1");
console.assert(question1(1) == 1, `${question1(1)}`);
console.log("question1(7) == 13");
console.assert(question1(7) == 13, `${question1(7)}`);
console.log("question1(12) == 144");
console.assert(question1(12) == 144, `${question1(12)}`);