Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / boolExpr.cs
Created May 15, 2019 15:56
BoolExpr eval
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 March 18, 2019 17:08
Expression splicing
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 July 27, 2017 10:25
Node to TreeNode traversal
class Program
{
class Node
{
public int Id;
public Node Parent;
}
class TreeNode
{
@palladin
palladin / dynamicy.cs
Created May 25, 2017 15:27
Y combinator (dynamic)
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 September 25, 2021 17:52
Struct Unions Perf
#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 September 10, 2016 16:21
Generic programming in Cubical type theory
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
@palladin
palladin / perftest.rs
Created June 21, 2016 21:51
Rust perf test
extern crate time;
use std::io;
use time::PreciseTime;
fn main() {
let mut guess: String = String::new();
io::stdin().read_line(&mut guess).expect("failed to read line");
let n: i64 = guess.trim().parse().expect("Please type a number!");
@palladin
palladin / eval.clj
Last active September 25, 2021 17:52
A meta-circular interpreter for Lambda Calculus
(require '[clojure.core.match :refer [match]])
(defn extend [k v l]
(fn [k']
(if (= k k') v (l k'))))
(def empty (fn [k'] (throw (Exception. "oups"))))
(defn eval [e env]
@palladin
palladin / Streamsig.fsx
Last active September 25, 2021 17:52
Stream sig
type Stream<'T> // abstract type
module Stream =
// general combinators
val map : (Expr<'A> -> Expr<'B>) -> Stream<'A> -> Stream<'B>
val collect : (Expr<'A> -> Stream<'B>) -> Stream<'A> -> Stream<'B>
val filter : (Expr<'A> -> Expr<bool>) -> Stream<'A> -> Stream<'A>
val take : Expr<int> -> Stream<'A> -> Stream<'A>
val zipWith : (Expr<'A> -> Expr<'B> -> Expr<'C>) -> Stream<'A> -> Stream<'B> -> Stream<'C>
// ...
@palladin
palladin / regex.fsx
Created December 31, 2015 16:20
A Staged Regular Expression Matcher
// http://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
// http://scala-lms.github.io/tutorials/regex.html
#r "../packages/FSharp.Compiler.Service.1.3.1.0/lib/net45/FSharp.Compiler.Service.dll"
#r "../packages/QuotationCompiler.0.0.7-alpha/lib/net45/QuotationCompiler.dll"
open QuotationCompiler