Skip to content

Instantly share code, notes, and snippets.

@mbillingr
mbillingr / tensor_autograd.rs
Last active August 16, 2023 13:48
Automatic differentiation on tensors prototype
use std::collections::HashMap;
use std::rc::Rc;
fn main() {
let x = &Tensor::new(vec![
vec![0.0, 0.0],
vec![1.0, 0.0],
vec![2.0, 0.0],
vec![0.0, 1.0],
vec![1.0, 1.0],
@mbillingr
mbillingr / main.rs
Created July 5, 2023 15:35
a basic evaluator in rust
use std::collections::HashMap;
use std::rc::Rc;
macro_rules! ident_list {
() => {
vec![]
};
($a:ident) => {
vec![stringify!(a)]
};
@mbillingr
mbillingr / mukanren.idr
Last active January 1, 2023 21:40
Implementation of µKanren in Idris
data Value = Null
| Var Nat
| Pair Value Value
| Num Int
data Substitution = Nil
| (::) (Nat, Value) Substitution
State : Type
@mbillingr
mbillingr / parsing.idr
Created December 28, 2022 13:45
playing with idris' parser
import Text.Lexer.Core
import Text.Lexer
import Text.Parser.Core
import Text.Parser
data ExpressionToken = Number Integer
| Operator String
| OParen
@mbillingr
mbillingr / main.py
Created December 13, 2022 13:16
Simulate belt balancers (Factorio)
import itertools
class Entity:
def __init__(self):
entities.append(self)
class Lane(Entity):
def __init__(self):
super().__init__()
@mbillingr
mbillingr / scanner.rs
Created October 27, 2022 14:17
Table based scanner
use std::collections::HashMap;
use std::hash::Hash;
pub type Result<T> = std::result::Result<T, Span>;
pub struct Scanner<'i, T: Clone> {
classifier_table: DefaultTable<char, usize>,
transition_table: Table<usize, Vec<usize>>,
accepting_states: Table<usize, T>,
input: &'i str,
@mbillingr
mbillingr / lamcalc1.idr
Last active October 11, 2022 10:10
Simple Lambda calculus evaluator in Idris
-- evaluation using environments
mutual
data Lc = Ident String
| Lambda String Lc
| Call Lc Lc
| Closure Env String Lc
data Env = Empty | Entry String Lc Env
@mbillingr
mbillingr / main.py
Created August 2, 2022 11:21
Memory/Pointer simulation in Python
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Literal, Any
BYTEORDER: Literal["little", "big"] = "big"
class Type(ABC):
@abstractmethod
def load(self, offset, memory):
@mbillingr
mbillingr / fieldmodel.py
Last active May 24, 2022 10:51
Model the electrical potential on a finite grid of resistors... and use it to naively solve xkcd356
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
try:
import seaborn
except ImportError:
pass
class FieldModel:
@mbillingr
mbillingr / Bin.idr
Created April 20, 2022 14:34
Binary numbers in Idris
module Bin
import Data.Nat
data Bit = O | I
Eq Bit where
(O == O) = True
(I == I) = True
(_ == _) = False