Skip to content

Instantly share code, notes, and snippets.

@RoyalIcing
RoyalIcing / makingMapsSucks.js
Last active September 27, 2023 06:56
Parsing JSON to Map
// Hard to read nested arrays.
const c = new Map([['a', 1], ['b', 2]])
console.log(c)
// Have to write using object wrapped in two layers of calls.
const d = new Map(Object.entries({ a: 1, b: 2}))
console.log(d)
const undefinedValue = Symbol();
export class MapWithFallback<Key, Value> {
#map: Map<Key, Value>;
#fallback: Value;
constructor(fallback: Value) {
this.#map = new Map();
this.#fallback = fallback;
}
@RoyalIcing
RoyalIcing / parse_u8.ex
Last active December 2, 2023 01:34
Lemire’s Parsing 8-bit integers quickly in WebAssembly via Orb. https://lemire.me/blog/2023/11/28/parsing-8-bit-integers-quickly/
defmodule ParseU8 do
@moduledoc """
https://lemire.me/blog/2023/11/28/parsing-8-bit-integers-quickly/
"""
# Orb lets you write WebAssembly with friendly Elixir syntax: https://github.com/RoyalIcing/Orb
use Orb
Memory.pages(1)
wasm_mode(U32)
@RoyalIcing
RoyalIcing / youtube_url_parser.wat
Created March 18, 2024 05:50
YouTube Parser with Orb
(module $YouTubeURLParser
(memory (export "memory") 1)
(global $YouTubeURLParser.Input.bump_offset (mut i32) (i32.const 0))
(func $YouTubeURLParser.Input.alloc! (param $byte_count i32) (result i32)
(local $new_ptr i32)
(global.get $YouTubeURLParser.Input.bump_offset)
(local.set $new_ptr)
(i32.gt_u (i32.add (local.get $new_ptr) (local.get $byte_count)) (i32.const 65536))
(if
(then
import { setState } from "react";
class SharedState<State> extends EventTarget {
#state: State;
constructor(state: State) {
super();
this.#state = state;
}
get current() {
return this.#state;