Skip to content

Instantly share code, notes, and snippets.

View sampersand's full-sized avatar

Sam Westerman sampersand

  • CA
View GitHub Profile
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;
# 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
@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 }}
}
@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
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|
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+ }
@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; \
@sampersand
sampersand / cursed.rb
Last active January 28, 2022 09:06
Determine if the inptu contains all alphabetical letters
#!ruby -W0anF(?=) -rstringio
# Prompt: Create a program that can check to see if an input
# (given any way you want) contains all the letters in the
# alphabet, case-insensitive. Note that the input string will
# only be ascii, but it may contain non-alphabetic letters.
# Make it cursed.
END{p$====032}
BEGIN{$_=$0.tr *%w*A-Z a-z*; sub $&,% while/\W|\d|_/; $stdin=StringIO.new$_}
class Literal {
constructor(value) { this.value = value; }
run(env) { return this; }
toInteger() { return parseInt(this.value); }
toString() { return "" + this.value; }
toBoolean() { return !!this.value; }
}
class Variable {
constructor(name) { this.name = name; }
@sampersand
sampersand / parser.rb
Created May 13, 2022 19:02
basic ruby friar parser
class ParseError < RuntimeError; end
class Token
attr_reader :value, :kind, :location
def initialize(value, kind, location)
@value = value
@kind = kind
@location = location
end