Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
ClarkeRemy / coulomb_auxiliary.rs
Created October 16, 2024 06:42
Yet another defunctionalization exercise from a friend's code
// this is just to satisfy the type system as I don't have the function
mod boys { pub mod micb25 { pub fn boys(_:u64,_:f64)->f64 {1.0} }}
#[derive(Clone, Copy)]
pub struct Vector3<T> {x:T,y:T,z:T}
impl Vector3<f64> {
fn norm_squared(&self)->f64 {self.x*self.x+self.y*self.y+self.z*self.z}
}
// direct style
@ClarkeRemy
ClarkeRemy / formula.rs
Last active October 23, 2024 04:19
Compile Time Boolean formula Interpreter
use std::marker::PhantomData;
enum Ast{};
enum Eval{};
trait Mode {}
impl Mode for Ast {}
impl Mode for Eval {}
@ClarkeRemy
ClarkeRemy / hermite.rs
Last active October 15, 2024 15:34
Hermite expansion defunctionalization for a friend
// original
// fn hermite_expansion(i: i32, j: i32, t: i32, dist: f64, a: f64, b: f64) -> f64 {
// let p = a + b;
// let q = a * b / p;
// if t < 0 || t > i + j {
// 0.0
// } else if i == j && j == t && t == 0 {
// f64::exp(-q * dist.powi(2))
@ClarkeRemy
ClarkeRemy / dict.sml
Last active October 16, 2024 08:40
Dictionary Passing style
structure Plus = struct
datatype 'a t = T of 'a -> 'a -> 'a
end
structure Mul = struct
datatype 'a t = T of 'a -> 'a -> 'a
end
structure FromInt = struct
@ClarkeRemy
ClarkeRemy / subtratorial.sml
Last active October 28, 2024 00:25
Defunctionalization Subtractorial
(* direct style *)
fun substractorial n =
if n < 0
then 0
else n - substractorial (n-1)
(* Continuation passing style *)
(* In a functional language, when every branch is a "tail call", it optimizes to a jumps *)
(* this could already be turned into a loop pretty easily,
@ClarkeRemy
ClarkeRemy / oop.js
Created September 4, 2024 07:37
Object oriented code JS manual
const interface_method = (object, _interface)=>(method) => (
object._interface[_interface].hasOwnProperty(method)
&& (typeof object._interface[_interface][method]) == "function"
)
const implInterface = (_interface, invariant, object, impl) => {
if (!object.hasOwnProperty("_interface")) {
object._interface = {};
}
object._interface[_interface] = impl
if (!invariant(object)) {throw `FAILED TO IMPLEMENT \`${_interface}\``}
@ClarkeRemy
ClarkeRemy / recursion_schemes.rs
Last active August 30, 2024 20:52
Rust Recursion schemes
#![allow(unused)]
use std::{borrow::Borrow, convert::Infallible};
trait Functor {
type F<T>;
fn fmap<A, B>(f: impl Fn(A) -> B, x: Self::F<A>) -> Self::F<B>;
}
trait Rec: Sized {
type Fix;
@ClarkeRemy
ClarkeRemy / recursion_schemes.sml
Last active August 16, 2024 12:58
Recursion Schemes SML extended
signature FUNCTOR = sig
type 'a F
val fmap : ('a -> 'b) -> 'a F -> 'b F (* F is a category theory functor *)
end
signature FUNCTOR_FIX = sig
include FUNCTOR
type fix (* The Fixpoint type *)
val prj : fix -> fix F (* Recursive *)
@ClarkeRemy
ClarkeRemy / main.rs
Created August 8, 2024 17:56
Recursion Schemes in Rust (naive)
trait Rec : Sized{
type F<T>;
fn fmap<A,B>(f : impl Fn(A)->B, x : Self::F<A>) -> Self::F<B>;
fn prj(t : Self)->Self::F<Self>;
fn inj(t : Self::F<Self>)->Self;
}
fn fold<R : Rec, A>(step : &impl Fn(R::F<A>)->A, x : R )->A {
@ClarkeRemy
ClarkeRemy / main.rs
Last active August 17, 2024 12:28
Flatten State Machine
fn flatten1(t: BinaryTree) -> Vec<i32> {
match t {
BinaryTree::Leaf(leaf) => Vec::from([leaf]), /* ret */
BinaryTree::Branch((l, r)) => {
let mut left /* ret */ = flatten1(*l); //<- rec remember!(r)
let right/* ret */ = flatten1(*r); //<- rec remember!(left)
left.extend(right);