Skip to content

Instantly share code, notes, and snippets.

View rajeevprasanna's full-sized avatar

Rajeev Kumar kallempudi rajeevprasanna

View GitHub Profile
@rajeevprasanna
rajeevprasanna / statefactorial.scala
Created June 29, 2017 10:35 — forked from calvinlfer/statefactorial.scala
Factorial using the State monad
import cats.data.State
import cats.data.State._
def factorialWithState(x: Int): Int = {
def stateFactorial: State[Int, Int] =
get.flatMap(x =>
if (x <= 1)
State.pure(1)
else {
set[Int](x - 1).flatMap(_ => stateFactorial.map(z => x * z))
//Problem : https://www.hackerrank.com/challenges/angry-children
// Algorithm :
// Read input into 2D lists => List(List(x))
// Sort every row of the matrix
// Then iterate over every column, check for unsorted first column
// break if we find unsorted column
object Test extends App {
@rajeevprasanna
rajeevprasanna / Solution.scala
Last active September 18, 2016 13:31
Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
object Solution {
def main(args: Array[String]) {
val sc = new java.util.Scanner (System.in);
var n = sc.nextInt();
var a = Array.ofDim[Int](n,n);
for(a_i <- 0 to n-1) {
for(a_j <- 0 to n-1){
a(a_i)(a_j) = sc.nextInt();
}
@rajeevprasanna
rajeevprasanna / Main.scala
Created June 12, 2016 05:14
file io with akka streams
import java.util.concurrent.ThreadLocalRandom
import akka.actor.ActorSystem
import akka.stream.{ClosedShape, ActorMaterializer}
import akka.stream.scaladsl.{Sink, Flow, Source, FileIO, GraphDSL, Broadcast, RunnableGraph}
import java.io.File
import akka.util.ByteString
@rajeevprasanna
rajeevprasanna / Main.scala
Created June 10, 2016 16:34
Twitter stream listener
import akka.actor.{ActorRef, ActorSystem}
import akka.stream.scaladsl._
import akka.stream.{OverflowStrategy, ActorFlowMaterializer}
import org.reactivestreams.Publisher
import twitter4j.{StallWarning, StatusDeletionNotice, TwitterStreamFactory, FilterQuery}
import twitter4j.{Status => TwitterStatus}
import com.typesafe.config.ConfigFactory
import twitter4j._
import twitter4j.conf.ConfigurationBuilder
@rajeevprasanna
rajeevprasanna / TransformationBackend.scala
Created May 13, 2016 11:58
Simple akka actor cluster example
import akka.actor.{RootActorPath, Actor, Props, ActorSystem}
import akka.cluster.{Member, MemberStatus, Cluster}
import akka.cluster.ClusterEvent.{CurrentClusterState, MemberUp}
import com.typesafe.config.ConfigFactory
/**
* Created by rajeevprasanna on 5/13/16.
*/
object TransformationBackend {
def main(args: Array[String]): Unit = {
import akka.actor.ActorSystem
import akka.http.Http
import akka.http.model.{StatusCodes, Uri, HttpResponse, HttpRequest}
import akka.stream.FlowMaterializer
import akka.stream.scaladsl.Flow
import akka.http.model.HttpMethods._
import play.api.libs.json.Json
import play.modules.reactivemongo.json.BSONFormats
import reactivemongo.bson.BSONDocument
@rajeevprasanna
rajeevprasanna / WebsocketExample.scala
Created May 6, 2016 11:33
simple websocket example
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.ws.{TextMessage, Message}
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Flow
import scala.concurrent.ExecutionContext.Implicits.global
object WebsocketExample extends App {
@rajeevprasanna
rajeevprasanna / Main.scala
Created May 6, 2016 10:50
Sample supervision strategy example
import akka.actor.SupervisorStrategy.{Escalate, Restart}
import akka.actor._
import scala.concurrent.duration._
object Main extends App {
val system = ActorSystem("supervisorStrategy")
implicit val context = system.dispatcher
import scala.concurrent.ExecutionContext.Implicits.global
@rajeevprasanna
rajeevprasanna / Main.scala
Created May 6, 2016 07:32
Simple message persistence example
import akka.actor.{Props, ActorSystem}
import akka.event.Logging
import akka.http.scaladsl.Http
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.marshalling.ToResponseMarshaller
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.http.scaladsl.server.RouteResult.Complete
import akka.http.scaladsl.unmarshalling.Unmarshal