Skip to content

Instantly share code, notes, and snippets.

View kapilreddy's full-sized avatar

Kapil Reddy kapilreddy

View GitHub Profile
(defn divide-coll
"Divide collection of block heights into regions where water will be blocked.
ex.
[1 2 0 1 3 2 1 0 3] -> [[3 2 1 0 3] [2 0 1 3]]"
[c]
(let [regions (reduce (fn [coll i]
(cond
(seq coll) (let [[x & xs] coll
from (first x)
new-x (conj (vec x) i)]
@kapilreddy
kapilreddy / example.clj
Created October 21, 2013 15:04
core.match macro with fns.
;; Match with guard function
(match [{:a 2} {:b 3}]
[{:a (_ :guard even?)} _] 1)
;; Match-with-fn example for same
(match [{:a 2} {:b 3}]
[{:a even?} _] 1)
@kapilreddy
kapilreddy / pubsub.clj
Last active December 15, 2015 05:49
A simple PubSub system written in Clojure.
(defn publish
([a & ch-vals]
(let [key-vals (mapcat (fn [[channel val]]
[channel {:val val
:id (java.util.UUID/randomUUID)}])
(partition 2 ch-vals))]
(apply swap! a assoc key-vals))))
(defn subscribe
@kapilreddy
kapilreddy / josephus.clj
Created February 27, 2013 08:27
A simple solution to Josephus problem with kill rate of 2.
(defn find-surviver*
[in]
(loop [[f s & coll] in]
(if (and f s)
(recur (concat coll [f]))
f)))
(defn find-surviver
[population]
@kapilreddy
kapilreddy / application-tags.xml
Last active October 13, 2015 12:27 — forked from nayansuthar/application-tags.xml
helpshift initialization android
<activity android:name="com.helpshift.HelpshiftActivity" android:theme="@style/HSBottomAnimTheme" />
<activity android:name="com.helpshift.HSAddIssue" android:theme="@style/HSRightAnimTheme" />
<activity android:name="com.helpshift.HSAddProfile" android:theme="@style/HSRightAnimTheme" />
<activity android:name="com.helpshift.HSMessages" android:theme="@style/HSRightAnimTheme" />
<activity android:name="com.helpshift.HSQuestionsList" android:theme="@style/HSRightAnimTheme" />
<activity android:name="com.helpshift.HSQuestion" android:configChanges="orientation|screenSize" android:theme="@style/HSRightAnimTheme" />
@kapilreddy
kapilreddy / gist:3865597
Created October 10, 2012 13:18
OSX .bash_profile
##
# Prints terminal codes.
#
# Thanks to: http://github.com/wayneeseguin/rvm/blob/master/scripts/color
#
# @param [String] terminal code keyword (usually a color)
bput() {
case "$1" in
# regular colors
black) tput setaf 0 ;;
@kapilreddy
kapilreddy / gist:2868961
Created June 4, 2012 15:10
Midje emacs functions
(remove-hook 'clojure-mode-hook 'clojure-test-maybe-enable)
(defun clojure-in-tests-p ()
(or (string-match-p "test\." (clojure-find-ns))
(string-match-p "/test" (buffer-file-name))))
(defun midje-test-for (namespace)
(let* ((namespace (clojure-underscores-for-hyphens namespace))
(segments (split-string namespace "\\."))
@kapilreddy
kapilreddy / gist:2868737
Created June 4, 2012 14:32
A precommit hook for js
#!/bin/sh
# A pre-commit hook for js
# It runs jshint on js files.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
@kapilreddy
kapilreddy / gist:2867328
Created June 4, 2012 08:57
Precommit hook for js and clojure
#!/bin/sh
# A pre-commit hook for js and clojure
# It runs jshint on js files.
# and runs lein2 midje.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
@kapilreddy
kapilreddy / gist:1259881
Created October 3, 2011 18:42
php config for emacs
(require 'flymake)
(defun flymake-php-init ()
"Use php to check the syntax of the current file."
(let* ((temp (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace))
(local (file-relative-name temp (file-name-directory buffer-file-name))))
(list "php" (list "-f" local "-l"))))
(add-to-list 'flymake-err-line-patterns
'("\\(Parse\\|Fatal\\) error: +\\(.*?\\) in \\(.*?\\) on line \\([0-9]+\\)$" 3 4 nil 2))