Skip to content

Instantly share code, notes, and snippets.

View stoeckley's full-sized avatar

andrew stoeckley

  • Balcony Studio
  • Netherlands
View GitHub Profile
@stoeckley
stoeckley / waves.pde
Created April 4, 2018 20:29 — forked from beesandbombs/waves.pde
waves
int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);
@stoeckley
stoeckley / gist:5965f45cf6bc7853061e5d672ddd3577
Created January 1, 2018 14:04
best way to make this simple struct hashable?
// trying to keep this Hashable simple while minimizing collisions:
struct Simple: Hashable {
enum MyEnum: Int {
case a,b,c,d
}
let enumValue: MyEnum
let someInt: Int
@stoeckley
stoeckley / gist:f6da4a656b911d247e827ef2a15badc4
Created December 31, 2017 23:58
how to initialize a let based on conditional branches
// i often do this just so i can make a variable a constant let and not a var;
// with a var I could just use a switch, but it's better to have a let when possible
// how to make this prettier?
let someConstant: Int64 =
someBoolExpression ? 1 :
someOtherBoolExpression ? 2 :
yetAnotherBoolExpression ? 3 : 0
@stoeckley
stoeckley / gist:a51669e6140490ab5590dce24762ddbf
Last active January 1, 2018 14:47
different ways of creating a default item in a dictionary if doesn't exist
var dict = [String: [String: Int]]()
if dict["foo"] == nil {
dict["foo"] = [:]
}
dict["foo"]?["bar"] = 5 // safe way to access dictionaries in general
dict["foo"]!["bar"] = 6 // we already tested for nil in the if statement, so force unwrap is safe, and maybe faster?
if (foo != nil && bar > foo!) || foo == nil {
// something
}
if let foo = foo, bar > foo || self.foo == nil {
// something
}
#if !DEBUG
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
//does nothing if not in debug build
}
#else
public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") {
debugPrint(items)
}
#endif
@stoeckley
stoeckley / Cljs hangman
Last active October 19, 2017 21:55
A hangman port from the Elm Haarlem workshop Hangman, into ClojureScript
(def init {:word "XIMEDES"
:attempts-left 10
:state :playing
:guessed #{}})
(def model (atom init))
(defn show-won []
[:div "Jeej you won!, share it with your friends!"])
@stoeckley
stoeckley / missing_keys_specs.clj
Created October 14, 2017 12:37 — forked from stuarthalloway/missing_keys_specs.clj
I think it would be a mistake to introduce temporal coupling to prevent typos.
;; I think it would be a mistake to introduce temporal coupling to prevent typos.
;; The example program below lets you identify "missing" keys specs at
;; the time and place of your choosing, and then handle them as you
;; deem appropriate, without imposing those decisions on other
;; users of spec.
(require '[clojure.spec.alpha :as s]
'[clojure.set :as set])
@stoeckley
stoeckley / missing_keys_specs.clj
Created October 14, 2017 12:37 — forked from stuarthalloway/missing_keys_specs.clj
I think it would be a mistake to introduce temporal coupling to prevent typos.
;; I think it would be a mistake to introduce temporal coupling to prevent typos.
;; The example program below lets you identify "missing" keys specs at
;; the time and place of your choosing, and then handle them as you
;; deem appropriate, without imposing those decisions on other
;; users of spec.
(require '[clojure.spec.alpha :as s]
'[clojure.set :as set])
;; try this form-by-form at a REPL
(require '[clojure.spec.alpha :as s])
;; create an inline DSL to describe the FizzBuzz world
(defmacro divides-by
[nm n]
`(s/def ~nm (s/and pos-int? #(zero? (mod % ~n)))))
;; specify FizzBuzz
(divides-by ::fizz 3)