Skip to content

Instantly share code, notes, and snippets.

View laat's full-sized avatar
💤

Sigurd Fosseng laat

💤
View GitHub Profile
const sleep = (s) =>
new Promise((resolve) =>
setTimeout(() => resolve(s), s * 1000)
);
const sleepSort = async (arr) => {
const res = [];
await Promise.all(
arr
.map(sleep)
const arr = [1, 4, 4, 54, 56, 54, 2, 23, 6, 54, 65, 65];
const reduceSum = arr => arr.reduce((acc, val) => acc + val);
const forEachSum = arr => {
let sum = 0;
arr.forEach(el => sum += el);
return sum;
};
const forSum = arr => {
let sum = 0;
for (let i = 0; i < arr.length; i++ ) sum += arr[i];
@laat
laat / sse.http2.ts
Last active February 9, 2022 09:40
Server-Sent Events over HTTP/2 on Node.JS
Relays messages published on a redis channel over Server-Sent Events.
import mime from "mime";
import { stat } from "node:fs/promises";
import http2, { Http2ServerRequest, Http2ServerResponse } from "node:http2";
import { join } from "node:path";
export type Http2Handler = (
next?: (
req: Http2ServerRequest,
res: Http2ServerResponse
) => Promise<void> | void

Invert Binary Tree in F#

graph graph-inverted

An inverted Binary Tree is a Binary Tree with left and right children of all non-leaf nodes interchanged.

type Node =
  { Value: int
@laat
laat / episodes_in_wrong_series.sql
Last active May 14, 2021 19:02
Episodes in wrong TV Series.
WITH
external_data
/*
In an external system, we have some episode to series relations.
An episode can be in multiple series.
The SERIES_ID is monotonically increasing database ID.
*/
AS (SELECT 10 SERIES_ID, 1 EPISODE_ID FROM dual UNION ALL
SELECT 2 SERIES_ID, 1 EPISODE_ID FROM dual),
transformed_data
@laat
laat / 1-json-schema.fsx
Last active October 13, 2022 20:54
JsonSchema.Net validation in F#
#!/usr/bin/env -S dotnet fsi --quiet
#r "nuget: JsonSchema.Net"
//
// This file contains a basic example of json schema validation
//
open Json.Schema
open System.Text.Json
let schema =

The times macro

(defmacro laat/times (n fn arg) 
  (cons 'thread-last (cons arg (cl-loop for i below n collect fn))))
(laat/times 3 sqrt 256)
@laat
laat / aoc.org
Created December 1, 2020 23:03
org-mode and elisp

Advent Of Code 2020

Using org-mode and Emacs Lisp to solve Advent of Code.

(org-babel-lob-ingest "./library-of-babel.org")

Day 1

Part 1

@laat
laat / Nullable.ts
Last active February 8, 2020 15:40
rxjs nullable modad-ish
import { Observable, of } from "rxjs";
import { map, switchMap } from "rxjs/operators";
export class Nullable {
static isNotNull<T>(v: T): v is NonNullable<T> {
return v != null;
}
static bind = <T, R>(
project: (value: NonNullable<T>, index: number) => R