Skip to content

Instantly share code, notes, and snippets.

View mikera's full-sized avatar

Mike Anderson mikera

View GitHub Profile
@mikera
mikera / clj-1.8.0-alpha2-tuple-perf.clj
Last active August 29, 2015 14:25 — forked from ptaoussanis/clj-1.8.0-alpha2-tuple-perf.clj
Benchmarking for tuple performance
;; *clojure-version*
;; (require '[taoensso.encore :as encore])
(let [v [1 2 3 4]
x 1
y 2
z 3]
(encore/qbench ; Returns fastest of 3 sets of lap times for each form, in msecs
1000000 ; Number of laps
@mikera
mikera / gist:8bcbf0e037df312c6bf6
Last active August 29, 2015 14:25
Tuple performance testing with core.matrix
;; Using criterium to test performance of Tuples
;;
;; Benchmark using parts of the core.matrix implementation test suite
;;
;; ===========================================================
;; Common setup:
(use '[criterium.core :as c])
(require 'clojure.core.matrix.compliance-tester)
@mikera
mikera / gist:2a3ed082b07a4f65cbcc
Last active September 9, 2015 08:25
Exploring overhead of var indirection in Clojure
;; =======================================================================
;; Motivation: we are interested in the performance of tiny
;; wrapper functions which look like:
;; (fn [_ x y] (g x y))
;;
;; Question: are they getting the full performance benefits of inlining?
(ns test
(:require [criterium.core :as c]))
@mikera
mikera / clojure-match.clj
Created September 5, 2012 04:51 — forked from ckirkendall/clojure-match.clj
Language Compare F#, Ocaml, Scala, Clojure, Ruby and Haskell - Simple AST example
(use '[clojure.core.match :only [match]])
(defn evaluate [env [sym x y]]
(match [sym]
['Number] x
['Add] (+ (evaluate env x) (evaluate env y))
['Multiply] (* (evaluate env x) (evaluate env y))
['Variable] (env x)))
(def environment {"a" 3, "b" 4, "c" 5})
@mikera
mikera / gist:1332788
Created November 2, 2011 03:36
Incanter examples
(sample-normal 5)
=> (-0.10224658005696823 -0.39374760130345093 -0.9347003327466611 0.027885002566669224 -0.5329681018922551)
(view (histogram (sample-normal 1000)))
(view (function-plot sin -10 10))
@mikera
mikera / gist:1332761
Created November 2, 2011 03:22
Cool Clojure examples
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Frequency analysis (implicitly treating a string as a sequence of chars)
(frequencies "abracadabra")
=> {\a 5, \b 2, \r 2, \c 1, \d 1}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Infinite, lazily calculated sequence of Fibonacci numbers
;; enodyt's solution to Game of Life
;; https://4clojure.com/problem/94
(fn [board]
(let [dx (count (first board))
dy (count board)
neighbours (fn [pos board]
(let [xs [[-1 -1] [0 -1] [1 -1]
[-1 0] [1 0]
[-1 1] [0 1] [1 1]]]
@mikera
mikera / gist:1104578
Created July 25, 2011 16:57
Clojure quick tutorial
;; Objective
;; 1. Learn Clojure by doing Clojure
;;
;; Pre-requisites
;; You need to have a running Clojure REPL
;; see: http://clojure.org/getting_started
; Hello World in Clojure
(println "Hello World")
@mikera
mikera / gist:970439
Created May 13, 2011 12:28
Options for state update in Clojure
;; event application with subsequent events
;; event :: state -> (state' [event])
(defn update-with-events [state events]
(if-let [event (first events)]
(let [other-events (rest events)
[updated-state new-events] (event game)]
(recur updated-state (concat other-events new-events)))
state ))
(def test-map
{:a 1 :b 2})