Skip to content

Instantly share code, notes, and snippets.

View sampersand's full-sized avatar

Sam Westerman sampersand

  • CA
View GitHub Profile
@sampersand
sampersand / list.c
Last active April 29, 2021 18:33
No more need for linked lists
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <assert.h>
static inline void LIST_NOFREE(void *t) {}
#define LIST_DEFINE(kind, list_name, free_fn) \
typedef struct list_name { \
size_t len, cap; \
kind *eles; \
grammar Syntax {
rule TOP { <expr> .* }
rule expr { <.ws> [<literal> | <function>] }
token ws { [ <[(){}[\]:\s\n]> | '#' \N* \n? ]* }
proto token literal {*}
token literal:sym<identifier> { <[a..z_]> <[a..z0..9_]>* }
token literal:sym<number> { \d+ }
module FP
class MultiMethod
NoMethodMatchesError = Class.new StandardError
def initialize = @methods = []
def add(callable, conditions) = @methods.push([callable, conditions])
def call(...)
@methods.each do |callable, conditions|
@sampersand
sampersand / inquire.rb
Last active February 4, 2021 05:58
Example of my Inquire Gem
require 'inquire'
# An example of my command-line parsing inquire gem
Inquire.new do
version '2.0a', switch: '-V'
app_name 'cURL2'
author 'Sam'
switch '-v', '--[no-]verbose', 'Enables (or disables) verbose mode', default: false
@sampersand
sampersand / rust-in-ruby.rb
Last active March 4, 2024 15:43
Silly little example of abusing ruby's syntax to emulate rust code.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Person {{
age: u32,
name: String
}}
impl Person {
fn new(age: u32, name: String) => Self {
Self {{ age: age, name: name }}
}
# A playing card.
class Card
# All possible suits a card can have
SUITS = %w(Clubs Diamonds Hearts Spades).freeze
# All possible ranks a card can have
RANKS = %w(2 3 4 5 6 7 8 9 10 Jack Queen King Ace).freeze
# Allows us to do `some_card.rank` and `some_card.suit`
attr_reader :rank, :suit
import java.util.Random;
public class FireSim {
public static final int EMPTY = 0;
public static final int BURNING_STATE_1 = 1;
public static final int BURNING_STATE_2 = 2;
public static final int TREE = 3;
public static final int SAPLING = 4;
public static final int LIGHTNING = 5;
public static final int INFLAMMABLE = 6; // small large
public static final int SMOKE_STATE_1 = 7;