Skip to content

Instantly share code, notes, and snippets.

View jcromartie's full-sized avatar

John Cromartie jcromartie

View GitHub Profile
(defn compile-unrolled
[f inputs]
(let [clauses (mapcat #(list % (f %)) inputs)]
`(fn [x#]
(case x#
~@clauses
(~f x#)))))
(defn unrolled
"Return a function with precomputed lookup for the given input domain."
;;; MrMusAddict's original function
(def charge-max 100.0)
(def charge-min 0.0)
(def bright-max 0.9)
(def bright-min 0.0)
;;; float brightFinal = (brightMin-brightMax)*Math.Pow((chargeCurrent-chargeMax)/(chargeMax-chargeMin), 4)+brightMax;
(defn mr-mus-addict
;; now with an escape hatch!
(deftest home-page-flow
(naturally
(navigate to "/")
(click link "Sign In")
(fill in "Username" with $username, "Password" $password)
(click button "Sign In")
(expect page to have content "Welcome, " $username)))
- (NSIndexPath *)nextIndexPath:(NSIndexPath *)i
{
// apologies to any maintainer but this was some code golf going on in #iphonedev
id t=_tableView;
int s,r,n;s=i.section;r=(i.row+1)%[t numberOfRowsInSection:s];n=[t numberOfSections];
if(!r)for(;s<n&&![t numberOfRowsInSection:s+++1];);
return s==n?nil:[NSIndexPath indexPathForRow:r inSection:s];
}
@jcromartie
jcromartie / let_futures.clj
Created October 4, 2013 19:18
A macro that wraps values in a binding form with `future` and transparently `deref` them in the body.
(defmacro let-futures
[bindings & body]
(let [binding-pairs (partition 2 bindings)
future-syms (set (map first binding-pairs))
binding-vector (vec (mapcat (fn [[l r]] [l (list 'future r)]) binding-pairs))
body-code (clojure.walk/walk
(fn derefify [x]
(if (future-syms x)
(list 'clojure.core/deref x)
(clojure.walk/walk derefify identity x)))
@jcromartie
jcromartie / thumbs.sh
Created September 18, 2013 18:57
Put a thumbs-up or thumbs-down in your prompt based on the exit code of the last command
thumbsDown=$(printf "\xF0\x9F\x91\x8E")
thumbsUp=$(printf "\xF0\x9F\x91\x8D")
function exitIcon()
{
if [ $? -eq 0 ]
then
echo $thumbsUp
else
echo $thumbsDown
fi
(ns speedtest)
(defn with-args
"Count down from n, return 0"
[n]
(if (< 0 n)
(recur (dec n))
0))
(defn with-state
@jcromartie
jcromartie / async_expansion.clj
Created July 13, 2013 19:35
Macro expansion of (go (+ 1 1))
;; (macroexpand-1 '(go (+ 1 1)) yields ...
(clojure.core/let
[c__2247__auto__
(clojure.core.async/chan 1)
captured-bindings__2248__auto__
(clojure.lang.Var/getThreadBindingFrame)]
(clojure.core.async.impl.dispatch/run
(clojure.core/fn
[]
@jcromartie
jcromartie / game.clj
Last active December 19, 2015 11:09
Clojure game skeleton
(defn run-game!
[title w h init-state tick render]
(let [dim (Dimension. w h)
keystate (atom #{})
listener (input/listener
{:key-pressed (fn [code]
(swap! keystate conj code))
:key-released (fn [code]
(swap! keystate disj code))
:focus-lost (fn []
@jcromartie
jcromartie / when_let.rb
Created May 10, 2013 16:15
I wanted something like when-let from Clojure in Ruby.
class Object
def when_let
yield self if self
end
end