Skip to content

Instantly share code, notes, and snippets.

View krishnabhargav's full-sized avatar

Krishna Vangapandu krishnabhargav

View GitHub Profile
@krishnabhargav
krishnabhargav / monad-examples.lhs
Last active August 29, 2015 14:10
monad examples that helped me understand .. in haskell
Functor & Fmap.
---------------
Lets start with fmap. fmap is defined in a Functor.
fmap takes a function and a box.
fmap then extracts the content from the box,
applies the function on the content and puts the result back into the box.
The signature of the fmap is
> fmap :: Functor f => (a -> b) -> f a -> f b
@krishnabhargav
krishnabhargav / websockettestclient.html
Created November 12, 2014 13:48
simple web socket test client
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script type="text/javascript">
var socket = new WebSocket("ws://localhost:8080/echo");
socket.onopen = function () {
console.log("Connected");
socket.send('hello, world!');
@krishnabhargav
krishnabhargav / build.sbt
Created November 12, 2014 13:46
atmosphere/nettosphere build.sbt
libraryDependencies ++= Seq(
"org.atmosphere" % "atmosphere-runtime" % "1.0.4",
"org.atmosphere" % "nettosphere" % "2.1.9")
resolvers += "Sonatype OSS Snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
@krishnabhargav
krishnabhargav / WebsocketServer.scala
Created November 12, 2014 13:43
Websocket server inside a scala application
import org.atmosphere.config.service.WebSocketHandlerService
import org.atmosphere.nettosphere.{Config, Nettosphere}
import org.atmosphere.websocket.{WebSocket, WebSocketHandlerAdapter}
object Main {
def main(args: Array[String]) {
val builder = new Config.Builder().host("127.0.0.1").port(8080)
@krishnabhargav
krishnabhargav / compose.scala
Created October 25, 2014 14:13
f compose g in Scala .. Chapter 2 FP In Scala
def compose[A,B,C] (f: B => C, g : A => B) : A => C = a => f(g(a))
@krishnabhargav
krishnabhargav / curry.scala
Last active August 29, 2015 14:08
implementing currying ... chapter 2 ... FP in Scala
def curry[A,B,C] (f: (A,B) => C): A => (B => C) = a => b => f(a,b)
curry((a:Int, b:Int) => a * b)(1)(2)
def uncurry [A,B,C] (f: A => B => C) : (A,B) => C = (a,b) => f(a)(b)
var f = uncurry (curry((a:Int, b: Int) => a * b)) // f (3,5) = 15
@krishnabhargav
krishnabhargav / isSorted.scala
Created October 21, 2014 11:52
isSorted for arrays and seq
def isSorted[A] (items : Seq[A], ordered : (A,A) => Boolean) : Boolean =
(items, items.tail).zipped.forall(ordered)
/*items match {
case null => true
case x :: Nil => true
case x :: y :: xs => ordered (x, y) && isSorted (xs, ordered)
}*/
isSorted(Seq(1,2,3), (a: Int, b: Int) => a < b);
@krishnabhargav
krishnabhargav / fib.scala
Created October 21, 2014 03:14
Functional Scala ... tail recursive computation of fib number
//tail recursive fibonacci numbers
def fib(n: Int) = {
@scala.annotation.tailrec
def fibImpl(a:Int, nxt: Int, res: Int) : Int =
a match {
case 0 => res
case _ => fibImpl(a-1, nxt+res, nxt)
}
fibImpl(n, 1, 0);
}
@krishnabhargav
krishnabhargav / index.html
Created September 23, 2014 01:44
clojurescript included html file
<html>
<body>
Welcome to clojure world!
<script type="text/javascript" src="js/goog/base.js"></script>
<script src="js/main.js" type="text/javascript"></script>
<script text="text/javascript">
goog.require("webapp.client");
</script>
@krishnabhargav
krishnabhargav / client.cljs
Last active August 29, 2015 14:06
simple hello alert message
(ns webapp.client
(:require
[figwheel.client :as fw :include-macros true]))
(fw/watch-and-reload)
(js/alert "Hello Zumanji")