Skip to content

Instantly share code, notes, and snippets.

View ghoseb's full-sized avatar
🏋️‍♂️

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@jmgimeno
jmgimeno / bfc.clj
Created June 14, 2012 07:20
Brainf*** compiler (Alan Dipert)
(defn bfc
[program]
(let [allowed #{\+ \- \< \> \[ \] \.}
src (->> program (filter allowed)
(interpose \space) (apply str))
fns (zipmap '(- + < > . ?) (repeatedly gensym))]
(letfn [(bfc* [s]
(if (vector? s)
`(while (not (~(fns '?))) ~@(map bfc* s))
`(~(fns s))))]
@jboner
jboner / latency.txt
Last active May 9, 2024 14:20
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers (~2012)
----------------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
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 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
@cgrand
cgrand / restrict-map.clj
Created May 29, 2012 10:15
Restricting nested maps to keys of interest
;; I could have used a closed dispatch (aka cond) but you may find this version more enjoyable
;; the spec format is the one provided by BG
(defprotocol Selector
(-select [s m]))
(defn select [m selectors-coll]
(reduce conj {} (map #(-select % m) selectors-coll)))
(extend-protocol Selector
@stuarthalloway
stuarthalloway / gist:2651196
Created May 10, 2012 05:22
Datomic queries against Java collections
// Datomic example code
package datomic.examples;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import static datomic.Peer.*;
@stuarthalloway
stuarthalloway / gist:2645453
Created May 9, 2012 15:22
Datomic queries against Clojure collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
@svs
svs / gist:2478214
Created April 24, 2012 09:23
Question about Rich Hickey's 'use hashes not classes' statement

So I don't have a model called Person, but I use hashes like {:name => "Siddharth", :sex => "Y"}. Great. Now I want to validate the presence of certain fields before persisting to the database. What do I do?

Module PersonValidation
  function validate_presence_of_name(person_hash)
    person_hash[:errors][:name] = "EEENKKK!" unless person_hash[:name]
  end
  
  ...
 ...
@redinger
redinger / Emacs.md
Created November 26, 2011 03:22
Setting up Emacs daemon on OS X

Setting up Emacs daemon on OS X

Tired of waiting for emacs to start on OS X? This step by step guide will teach you how to install the latest version of emacs and configure it to start in the background (daemon mode) and use emacsclient as your main editor.

Install Cocoa Emacs

Download the latest pretest version of [Emacs for Mac OS X]: http://emacsformacosx.com/builds

@nathanmarz
nathanmarz / gist:1246228
Created September 27, 2011 20:56
Exception cause predicate
;; Determine if any of the causes of the exception was of the specified type
(defn exception-cause? [klass ^Throwable t]
(->> (iterate #(.getCause ^Throwable %) t)
(take-while identity)
(some (partial instance? klass))
boolean))
@alandipert
alandipert / isbn13.clj
Created August 27, 2011 18:11
ISBN-13 validation
(defn isbn13? [isbn13]
(let [nums (map #(Integer/parseInt %) (re-seq #"\d" isbn13))
first-nums (butlast nums)
sum (->> first-nums
(map vector (cycle [1 3]))
(map (partial apply *))
(reduce +))
last-num (last nums)]
(= last-num
(- 10 (mod sum 10)))))
@cs224
cs224 / levenshtein-allison.clj
Created March 27, 2011 16:36
levenshtein-allison.clj
;;; implementation in clojure of the levenshtein allison algorithm as defined here:
;;; http://www.csse.monash.edu.au/~lloyd/tildeFP/Haskell/1998/Edit01/
(defn min3 [w nw n]
(if (< w nw) w (min nw n)))
(defn generate-diagonale [a b nw fn-diag-above fn-diag-below start]
(if start
(lazy-cat (list nw) (generate-diagonale a b nw fn-diag-above fn-diag-below false))
(if (or (empty? a) (empty? b)) '()