Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / Index.cs
Last active August 14, 2023 11:18
Composable Index
using System.Linq;
public static class Index
{
public static Index<TKey, TValue> Create<TKey, TValue>()
where TKey : notnull
{
var keyMap = new Dictionary<TKey, HashSet<TValue>>();
Action<TKey, TValue> add = (key, value) =>
@palladin
palladin / Eff.cs
Created July 12, 2023 19:54
Eff with static abstract members
using Nessos.Effects;
using Nessos.Effects.Handlers;
public class ConsolePrintEffect : Effect
{
public string? Message { init; get; }
}
@palladin
palladin / dropwhile.fs
Last active July 4, 2023 12:41
dropWhile using foldr
let dropWhile : ('a -> bool) -> list<'a> -> list<'a> = fun pred xs ->
let (_, ys) = List.foldBack (fun x (xs, ys) -> let xs' = x :: xs in if pred x then xs', ys else xs', xs') xs ([], [])
ys
// example 1
[1..10] |> dropWhile (fun x -> x <= 5) // [6..10]
// example 2
[1..10] |> dropWhile (fun x -> false) // [1..10]
@palladin
palladin / powerset.ts
Created June 2, 2022 14:06
Type level PowerSet in TypeScript
type Append<T, TArray> = TArray extends [infer TCurrent, ...infer Rest] ?
TCurrent extends any[] ? [[T, ...TCurrent], ...Append<T, Rest>] : never
: []
type PowerSet<TArray extends any[]> =
TArray extends [infer TCurrent, ...infer Rest] ? [...Append<TCurrent, PowerSet<Rest>>, ...PowerSet<Rest>] : [[]]
type Test = PowerSet<[1, 2, 3]> // [[1, 2, 3], [1, 2], [1, 3], [1], [2, 3], [2], [3], []]
@palladin
palladin / knowthyself.js
Created July 28, 2021 19:00
Know thyself
let fact = `(f => n => (n === 0) ? 1 : n * f(n - 1))`
function Y(g : string) : (any) => any {
let f = `(f => ${g}(x => eval(f)(f)(x)))`
return eval(f)(f)
}
console.log(Y(fact)(10))
@palladin
palladin / knowthyself.cs
Created July 27, 2021 14:51
Know thyself
Expression<Func<Expression, Func<int, int>>> f =
self => x => ((Expression<Func<Expression, Func<int, int>>>)self).Compile()(self)(x + 1);
var x = f.Compile()(f)(0);
@palladin
palladin / UnFold.cs
Last active July 6, 2020 12:27
IAsyncEnumerable UnFold
static async IAsyncEnumerable<T> UnFold<T, TAcc>(Func<TAcc, Task<(bool Next, IAsyncEnumerable<T> Values, TAcc Acc)>> f, TAcc seed)
{
var acc = seed;
var result = default((bool Next, IAsyncEnumerable<T> Values, TAcc Acc));
do
{
result = await f(acc);
await foreach (var value in result.Values)
yield return value;
@palladin
palladin / maybe.cs
Last active September 25, 2021 17:42
Maybe monad
// https://blog.neteril.org/blog/2017/04/26/maybe-computation-expression-csharp/
[AsyncMethodBuilder(typeof(MaybeAsyncMethodBuilder<>))]
interface Option<T> { }
// Could use the closed type hierarchy Roslyn feature
// to be an approximation of a discriminated union
// https://github.com/dotnet/csharplang/issues/485
sealed class None<T> : Option<T> { public static readonly None<T> Value = new None<T>(); }
sealed class Some<T> : Option<T>
{
@palladin
palladin / output.fs
Created May 22, 2019 12:21
Example3 output
let f (x0 : int[]) (x1 : int[]) =
let x2 = ref 0
let x3 = ref true
let x4 = ref false
let x5 = ref 0
let x6 = ref 0
let x7 = ref true
let x8 = ref true
let x9 = ref 0
let x10 = ref 0
@palladin
palladin / output.fs
Created May 19, 2019 15:43
Stream fusion output
let f (x0 : int []) =
let x1 = ref 0
let x2 = ref true
let x3 = ref false
let x4 = ref 0
let x5 = ref 0
let x6 = ref true
let x7 = ref true
let x8 = ref 0
let x9 = ref 0