Skip to content

Instantly share code, notes, and snippets.

@monzee
monzee / fowler.ts
Created March 30, 2021 11:03
Fowler GUI Architectures running example - https://www.martinfowler.com/eaaDev/uiArchs.html
/// <reference path="./mvvm.ts" />
namespace Fowler {
export function mvvm(
ids: Dict<string> & { stations: string },
dataSet: Reading[]
): Promise<never> {
return connect(
appOf(dataSet),
@monzee
monzee / login.ts
Last active March 3, 2021 14:21
MVx pattern, not-invented-here edition
/// <reference lib="es2015" />
type Visitor<R, V> = {
[K in keyof V]: V[K] extends any[]
? (...pattern: V[K]) => R
: never;
};
type Sum<V> = <R>(visitor: Visitor<R, V>) => R;
@monzee
monzee / example.scm
Last active May 27, 2020 07:11
Scheme section sequence macros (aka threading macros in clojure) with haskell-style 'where clause
(define (hex->dec str)
(>> (string-fold add-next 0 str)
:where ; deferred declaration of local bindings
(char-values (map cons (string->list "0123456789abcdef") (iota 16)))
#((add-next digit acc) ; lambda binding shorthand in :where clause
(>> (assv-ref char-values digit)
(or (error "invalid hex digit" digit str))
(+ (* 16 acc))))))
(define (dec->hex num)
@monzee
monzee / list.ts
Created April 25, 2020 05:36
Church-encoded sum types in typescript
type Sum<K> = <T> (cases: Pattern<K, T>) => T;
type Pattern<K, T> = {
[C in keyof K]?: K[C] extends any[] ? (...args: K[C]) => T : never;
} & {
otherwise?: () => T
}
type List<T> = Sum<{nil: [], cons: [T, List<T>]}>;
function fallback<T>(): T {
package em.zed.literallynothing
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
@monzee
monzee / LongDistance.java
Last active June 8, 2017 02:20
somebody's homework
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.StreamSupport;
public class LongDistance {
public static void main(String[] args) throws IOException {
@monzee
monzee / MainActivity.java
Created May 22, 2017 02:51
Java vs. Kotlin
public class MainActivity extends AppCompatActivity {
static class Retained {
final LipsumAdapter adapter = new LipsumAdapter();
State loadState = State.Case::unknown;
}
interface State {
void match(Case of);
@monzee
monzee / Adt.java
Last active April 22, 2017 04:32
ADT codegen reference
import java.util.function.Function;
public interface Adt {
static void main(String[] args) {
map(OptionOf.some(100), String::valueOf).match(new Option<String>() {
@Override
public void none() {
System.out.println("NONE!");
}
@monzee
monzee / monad.kt
Last active March 9, 2017 10:54
Monadic comprehension in Kotlin via suspending blocks
import kotlin.coroutines.experimental.*
fun main(vararg args: String) {
safeDiv(None(), Some(1)).let(::println)
safeDiv(Some(5L), Some(0.5)).let(::println)
safeDiv(Some(1), None()).let(::println)
for (x in 20..30 step 5) {
for (y in 15 downTo 0 step 5) {
print("$x / $y = ")
@monzee
monzee / Monads.java
Last active February 15, 2017 01:17
Monad interfaces for IO<T>, Option<T>, Either<L, R> and combined IO<Option<T>> (Job<T>) and IO<Either<L, R>> (Task<L,R>)
package m;
import java.util.function.*;
public class Monads {
static <T, U> Function<Option<T>, Option<U>>
lift(Function<T, U> t2u) {
return ot -> pu -> ot.map(t2u).match(pu);
}