Skip to content

Instantly share code, notes, and snippets.

Avatar

Nick Palladinos palladin

View GitHub Profile
@palladin
palladin / UnFold.cs
Last active Jul 6, 2020
IAsyncEnumerable UnFold
View UnFold.cs
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 Sep 13, 2019
Maybe monad
View maybe.cs
// 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
Example3 output
View output.fs
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
Stream fusion output
View output.fs
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
View boolExpr.cs
public interface IBoolExpr { }
public class And : IBoolExpr
{
public IBoolExpr Left { get; set; }
public IBoolExpr Right { get; set; }
public void Deconstruct(out IBoolExpr left, out IBoolExpr right)
{
left = Left;
right = Right;
}
@palladin
palladin / Splicer.cs
Created Mar 18, 2019
Expression splicing
View Splicer.cs
public class Subs : ExpressionVisitor
{
private Dictionary<ParameterExpression, Expression> env;
public Subs(Dictionary<ParameterExpression, Expression> env)
{
this.env = env;
}
protected override Expression VisitParameter(ParameterExpression node)
{
@palladin
palladin / traversal.cs
Created Jul 27, 2017
Node to TreeNode traversal
View traversal.cs
class Program
{
class Node
{
public int Id;
public Node Parent;
}
class TreeNode
{
@palladin
palladin / dynamicy.cs
Created May 25, 2017
Y combinator (dynamic)
View dynamicy.cs
using System;
public class C {
public void M() {
Func<Func<dynamic, dynamic>, dynamic> fd = x => x;
dynamic Y = fd(f => fd(x => f(fd(y => x(x)(y))))(fd(x => f(fd(y => x(x)(y))))));
Y(fd(f => fd(x => f(x))))(42);
}
}
@palladin
palladin / StructUnions.fsx
Last active Feb 20, 2017
Struct Unions Perf
View StructUnions.fsx
#time
[<Struct>]
type ResultStruct<'T, 'TError> =
| OkS of ok : 'T
| ErrorS of error : 'TError
type ResultClass<'T, 'TError> =
| OkC of ok : 'T
| ErrorC of error : 'TError
@palladin
palladin / gp.ctt
Created Sep 10, 2016
Generic programming in Cubical type theory
View gp.ctt
module test where
import prelude
import univalence
data list (A : U) = nil | cons (x : A) (xs : list A)
data bool = false | true
data nat = zero | suc (n : nat)
one : nat = suc zero