Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
mlhaufe / expression-problem.mts
Created September 8, 2023 00:42
Trivial OO Expression Problem Solved in TypeScript
// See the following for a Scala example: <https://i.cs.hku.hk/~bruno/papers/Modularity2016.pdf>
/* Initial code */
abstract class Exp { abstract evaluate(): number }
class Lit extends Exp {
constructor(readonly x: number){ super(); }
evaluate(): number { return this.x }
}
class Add extends Exp {
constructor(readonly left: Exp, readonly right: Exp) { super() }
@mlhaufe
mlhaufe / ShellBrowse.vbs
Last active August 19, 2023 19:28
VBScript File Browser
Option Explicit
' Flags for the options parameter
Const BIF_returnonlyfsdirs = &H0001
Const BIF_dontgobelowdomain = &H0002
Const BIF_statustext = &H0004
Const BIF_returnfsancestors = &H0008
Const BIF_editbox = &H0010
Const BIF_validate = &H0020
Const BIF_browseforcomputer = &H1000
@mlhaufe
mlhaufe / callcc.js
Created July 13, 2023 22:03
callcc
class ContError extends Error { returnValue = undefined; }
function callcc(fnCont) {
const contErr = new ContError("Unable to continue current continuation.");
const fnEscape = (returnValue) => {
contErr.returnValue = returnValue;
throw contErr;
}
try {
return fnCont(fnEscape);
@mlhaufe
mlhaufe / delimited-continuations.js
Created July 13, 2023 21:41 — forked from yelouafi/delimited-continuations.js
delimited continuations using javascript generators
// We model the call stack using a linked list of Generators
// Each Generator has a _return field pointing back to its parent
function stepGen(gen, arg) {
const {done, value} = gen.next(arg)
if(done) {
if(gen._return) {
stepGen(gen._return, value)
}
@mlhaufe
mlhaufe / TypeLevel.ts
Last active June 10, 2023 16:18
TypeScript Union and Intersection types
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type IntersectionToUnion<I> = (I extends any ? (x: I) => any : never) extends ((x: infer U) => any) ? U : never;
/**
* Zip two tuples together into a tuple of tuples
* @example
* ZipTuple<['name', 'age', 'isActive'], [string, number, boolean]>
* => [["name", string], ["age", number], ["isActive", boolean]]
*/
@mlhaufe
mlhaufe / islanders.ts
Created May 5, 2022 01:42
12 islanders balancing problem
// Random Integer from m to n excluding e
const randInt = (m: number, n: number, e?: number): number => {
const result = Math.floor(Math.random() * m) + n;
return e == undefined ? result :
result == e ? randInt(m, n, e) :
result
}
class Person { constructor(public weight: number) {} }
@mlhaufe
mlhaufe / Functional.ts
Created April 16, 2022 03:37
Expression Problem
/** Number Expressions */
// data
type Exp =
{ tag: 'Lit', value: number } |
{ tag: 'Add', left: Exp, right: Exp }
// operations
function evaluate(exp: Exp): number {
switch (exp.tag) {
@mlhaufe
mlhaufe / parameters.vbs
Created June 19, 2011 12:52
VBScript class constructor parameters
Class Person
Private m_Age
Private m_Name
Public Default Function Init(Name, Age)
m_Name = Name
m_Age = Age
Set Init = Me
End Function
@mlhaufe
mlhaufe / error.txt
Created May 24, 2021 13:57
Magnolia SPAv2 error
Context: <https://groups.google.com/a/magnolia-cms.com/g/user-list/c/MRpHYKkOMhA>
Type Exception Report
Message com.machinezoo.noexception.WrappedException: java.net.ConnectException: Connection refused (Connection refused)
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception