Skip to content

Instantly share code, notes, and snippets.

View krishnabhargav's full-sized avatar

Krishna Vangapandu krishnabhargav

View GitHub Profile
@krishnabhargav
krishnabhargav / HelloWorld.cs
Created December 7, 2011 03:47
C# Hello World
using System;
public static class HelloWorld{
public static void Main(){
Console.WriteLine("Welcome to Gist!");
}
}
@krishnabhargav
krishnabhargav / EventThrottlingTest.cs
Created December 13, 2011 01:53
Simple Event Throttling Requirement Expressed as a Test
[TestFixture]
public class TestEventThrottling
{
[Test]
public void EventsAreThrottled()
{
var fq = new Frequent();
int counter = 0;
fq.PropertyChanged += (s, e) => { counter++; };
foreach (var prop in Enumerable.Range(0, 200))
@krishnabhargav
krishnabhargav / Frequent.cs
Created December 13, 2011 02:10
Frequent class implementation
/// Krishna Vangapandu
public class Frequent : INotifyPropertyChanged
{
private string _property;
private readonly StringMessageThrottler _stringMessageThrottler;
public Frequent()
{
//Create a new message throttler. The implementation for this would be shown later.
_stringMessageThrottler = new StringMessageThrottler();
@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
(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 / 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")
@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 / 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 / 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 / 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