Skip to content

Instantly share code, notes, and snippets.

View krishnabhargav's full-sized avatar

Krishna Vangapandu krishnabhargav

View GitHub Profile
@krishnabhargav
krishnabhargav / quicksort.hs
Created July 3, 2014 01:43
Haskell is beautiful!
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let small = quicksort [ a | a <- xs, a <=x]
large = quicksort [ a | a <- xs, a > x]
in small ++ [x] ++ large
@krishnabhargav
krishnabhargav / tidbits.clj
Created July 19, 2014 12:51
Some Clojure Tidbits
;; CLOJURE STM USING REFS
;; this is just an immutable declaration
(def some-numbers #{1 2 3})
;;if you want to make the above mutable, you can use "refs"
(def some-numbers (ref #{1 2 3 }))
;; @ is used to dereference data
(def current-some-numbers @some-numbers)
@krishnabhargav
krishnabhargav / Clojure.clj
Last active August 29, 2015 14:04
Clojure Notest
;; http://grimoire.arrdem.com/
;;shows how to define lambda expressions in clojure
;;one form is a shortcut
(defn empty? [string]
(every? #(Character/isWhitespace %) string))
;; this one is a little more explicit
(defn empty2? [string]
(every? (fn [x] (Character/isWhitespace x)) string))
def factorial(num : Int) : Int = {
if(num > 1) num * factorial (num-1) else num
}
factorial(5) ///will print 120
@krishnabhargav
krishnabhargav / tailfactorial.scala
Created July 25, 2014 00:33
Factorial implemented using tail recursion
def factorial(number: Int) : Int = {
@tailrec def internalFactorial(seed: Int, num : Int) : Int = if (num > 1) internalFactorial (seed * num, num - 1) else num
internalFactorial(1, number)
}
@krishnabhargav
krishnabhargav / recur.clj
Created July 25, 2014 01:19
two examples to help understand recur and loop....recur
(defn count-down [n]
(do
(println "Method Started")
(println "Number : " n)
(when (pos? n) (recur (dec n)))))
;; when you run (count-down 5), output is
;; Method Started
;; Number : 5
;; Method Started
@krishnabhargav
krishnabhargav / factorial.clj
Created July 25, 2014 01:29
factorial implementation in clojure
(defn factorial [number]
(loop [num (biginteger number)
acc 1]
(if (zero? num) acc
(recur (dec num) (* acc num)))))
;; factorial can now be called on any large numbers
@krishnabhargav
krishnabhargav / destructure.clj
Last active August 29, 2015 14:04
Clojure Destructuring
;; Positional Destructuring
(def fullname ["Krishna", "Bhargava", "Vangapandu"])
(defn firstName [fullname]
; this is positional destructuring - works only on vectors as they are aligned sequentially
(let [[fName _ _] fullname]
fName))
(defn first5
"returns a vector with the first item as count of the sequence passed. The rest of the items are the first 5 items in the sequence."
@krishnabhargav
krishnabhargav / are-consecutive.clj
Last active August 29, 2015 14:05
functional way to determine if the elements in the sequence are all consecutive
(defn are-consecutive? [input]
(let [sorted (sort input)
diff (map - (rest sorted) sorted)
all-one? (every? #(= 1 %) diff)]
all-one?))
@krishnabhargav
krishnabhargav / seq-merge.clj
Created August 24, 2014 17:49
merging two sequences in clojure
(defn seq-merge [a-seq b-seq]
(cond (empty? a-seq) b-seq ;; retur non-empty sequence if any of the sequence is empty
(empty? b-seq) a-seq
:else (let [first-a (first a-seq)
first-b (first b-seq)]
(if (< first-a first-b) ;;if picking the first item, the rest of a-seq or vice versa
(cons first-a
(seq-merge (rest a-seq) b-seq))
(cons first-b
(seq-merge a-seq (rest b-seq)))))))