This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn are-consecutive? [input] | |
(let [sorted (sort input) | |
diff (map - (rest sorted) sorted) | |
all-one? (every? #(= 1 %) diff)] | |
all-one?)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def factorial(num : Int) : Int = { | |
if(num > 1) num * factorial (num-1) else num | |
} | |
factorial(5) ///will print 120 |