Skip to content

Instantly share code, notes, and snippets.

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

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@ghoseb
ghoseb / download.sql
Last active December 11, 2015 08:08
Check out what was the first item that you ever downloaded on your Mac OS X
-- Open a SQLite shell by issuing the command below
-- sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV*
select LSQuarantineDataURLString from LSQuarantineEvent order by LSQuarantineTimeStamp asc limit 1;
-- For me, it's GNU Emacs.
-- sqlite> select LSQuarantineDataURLString from LSQuarantineEvent order by LSQuarantineTimeStamp asc limit 1;
-- http://bandwidth.porkrind.org/emacs-builds/Emacs-24.2-universal-10.6.8.dmg
@ghoseb
ghoseb / freq.clj
Last active December 12, 2015 08:19
Sample code from Pune Clojure Dojo Meetup session held at 8 feb 2013.
(ns ^{:doc "Word frequencies in a text file."
:author "Baishampayan Ghose <b.ghose@helpshift.com>"}
meetup.freq
(:require [clojure.java.io :as io]
[clojure.string :as s]))
(def stop-word? #{"is" "the" "am" "i" "that" "if"}) ;; fill it up!
;;; all these functions are written in a "point free" style
(def get-lines (comp line-seq io/reader))
@ghoseb
ghoseb / props.clj
Last active December 12, 2015 08:28
Read a Java Properties file into a map (with potentially nested keys).
(ns ^{:doc "Read Java properties file."
:author "Baishampayan Ghose <b.ghose@gmail.com>"}
in.freegeek.props
(:import java.util.Properties)
(:require [clojure.java.io :refer [resource reader]]
[clojure.string :refer [split]]))
(defn- load-props
"Load a Java properties file. File should be in classpath."
[props-file]
@ghoseb
ghoseb / props.clj
Last active December 16, 2015 03:49
A simple problem that I solved at the April '13 meetup of Pune Clojure Users' Group. The problem is about parsing a Java properties file into Clojure & back.
(ns props.core
(:require [clojure.string :refer [split]]
[clojure.java.io :refer [reader]])
(:import java.util.Properties))
(defn- split-key
"Split a string key to its subparts.
foo -> [foo]
foo.bar.baz -> [foo bar baz]"
; STM history stress-test
(defn stress [hmin hmax]
(let [r (ref 0 :min-history hmin :max-history hmax)
slow-tries (atom 0)]
(future
(dosync
(swap! slow-tries inc)
(Thread/sleep 200)
@r)
;;; 99 bottles of beer in Clojure
(def bottles "
# 99 Bottles of Beer
# Brainfuck version
# by Michal Wojciech Tarnowski
+>+++++++[>>>+++
+++++<<<<+++++
@ghoseb
ghoseb / uniquify.clj
Created January 15, 2014 07:09
Uniquify buffer names
(ns ^{:doc "Uniquify"
:author "Baishampayan Ghose <b.ghose@helpshift.com>"}
uniquify
(:require [clojure.string :refer [join]]))
(defn explode
"Explode a directory name to its subcomponents."
[^String dir]
(seq (.split dir "/")))
@ghoseb
ghoseb / prime_sieve.clj
Last active April 22, 2016 08:05
A concurrent prime sieve in Clojure using core.async
(ns prime-sieve
(:require [clojure.core.async :as async :refer [chan go <! >!]]))
;;; concurrent prime sieve in Clojure using core.async
;; inspired by a similar implementation in Go
;; http://golang.org/doc/play/sieve.go
(defmacro go-forever
"An infinite loop that runs in a go block."
@ghoseb
ghoseb / gevent_example.py
Created July 27, 2011 09:27
Gevent spawn / link example
from gevent import monkey; monkey.patch_all()
import gevent
import gevent.greenlet
from functools import partial
from random import random
import urllib
import urllib2
def on_exception(fun, greenlet):
@ghoseb
ghoseb / clj_spec_playground.clj
Last active March 30, 2019 22:35
Examples of Clojure's new clojure.spec library
(ns clj-spec-playground
(:require [clojure.string :as str]
[clojure.spec :as s]
[clojure.test.check.generators :as gen]))
;;; examples of clojure.spec being used like a gradual/dependently typed system.
(defn make-user
"Create a map of inputs after splitting name."
([name email]