Instantly share code, notes, and snippets.

# Shayan Mohantyshayanjm

• Sort options
Created Feb 2, 2016
Kafka + Transit = Niceness
View test.clj
 (ns project.core (:require [cognitect.transit :as transit]) (:use kafka-clj.client :reload) (:import [java.io ByteArrayInputStream ByteArrayOutputStream])) ;; Set up connector here (let [data (ByteArrayOutputStream. 4096) writer (transit/writer data :json)] (transit/write writer link-difference)
Created Apr 12, 2015
"Reverse Haversine" Formula implementation in Clojure
View reverse-haversine.clj
 ; "Reverse haversine" to derive lat/long from start point, bearing (degrees clockwise from north), & distance (km) ; φ2 = asin( (sin φ1 ⋅ cos δ) + (cos φ1 ⋅ sin δ ⋅ cos θ) ) ; λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 ) ; where φ is latitude, λ is longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance travelled, R the earth’s radius (defn reverse-haversine "Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair." [{lon :lng lat :lat bearing :bearing distance :distance}] (let [R 6378.137 ; Radius of Earth in km lat1 (Math/toRadians lat)
Created Apr 12, 2015
Haversine Formula implementation in Clojure
View haversine.clj
 ; Haversine formula ; a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2) ; c = 2 ⋅ atan2( √a, √(1−a) ) ; d = R ⋅ c ; where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km); (defn haversine "Implementation of Haversine formula. Takes two sets of latitude/longitude pairs and returns the shortest great circle distance between them (in km)" [{lon1 :lng lat1 :lat} {lon2 :lng lat2 :lat}] (let [R 6378.137 ; Radius of Earth in km
Created Mar 23, 2015
Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair.
View reverse-haversine.clj
 (defn reverse-haversine "Implementation of the reverse of Haversine formula. Takes one set of latitude/longitude as a start point, a bearing, and a distance, and returns the resultant lat/long pair." [{lon :long lat :lat bearing :bearing distance :distance}] (let [R 6378.137 ; Radius of Earth in km lat1 (Math/toRadians lat) lon1 (Math/toRadians lon) angdist (/ distance R) theta (Math/toRadians bearing) lat2 (Math/toDegrees (Math/asin (+ (* (Math/sin lat1) (Math/cos angdist)) (* (Math/cos lat1) (Math/sin angdist) (Math/cos theta))))) lon2 (Math/toDegrees (+ lon1 (Math/atan2 (* (Math/sin theta) (Math/sin angdist) (Math/cos lat1)) (- (Math/cos angdist) (* (Math/sin lat1) (Math/sin lat2))))))]
Last active Aug 29, 2015
iter-seq function (Scala iterable -> Clojure lazy-seq) and implementation function
View iterable-seq.clj
 (defn iter-seq "Takes a Scala iterable, and turns it into a lazy-seq" [iter] (lazy-seq (when (.hasNext iter) (cons (.next iter) (iter-seq iter))))) (defn iterable-seq "Takes a Scala iterable s, and returns a lazy-seq of its contents."
Created Aug 3, 2014
Postfix notation in Clojure
View gist:0b3f616a473885ab466a
 (defmacro postfix-notation "I'm too indie for prefix notation" [expression] (conj (butlast expression) (last expression)))
Created Jul 17, 2014
Sentiment analysis using Stanford CoreNLP in Clojure
View sentiments.clj
 (def nlp (let [props (new java.util.Properties)] (.setProperty props "annotators" "tokenize,ssplit,parse,sentiment") (new edu.stanford.nlp.pipeline.StanfordCoreNLP props))) (defn find-sentiment "Determines the sentiment of each sentence in a given glob of text. Results in a collection of integers ranging from [0-4]: where 0 is 'Very negative', 2 is 'neutral', and 4 is 'Very positive'" [blob] (let [glob (apply str blob)] (let [main-sentiment 0
Created Jul 15, 2014
A rough implementation of Normalized Google Distance in Clojure
View get-ngd.clj
 ; Rough implementation of Normalized Google Distance algorithm ; Assumed total number of indexed pages = 42,000,000,000 (defn get-ngd "Returns the normalized google distance of two searchable terms. Returns nil if no results available for either query, or if there is no overlap for either query. The closer the result trends to 0, the more closely 'related' the terms are." [term1 term2] (let [m 42000000000 fx (Integer. (:totalResults (:searchInformation (google-search term1)))) fy (Integer. (:totalResults (:searchInformation (google-search term2)))) fxy (Integer. (:totalResults (:searchInformation (google-search (str term1 "+" term2))))) ngdnumerator (- (max (math/log10 fx) (math/log10 fy)) (math/log10 fxy))
Created Jun 27, 2014