Skip to content

Instantly share code, notes, and snippets.

View krishnabhargav's full-sized avatar

Krishna Vangapandu krishnabhargav

View GitHub Profile
@krishnabhargav
krishnabhargav / server.clj
Last active August 29, 2015 14:06
clojure server
(ns webapp.server
(:require [ring.middleware.resource :refer [wrap-resource]]
[ring.middleware.file-info :refer [wrap-file-info]]
[ring.middleware.reload :refer [wrap-reload]]
[compojure.core :refer :all]
[compojure.route :as route]
[org.httpkit.server :refer [run-server]]))
(defroutes route-map
@krishnabhargav
krishnabhargav / project.clj
Last active August 29, 2015 14:06
web development blog's project.clj
(defproject webapp "0.1.0-SNAPSHOT"
:description "Web Application in Clojure"
:url "http://localhost/groupie"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.6.0"]
[ring "1.3.1"]
[http-kit "2.1.16"]
[compojure "1.1.9"]
@krishnabhargav
krishnabhargav / trip.clj
Last active August 29, 2015 14:06
some clojure code to get some experts review it ..
(ns trip.core
(:require [[clj-time.core :as t]
[clojure.set :as set]]))
(defn default-trip
"returns a default trip with current date as start date & end date as 5 days from now"
[]
{ :name "New Trip"
:description "Enter your trip description here"
:start-date (t/today)
@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)))))))
@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 / 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 / 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 / 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 / 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)
}
def factorial(num : Int) : Int = {
if(num > 1) num * factorial (num-1) else num
}
factorial(5) ///will print 120