Skip to content

Instantly share code, notes, and snippets.

@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
@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)
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 / 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)
@RoyalIcing
RoyalIcing / 1.ts
Last active November 18, 2022 05:44
JavaScript and TypeScript: two worlds run at different times
// Constants are values with names.
// Functions accept values and return values.
const favoriteNumber = 7
const favoriteNumber2 = (n: number) => n * 2
const doubleNumber = (n: number) => n * 2
const a = doubleNumber(favoriteNumber)
// = 14
@RoyalIcing
RoyalIcing / traffic-lights.jsx
Last active June 9, 2022 12:54
Yieldmachine + React
import { start, on } from "https://unpkg.com/yieldmachine@0.5.1/dist/yieldmachine.module.js"
function TrafficLightsMachine() {
const { Green, Yellow, Red } = {
*Green() {
yield on("timer", Yellow);
},
*Yellow() {
yield on("timer", Red);
},
@RoyalIcing
RoyalIcing / react-simple-model.js
Last active June 2, 2022 11:40
State Machine for simplified version of React’s rendering model
export function* ReactComponent() {
function* Initial() {
yield on("render", Rendering);
}
function* Rendering() {
yield on("resourceSuspended", Suspended);
yield on("error", Errored);
yield on("commit", Committed);
}
function* Suspended() {
@RoyalIcing
RoyalIcing / example.js
Created June 2, 2022 10:59
yieldtable
const Key = primaryKeyText('key');
const Count = column('count', int(1));
const CountersTable = table('counters', [Key, Count]);
function* Create() {
yield create(CounterTable);
}
function* QueryCountFor(key) {
return select([Count], [where(Key(key))]);
// See: https://twitter.com/buildsghost/status/1529258931809202176
// And: https://twitter.com/lewisl9029/status/1529360924343095296
let db
function allProps(object) {
Object.fromEntries(
await Promise.all(
Object.entries(object).map(async ([key, promise]) => {
return [key, await promise]
@RoyalIcing
RoyalIcing / filter-machine.js
Last active April 14, 2022 07:37
Filter with toggling machine
export function* OpenFilter() {
function* Closed() {
yield on("First", First);
yield on("Second", Second);
}
function* First() {
yield on("First", Closed);
yield on("Second", Second);
}
function* Second() {