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 / 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)
@stoeckley
stoeckley / vector drag
Created July 30, 2017 17:37
Drag index through a vector
(defn slide
"Returns the final vector after dragging an indexed item to another index, which means it swaps incrementally with each index it passes through. v should be a vector but if it isn't it will be converted to one."
[v o n]
(let [v (vec v) ;; ensures v is a vector
item (v o)
removed (if (= o (count v))
(pop v)
`[~@(concat
(subvec v 0 o)
(subvec v (inc o)))])]
@stoeckley
stoeckley / bench.clj
Created July 30, 2017 17:37 — forked from hiredman/bench.clj
core.logic for joinery
#!/usr/bin/java -jar clojure-1.7.0-master-SNAPSHOT.jar
(let [pom-uber-jar
(str "http://thelibraryofcongress.s3.amazonaws.com/"
"pomegranate-0.0.13-SNAPSHOT-jar-with-dependencies.jar")
cl (java.net.URLClassLoader. (into-array [(java.net.URL. pom-uber-jar)]))
cx (.getContextClassLoader (Thread/currentThread))]
(push-thread-bindings {clojure.lang.Compiler/LOADER cl})