Skip to content

Instantly share code, notes, and snippets.

View sampersand's full-sized avatar

Sam Westerman sampersand

  • CA
View GitHub Profile
@sampersand
sampersand / gc.diff
Created September 13, 2023 19:28
GC stress diff
[12:27:12 PM 1029] ruby % git diff
diff --git a/gc.c b/gc.c
index 3fd2a3b33f..f909e5b81b 100644
--- a/gc.c
+++ b/gc.c
@@ -11460,6 +11460,10 @@ gc_stress_get(rb_execution_context_t *ec, VALUE self)
static void
gc_stress_set(rb_objspace_t *objspace, VALUE flag)
{
+ if (flag != Qtrue && flag != Qfalse) {
@sampersand
sampersand / code.ijs
Created February 4, 2023 09:21
aoc 2019 day 2 part 1
CWD =: (<./CWD i:'/\') {.CWD=: ;(4!:3''){~4!:4 CWD=: <'CWD'
raw =: }: freads CWD , '/day2.txt'
die =: {{ echo y throw. }}
NB. raw =: '1,9,10,3,2,3,11,0,99,30,40,50'
ints =: ". every (',' cut raw)
ints =: (12 2) (1 2) } ints
number_of_params =: ((_ 3 3) , (_ * i.96) , 0) & {
@sampersand
sampersand / thing.ijs
Last active February 2, 2023 23:58
aoc 2019 day2 part 1
raw =: '1,9,10,3,2,3,11,0,99,30,40,50'
ints =: ". every (',' cut raw)
run_one =: {{
'op src1 src2 dst' =. (x + i.4) { y
select. op
case. 1 do. ((src1{y)+(src2{y)) dst } y
case. 2 do. y((src1{y)*(src2{y)) dst } y
@sampersand
sampersand / knight.rb
Last active September 25, 2022 09:05
Monkeypatched knight in ruby
#!/usr/bin/env ruby
# Knight in ruby, but with copious amounts of monkey patching :-)
class Fn
@fns = {}
def self.register(name, ...) = @fns[name] = Fn.new(...)
def self.lookup(name) = @fns.fetch(name)
def initialize(call_args: true, &block) @call_args, @block = call_args, block end
@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
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 / 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$_}
@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|