Skip to content

Instantly share code, notes, and snippets.

View bvandgrift's full-sized avatar

Ben Vandgrift bvandgrift

View GitHub Profile
@bvandgrift
bvandgrift / can_pet.js
Last active February 23, 2022 16:43
simple decision logic demo for a friend's son currently javascripting
// more javascripty!
const FRIENDLY = {
MAYBE: 'pet at your own risk',
YES: 'ok to pet',
NO: 'do NOT pet'
}
class Pet {
constructor(name, isFriendly = FRIENDLY.MAYBE, sound = "UNKNOWN") {
@bvandgrift
bvandgrift / fix-missing.sh
Last active December 5, 2020 04:30
cheap idempotent file fetching
#!/usr/bin/env bash
# given a single case id (e.g., 19820427023459I) will check to see if
# it exists in the output directory, otherwise fetch it --
# it won't store the file if there's a problem with the request
# usage: ./fix-missing.sh <case_id>
if [ `ls out/${1}.html 2> /dev/null` ]; then
exit
else
@bvandgrift
bvandgrift / pseudo-test.rb
Last active March 20, 2019 19:19
throwaway sketch for a maybe test
test "don't do unnecessary work for no change" do
# let's assume that there's an enqueue(evt) method which pulls an event into the
# processing pipeline, and that the enqueue method in turn calls a process(evt) method
# let's further assume that if an event has an attribute, once processed, we don't want
# to process it again for the same attribute value. so evt(attr:5), evt(attr:5), evt(attr:6) would
# generate two process() calls, not three.
pipeline = Pipeline.new() # or whatever
@bvandgrift
bvandgrift / keybase.md
Created November 15, 2016 00:48
keybase verification

Keybase proof

I hereby claim:

  • I am bvandgrift on github.
  • I am bvandgrift (https://keybase.io/bvandgrift) on keybase.
  • I have a public key ASAf4orFDrtbzOXVMO64S8C44-wKfuSIdKy5-VCmHth31wo

To claim this, I am signing this object:

@bvandgrift
bvandgrift / lists.rb
Created September 1, 2016 20:51
for jason, closures
## off the top of my head, syntax problems are an exercise for the reader
list = [:a, 'list', 3, 1.15, {b: 'yes, really'}]
# a simple list comprehension.
list.each do |thing|
puts thing.class # or some triviality
end
# functions take blocks, by default -- a block is a closure over
@bvandgrift
bvandgrift / unicat.js
Last active August 5, 2016 23:11
return an array of unique elements from an arbitrary list of arrays
// why not let set and spread do all the work?
// arbitrary number of arrays, unique
function unicat(...arrays) {
let collector = [];
arrays.forEach((a) => {collector = collector.concat(a);});
return Array.from(new Set(collector).values());
}
// O(n), i think, if concat() is implemented sanely.
@bvandgrift
bvandgrift / partials.js
Created March 31, 2016 18:46
fiddling around with the `arguments` construct in .js
// takes a fn and a number of arguments to append to the
// fn's argument list
var rPartial = function(fn, extras) {
var postArgs = [].slice.call(arguments).slice(1);
return function(vars) {
var preArgs = [].slice.call(arguments);
var allArgs = preArgs.concat(postArgs);
return fn.call(fn.this, ...allArgs);
};
@bvandgrift
bvandgrift / attribute_requirements.rb
Created February 28, 2016 03:46
a terrible way to determine whether attrs on an ActiveRecord model are optional or required when using grape.
module AttributeRequirements
extend ActiveSupport::Concern
SQL_TYPE_MATCHERS = {
/^character varying/ => String,
/^text/ => String,
/^enum/ => String,
/^timestamp/ => DateTime,
/^integer/ => Integer,
Verifying that +bvandgrift is my blockchain ID. https://onename.com/bvandgrift
@bvandgrift
bvandgrift / let-placement.clj
Created January 7, 2016 18:24
placing your peek inside of a dosync avoids potential thread conflict (from Clojure Applied forum)
;; response to: https://forums.pragprog.com/forums/352/topics/13835
;; this is a quick demonstration of the pitfalls in querying for a ref
;; outside of the accompanying transaction in which you plan on changing it.
(import '(java.util.concurrent Executors))
;; start list
(def slist (ref #{}))