Skip to content

Instantly share code, notes, and snippets.

View rajeevprasanna's full-sized avatar

Rajeev Kumar kallempudi rajeevprasanna

View GitHub Profile
import scala.concurrent.duration.Duration.Inf
import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
object App{
def main(args:Array[String]):Unit = {
import cats.syntax.cartesian._
import cats.instances.future._
val program = for {
@noelwelsh
noelwelsh / Website.scala
Created May 12, 2017 16:19
Simple website login implemented using the free monad
object Website {
import cats.free.Free
import cats.Comonad
import scala.io._
final case class User(username: String)
sealed trait Page
final case object Welcome extends Page
final case object TryAgain extends Page
@PeterPerhac
PeterPerhac / OptionTSequencing.scala
Last active June 29, 2017 10:50
Traversing OptionTs - how to get from List[OptionT[F,T]] to F[List[T]] by discarding None's
import cats.data.OptionT
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
object CollectingOptionTs {
def main(args: Array[String]): Unit = {
import cats.instances.future._
@jackcviers
jackcviers / MonadErrorExample.scala
Created March 27, 2017 21:59
MonadError example
import cats.MonadError
import cats.instances.either._
import cats.instances.future._
import cats.instances.try_._
import cats.syntax.applicativeError._
import cats.syntax.flatMap._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
@pbassiner
pbassiner / ComposingFutureListAndEither.scala
Last active November 24, 2024 19:30
From List[Either[A, B]] to Either[A, List[B]]
import scala.language.postfixOps
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import cats.data.EitherT
import cats.instances.list._
import cats.instances.future._
import cats.syntax.traverse._
@calvinlfer
calvinlfer / statefactorial.scala
Created November 30, 2016 17:26
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))
package futureeitherapplicativestack
import cats._
import cats.data._
import cats.implicits._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import cats.data._
import cats.syntax.traverse._
import cats.std.list._
import scala.util.Try
def tryInt(s: String): Try[Int] = Try(s.toInt)
def reader: Reader[Try[Int], String] = Reader[Try[Int], String] { t =>
t.map(s => s"$s is a number").getOrElse("not")
//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();
}