Skip to content

Instantly share code, notes, and snippets.

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

Baishampayan Ghose ghoseb

🏋️‍♂️
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / with-retries.clj
Created March 18, 2011 14:18
Macro to retry executing some code in case of an exception
(ns test)
(defn try-times
"Try executing a thunk `retries` times."
[retries thunk]
(if-let [res (try
[(thunk)]
(catch Exception e ; can be any exception
(when (zero? retries)
(throw e))))]
@ghoseb
ghoseb / accounts.clj
Created May 4, 2010 05:52
Simple bank account code
;;; accounts.clj -- Accounts -*- Clojure -*-
;;; Time-stamp: "2010-05-04 11:17:53 ghoseb"
;;; Author: Baishampayan Ghose <bg@infinitelybeta.com>
(ns accounts)
(def *min-bal* 500)
(defstruct account
:name
@ghoseb
ghoseb / ns-cheatsheet.clj
Last active April 11, 2024 05:28 — forked from alandipert/ns-cheatsheet.clj
Clojure ns syntax cheat-sheet
;;
;; NS CHEATSHEET
;;
;; * :require makes functions available with a namespace prefix
;; and optionally can refer functions to the current ns.
;;
;; * :import refers Java classes to the current namespace.
;;
;; * :refer-clojure affects availability of built-in (clojure.core)
;; functions.