Skip to content

Instantly share code, notes, and snippets.

@anmonteiro
anmonteiro / getCountdown
Created March 17, 2014 14:30
Little function to get the remaining time until a target date - still WIP
var getCountdown = function(targetDate){
var left = targetDate.getTime() - new Date().getTime();
var seconds = + ((left / 1000) % 60) ;
var minutes = + ((left / (1000*60)) % 60);
var hours = + ((left / (1000*60*60)));
return hours.toFixed(0)+' h '+minutes.toFixed(0)+' min '+seconds.toFixed(0) + ' s';
}
@anmonteiro
anmonteiro / 2048CheatBookmarklet.js
Last active August 29, 2015 13:58
Bookmarklet to start 2048 with a 2048 tile.
javascript:(function() {
localStorage.setItem('gameState',
"{\"grid\":{\"size\":4,\"cells\":[[null,{\"position\":{\"x\":0,\"y\":1},\"value\":2},null,{\"position\":{\"x\":0,\"y\":3},\"value\":4}],[null,null,null,{\"position\":{\"x\":1,\"y\":3},\"value\":4}],[null,{\"position\":{\"x\":2,\"y\":1},\"value\":2},{\"position\":{\"x\":2,\"y\":2},\"value\":16},{\"position\":{\"x\":2,\"y\":3},\"value\":8}],[null,{\"position\":{\"x\":3,\"y\":1},\"value\":4},{\"position\":{\"x\":3,\"y\":2},\"value\":8},{\"position\":{\"x\":3,\"y\":3},\"value\":2048}]]},\"score\":20232,\"over\":false,\"won\":true,\"keepPlaying\":true}");
window.location.reload();
})();
(ns express_sample
(:require [cljs.nodejs :as node]))
(def express (node/require "express"))
(def app (. express (createServer)))
(defn -main [& args]
(doto app
(.use (. express (logger)))
(.get "/" (fn [req res]
@anmonteiro
anmonteiro / fib.js
Created July 14, 2014 10:11
Caching Fibonacci (JavaScript)
var fib = (function() {
var fibArr = [];
var calcFib = function( n ) {
return ( n <= 1 ? 1 : (fib( n - 1 ) + fib( n - 2 )));
};
return function( n ) {
if( !fibArr[ n ] ) {
fibArr[ n ] = calcFib( n );

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns                     on recent CPU
L2 cache reference ........................... 7 ns                     14x L1 cache
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns                     20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs 4X memory

@anmonteiro
anmonteiro / parse.clj
Last active September 6, 2015 18:05 — forked from timsgardner/parse.clj
parse
(defn parse-form [down-tok?, up-tok?, [tok & toks]]
(loop [frm [tok], toks toks]
(if-let [[tok2 & toks2] (seq toks)]
(cond
(down-tok? tok2) (let [[res toks3] (parse-form down-tok?, up-tok?, toks)]
(recur (conj frm res) toks3))
(up-tok? tok2) [(conj frm tok2) toks2]
:else (recur (conj frm tok2) toks2))
[frm nil])))
(def app-state
{:people [{:name "Alice" :age 31 :single false}
{:name "Bob" :age 25 :single false}
{:name "Carl" :age 20 :single true}]})
(defn read
[{:keys [state]} k {:keys [length]}]
{:value
(take length
(into []
@anmonteiro
anmonteiro / navigator_om.next.cljs
Last active November 4, 2015 20:11
NavigatorIOS (Om Next) - No props. / Navigator (Om Next)
(defn view [opts & children]
(apply js/React.createElement js/React.View (clj->js opts) children))
(defn text [opts & children]
(apply js/React.createElement js/React.Text (clj->js opts) children))
(defn image [opts & children]
(apply js/React.createElement js/React.Image (clj->js opts) children))
(defn navigator [opts & children]
@anmonteiro
anmonteiro / composite.cljs
Last active November 5, 2018 16:59
Code for the series of posts "An exploration of object recursion design patterns in Om Next recursive queries"
;; =============================================================================
;; Composite
(def composite-data
{:composite/item {:id 0
:width 400
:height 400
:color "#428BCA"
:children [{:id 1
:width 200