Skip to content

Instantly share code, notes, and snippets.

@ClarkeRemy
ClarkeRemy / cmp.rs
Last active October 11, 2024 17:54
type level cmp
use std::cmp::Ordering;
type U0 = Zero;
type U1 = Succ<Zero>;
type U2 = Succ<U1>;
type U3 = Succ<U2>;
type U4 = Succ<U3>;
type U5 = Succ<U4>;
fn main() {
@ClarkeRemy
ClarkeRemy / bad_transducer.rs
Created April 2, 2024 08:55
Bad Transducers
use std::{sync::Arc};
#[derive(PartialEq, Eq,Debug, PartialOrd, Ord)]
enum Reduced<A>{
Continue(A),
Reduced(A)
}
#[derive(Clone)]
@ClarkeRemy
ClarkeRemy / dyn_compose.rs
Last active March 4, 2024 13:15
Values must live longer than functions
fn dyn_compose<'val, 'func, In, Out, Middle>(
f: Box<dyn Fn(Middle) -> Out + 'func>,
g: Box<dyn Fn(In) -> Middle + 'func>,
) -> Box<dyn Fn(In) -> Out + 'func>
where
In : 'val,
Middle : 'val,
Out : 'val,
'val : 'func,
@ClarkeRemy
ClarkeRemy / cps_tree_traversal.rs
Created February 20, 2024 05:55
Rust CPS tree traversal
fn main() {
let sub = |x,y| Diff::Sub((Box::new(x), Box::new(y)));
let lit = |x| Diff::Lit(x);
let tree = sub(sub(lit(5.0), lit(2.0)), lit(3.0));
let tree2 = tree.clone();
println!("tree : {:?}\ntree2: {:?}",
@ClarkeRemy
ClarkeRemy / group_by.rs
Last active February 15, 2024 17:58
GroupBy Rust
use core::{marker::PhantomData, sync::atomic::{AtomicBool, Ordering}};
fn main() {
println!("Hello, world!");
let test = *b"aabba";
let group_by_0 = GroupBy::new(test.into_iter(), |&x|x);
for (key, group) in group_by_0 {
println!("key: {} {{", key as char);
@ClarkeRemy
ClarkeRemy / tail_drop_elimination.rs
Last active February 10, 2024 19:14
Initial proposal for Tail-Drop Elimination
///! Proposal for guaranteed Tail-Drop elimination for functions that do not return values.
///!
///! We revisit the `become` keyword limiting it to functions that do not return values
///! Given this constraint, it becomes much easier to implement.
///! A programmer can always supply the place to return to with a continuation anyways quite trivialy.
/// This is why it must be done at a language level.
/// The programmer cannot do this themselves or they would have a stack overflow.
/// If the programmer did try to do it themselves, it would require a very complex, type unsafe way to do it.
/// All parts can be guaranteed to be sound statically
@ClarkeRemy
ClarkeRemy / calc.prolog
Created January 24, 2024 17:04
Prolog S-expr
:- module(calc, [tokens//1, ast//1, ast_eval/2, eval_formatted/2, source_eval/2, source_evalFormatted/2]).
:- use_module(library(clpz)).
:- use_module(library(dcgs)).
:- use_module(library(lists)).
:- use_module(library(charsio)).
:- use_module(library(dif)).
:- use_module(library(debug)).
/** S-expression calculator
@ClarkeRemy
ClarkeRemy / debug.sh
Created January 1, 2024 14:39
gdb breakpoints no line numbers
cargo build && gdb --command="my.gdb" ./target/debug/asm_learn
@ClarkeRemy
ClarkeRemy / thread_local_add.rs
Created December 11, 2023 17:47
Thread Local Add
use core::cell::RefCell;
macro_rules! call {
( $FUNCTION:ident $ARGS:expr => $RETURN:ident) => {
unsafe { $FUNCTION::LOCAL.set(Funct { args: $ARGS, ret: &mut $RETURN }) }
$FUNCTION();
};
}
fn main() -> std::process::ExitCode
@ClarkeRemy
ClarkeRemy / basicbox.rs
Last active November 21, 2023 08:23
Basic Box
#![no_implicit_prelude]
pub(crate) extern crate alloc; // with heap
pub(crate) extern crate core; // fundamental
pub(crate) extern crate std; // OS
fn main() {
let _x = MyBox::new(alloc::vec![4]);
}
pub struct MyBox<T> {