Skip to content

Instantly share code, notes, and snippets.

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

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@ghoseb
ghoseb / pascal.clj
Created July 11, 2011 12:33
Psacal's Functional Triangle
(ns pascal)
(defn next-row
"Given one row, generate the next one."
[row]
(vec (map + (concat [0] row) (concat row [0]))))
(defn pascal
"Generate an infinite lazy sequence of Pascal rows."
[]
@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 / tetris.clj
Created September 12, 2011 08:09 — forked from alexander-yakushev/tetris.clj
Tetris implementation in Clojure
(ns tetris.core
(:import (java.awt Color Dimension BorderLayout)
(javax.swing JPanel JFrame JOptionPane JButton JLabel)
(java.awt.event KeyListener))
(:use clojure.contrib.import-static deflayout.core
clojure.contrib.swing-utils)
(:gen-class))
(import-static java.awt.event.KeyEvent VK_LEFT VK_RIGHT VK_DOWN VK_UP VK_SPACE)
@ghoseb
ghoseb / flatten.clj
Created September 14, 2011 04:46
Flatten a nested collection using Tail Recursion.
(defn my-flatten
"Flatten a nested collection using Tail Recursion."
[coll]
(letfn [(step [[fst & more :as coll] res]
(if (seq coll)
(if (coll? fst)
(recur (concat fst more) res)
(recur more (cons fst res)))
(reverse res)))]
(step coll nil)))
@ghoseb
ghoseb / 97.clj
Created December 17, 2011 15:55
4clojure #97
(fn [n]
(nth
(iterate (fn [row]
(let [prow (partition 2 1 row)]
(concat [1] (map #(apply + %) prow) [1])))
[1])
(dec n)))
@ghoseb
ghoseb / col-str.clj
Created January 6, 2012 08:42
Convert numbers to Excel headers
(let [chr (zipmap (range) "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
encode (fn [n acc]
(if (< n 26)
(cons n acc)
(recur (dec (quot n 26)) (cons (rem n 26) acc))))]
(defn col-str
"Convert numbers to Excel headers."
[n]
(apply str (map chr (encode n '())))))
@ghoseb
ghoseb / fact.py
Created March 11, 2012 05:14
Some factorial implementations in Python
import operator
# naive recursive solution
def fact1(n):
if n <= 1:
return 1
return n * fact1(n - 1)
fact2 = lambda n: reduce(operator.mul, xrange(1, n + 1))
@ghoseb
ghoseb / scaffold.clj
Created March 16, 2012 12:54
Scaffold by Christophe Grand
(defn scaffold
"Print the ancestor method signatures of a given interface."
[iface]
(doseq [[iface methods] (->> iface
.getMethods
(map #(vector (.getName (.getDeclaringClass %))
(symbol (.getName %))
(count (.getParameterTypes %))))
(group-by first))]
(println (str " " iface))
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(set! *warn-on-reflection* true)
@ghoseb
ghoseb / cut.clj
Created April 16, 2012 09:32
Cut macro from SRFI-26 in Clojure
;;; http://srfi.schemers.org/srfi-26/srfi-26.html
(defn ^:private cut*
[[a f] form]
(cond
(nil? form) [a f]
(seq? (first form))
(let [[arg-list xform] (cut* [[] '()] (first form))]
(recur [(reduce conj a arg-list) (concat f (list xform))] (next form)))