Skip to content

Instantly share code, notes, and snippets.

View krishnabhargav's full-sized avatar

Krishna Vangapandu krishnabhargav

View GitHub Profile
@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))
@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 / 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 / LuceneMain.scala
Created June 17, 2014 10:59
Lucene 4.3 Example
import scala.Array.canBuildFrom
import org.apache.lucene.analysis.standard.StandardAnalyzer
import org.apache.lucene.document.Document
import org.apache.lucene.document.Field
import org.apache.lucene.document.TextField
import org.apache.lucene.index.DirectoryReader
import org.apache.lucene.index.IndexWriter
import org.apache.lucene.index.IndexWriterConfig
import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser
@krishnabhargav
krishnabhargav / Places.scala
Created June 16, 2014 01:57
Scala + REST + JSON
//Imports for Dispatch
import dispatch._
import Defaults._
object Places {
def main(args: Array[String]) {
//Step 1 : Prepare the request object
@krishnabhargav
krishnabhargav / json2caseclass.scala
Created June 16, 2014 01:51
JSON deserialization in Scala
//CASE CLASSES
case class Location(lat: Double, lng: Double)
case class Geometry(location: Location)
case class RunningStatus(open_now: Boolean)
case class Results(geometry: Geometry, icon: Option[String], id: Option[String], name: Option[String],
opening_hours: Option[RunningStatus], price_level: Option[Double], rating: Option[Double],
reference: Option[String], types: Option[List[String]], vicinity: Option[String])
case class RootJsonObject(results: List[Results])
//JSON imports
@krishnabhargav
krishnabhargav / main.scala
Created June 16, 2014 01:42
Making a REST Service call in Scala using Dispatch
def main(args: Array[String]) {
//Step 1 : Prepare the request object
//even though its a https . doing a .secure is not required
val request = url("https://maps.googleapis.com/maps/api/place/nearbysearch/json")
val requestAsGet = request.GET //not required but lets be explicit
//Step 2 : Set the required parameters
val builtRequest = requestAsGet.addQueryParameter("key", "Almost gave away my key")
@krishnabhargav
krishnabhargav / build.sbt
Created June 16, 2014 01:23
build.sbt for scala + rest calls + json
name := "Places Demo"
version := "0.1"
scalaVersion := "2.10.2"
libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "dispatch-core" % "0.11.1", //Dispatch is a wrapper over async http library from apache
"org.json4s" %% "json4s-native" % "3.2.9", //json4s support for working with json serialization/deserialization
"org.json4s" %% "json4s-jackson" % "3.2.9")
(defn get-average-height
[people]
(let [people-with-height (filter #(contains? % :height) people)
heights (map #(:height %1) people-with-height)
total (count people-with-height)]
(/ (reduce + heights) total)))
(defn get-average-height2
[people]
(let [people-with-heights (filter #(contains? % :height) people)]
@krishnabhargav
krishnabhargav / StringMessageThrottler.cs
Created December 13, 2011 02:16
The class that takes care of throttling the string messages for 1/2 second and returns unique properties that changed
public class StringMessageThrottler
{
//Subject acts as the message bus. It is a IObservable<string> in our case.
private readonly ISubject<string> _eventObservables = new Subject<string>();
private readonly IObservable<string> _distinctPropertyChanged;
public StringMessageThrottler()
{
var eventsAggregatedForHalfSecond = _eventObservables.Buffer(TimeSpan.FromMilliseconds(500), Scheduler.ThreadPool);
//get the unique properties that were changed within the buffered time