Skip to content

Instantly share code, notes, and snippets.

@mkhan45
mkhan45 / Dockerfile
Last active December 10, 2021 21:31
simple HTTP RustScript endpoint
FROM hexpm/elixir:1.13.0-erlang-24.0.3-alpine-3.14.0
RUN mix local.hex --force
RUN mix local.rebar --force
COPY --from=mkhan45/rustscript /bin/rustscript /bin/rustscript
COPY server.exs .
# hacky way to get elixir deps
RUN sh -c "timeout 30s elixir server.exs"
#define MINERAL_LEN (50)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct rock {
char mineral[MINERAL_LEN];
float mass;
@mkhan45
mkhan45 / fancy_assert.rs
Created October 20, 2021 05:00
fancy assert
macro_rules! function {
() => {{
fn f() {}
fn type_name_of<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
let name = type_name_of(f);
&name[..name.len() - 3]
}}
}
@mkhan45
mkhan45 / ttable.hs
Created August 30, 2021 02:12
Truth Table Generator
import Data.List (intercalate)
import Prelude hiding (or, and)
toBinArray :: Int -> [Bool]
toBinArray = reverse . helper
where helper :: Int -> [Bool]
helper 0 = [False]
helper n
| n `div` 2 == 0 = [(n `mod` 2) /= 0]
| otherwise = ((n `mod` 2) /= 0) : helper (n `div` 2)
# Configuration for Alacritty, the GPU enhanced terminal emulator.
# Any items in the `env` entry below will be added as
# environment variables. Some entries may override variables
# set by alacritty itself.
#env:
# TERM variable
#
# This value is used to set the `$TERM` environment variable for
# each instance of Alacritty. If it is not present, alacritty will
@mkhan45
mkhan45 / euler.apl
Last active June 13, 2021 18:46
APL Euler
⍝ Problem 1
{+/(∨⌿0=(3 5)∘.|⍵)/⍵}⍳999
⍝ ⍳999 is [1..999]
⍝ {...} is a function which is applied to [1..999]
⍝ a/b where a and b are vectors of the same length replicates
⍝ each index of b by the equivalent index of a. Here it acts as a filter since each value in a is either 0 or 1
⍝ (3 5)∘.|⍵ is the dot product of (3 5) with w using | (residue) as the operator. | is like backwards remainder in this case
⍝ 0= compares the remainder matrix with 0
@mkhan45
mkhan45 / expr.c
Created April 27, 2021 05:52
simple expr eval in c
#include <stdio.h>
#include <stdlib.h>
void free_expr();
int eval_expr();
typedef enum expr_ty {
Atomic,
Binary,
} expr_ty;
@mkhan45
mkhan45 / expr.cpp
Last active April 27, 2021 04:36
simple expr eval in cpp
#include <iostream>
#include <memory>
enum BinOp {
Add,
Sub,
Mul,
Div,
};
@mkhan45
mkhan45 / demo.bytecode
Last active March 13, 2021 19:57
A super simple bytecode VM
Push 0
Push 0
-- accumulator is 0
-- index is 1
-- adds index to accumulator
Get 0
Get 1
Add
@mkhan45
mkhan45 / linkedlist.hs
Last active March 11, 2021 18:38
Data.List is already a LinkedList
import Data.Semigroup
data Node a -- a Node containing type a is
= Val a (Node a) -- either a Value of type a and another Node
| End -- or the end of the list
-- Makes it possible for REPL to display a linked list
-- Val 5 (Val 3 (Val 9 End)) becomes 5->3->9->()
instance (Show a) => Show (Node a) where
show (Val a next) = (show a) <> "->" <> (show next)