Skip to content

Instantly share code, notes, and snippets.

@shayanjm
shayanjm / haversine.clj
Created April 12, 2015 20:15
Haversine Formula implementation in Clojure
; 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
@shayanjm
shayanjm / reverse-haversine.clj
Created March 23, 2015 17:25
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.
(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))))))]
@shayanjm
shayanjm / reverse-haversine.clj
Created April 12, 2015 22:01
"Reverse Haversine" Formula implementation in Clojure
; "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)
@shayanjm
shayanjm / test.clj
Created February 2, 2016 22:52
Kafka + Transit = Niceness
(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)
@shayanjm
shayanjm / iterable-seq.clj
Last active August 29, 2015 14:04
iter-seq function (Scala iterable -> Clojure lazy-seq) and implementation function
(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."
@shayanjm
shayanjm / gist:0b3f616a473885ab466a
Created August 3, 2014 05:13
Postfix notation in Clojure
(defmacro postfix-notation
"I'm too indie for prefix notation"
[expression]
(conj (butlast expression) (last expression)))
@shayanjm
shayanjm / sentiments.clj
Created July 17, 2014 15:59
Sentiment analysis using Stanford CoreNLP in Clojure
(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
@shayanjm
shayanjm / get-ngd.clj
Created July 15, 2014 20:46
A rough implementation of Normalized Google Distance in Clojure
; 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))
(time
(dosync
(ensure newswire)
(ref-set ref2 {:titles (:titles @newswire)
:urls (:urls @newswire)
:sentiments (pmap #(find-sentiment (get-nyt-article-contents %)) (:urls @newswire))})))
// problem 05 every some
/*
Return a function that takes a list of valid users, and returns a function that returns true
if all of the supplied users exist in the original list of users.
You only need to check that the ids match.
## Example
var goodUsers = [