Skip to content

Instantly share code, notes, and snippets.

View baetheus's full-sized avatar

Brandon Blaylock baetheus

View GitHub Profile
@baetheus
baetheus / range.ts
Created May 3, 2024 16:44
Callbag-ish Range
export function range(count: number, start = 0, step = 1): Stream<number> {
return (snk) => {
let open = true;
let index = Math.floor(Math.max(0, count));
let value = start;
let readyCount = 0;
let pulling = false;
const close = () => open = false;
const talk = snk((count) => {
@baetheus
baetheus / transformers.ts
Last active November 28, 2023 04:15
Monad Transformers in fun
import type { $, In, InOut, Kind, Out } from "../kind.ts";
import type { Option } from "../option.ts";
import type { Flatmappable } from "../flatmappable.ts";
import type { Fn } from "../fn.ts";
import * as F from "../fn.ts";
import { createBind, createTap } from "../flatmappable.ts";
import { createBindTo } from "../mappable.ts";
import { pipe } from "../fn.ts";
@baetheus
baetheus / ideas.ts
Created November 26, 2023 18:00
Idea for Route/Router types in pick
/**
* Here are some ideas for router and route. Handler is already set as an async
* indexed state monad.
*
* ```ts
* export type Handler<D, A, B> = (d: D) => Promise<[A, B]>;
* ```
*/
import type { Option } from "fun/option.ts";
@baetheus
baetheus / example.ts
Last active October 23, 2023 03:58
Basic Router
/** @jsx h */
import { pipe } from "fun/fn.ts";
import { h } from "https://esm.sh/[email protected]";
import * as R from "../router.ts";
import { jsx } from "../jsx.ts";
type State = { count: number };
@baetheus
baetheus / const.ts
Created January 4, 2023 18:42
Const Types in Deno Doc
export const apply1 =
<A>(ua: Promise<A>) => <I>(ufai: Promise<(a: A) => I>): Promise<I> =>
Promise.all([ua, ufai]).then(([a, fai]) => fai(a));
export const apply2: <A>(
ua: Promise<A>,
) => <I>(ufai: Promise<(a: A) => I>) => Promise<I> = apply1;
@baetheus
baetheus / curry.ts
Last active January 4, 2023 18:35
Testing Deno Doc Gen
import type { $, Kind } from "https://deno.land/x/fun/kind.ts";
import type { Applicative } from "https://deno.land/x/fun/applicative.ts";
import { pipe } from "https://deno.land/x/fun/fn.ts";
export function curry2<A, B, C>(f: (a: A, b: B) => C): (a: A) => (b: B) => C {
return (a) => (b) => f(a, b);
}
export function curry3<A, B, C, D>(
@baetheus
baetheus / bench.txt
Created December 31, 2022 01:21
Pipe and Flow benchmarks
cpu: Apple M1 Max
runtime: deno 1.29.1 (aarch64-apple-darwin)
file:///Users/brandon/src/fun/main/benchmarks/fn.bench.ts
benchmark time (avg) (min … max) p75 p99 p995
------------------------------------------------------------- -----------------------------
flowOptimized 262.67 ns/iter (251.3 ns … 443.08 ns) 259.91 ns 312.58 ns 341.9 ns
flowReduceConst 404.92 ns/iter (395.69 ns … 504.78 ns) 402.08 ns 504.4 ns 504.78 ns
flowReduceFunction 510.3 ns/iter (504.26 ns … 540.63 ns) 510.64 ns 539.74 ns 540.63 ns
@baetheus
baetheus / lazy_fn.ts
Last active December 11, 2022 00:18
Stack Safe Reader in TypeScript?
// deno-lint-ignore-file no-explicit-any
/**
* I think this is a stack safe implementation of Fn/Reader..
*
* I haven't really tested it though..
*
* The core idea here is to build up an array of "Controls"
* that represent a few core operations over a LazyFn. To
* keep things simple we only implement controls for id,
@baetheus
baetheus / redis.ts
Last active November 6, 2022 18:41
Quick Hack of Redis Client
import type { Either } from "fun/either.ts";
import type { Option } from "fun/option.ts";
import type { Hold } from "fun/kind.ts";
import * as A from "fun/array.ts";
import * as E from "fun/either.ts";
import * as O from "fun/option.ts";
import { flow, pipe, todo } from "fun/fn.ts";
@baetheus
baetheus / freer.ts
Created November 1, 2022 23:08
Freer Monads Take 1
/**
* See
* * https://okmij.org/ftp/Computation/free-monad.html
* * https://github.com/gcanti/fp-ts-contrib/blob/master/src/Free.ts
* * https://reasonablypolymorphic.com/blog/freer-monads/
* * https://serokell.io/blog/introduction-to-free-monads
*/
import { flow, identity, pipe, todo } from "https://deno.land/x/fun/fn.ts";