View .block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
height: 600 |
View ShapelessCoproduct.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coproduct is extension of Either concept, to N multually exlusive choices | |
type ISB = Int :+: String :+: Boolean :+: CNil | |
val isb = Coproduct[ISB]("foo") //> isb : qaaz.ISB = foo | |
isb.select[Int] //> res0: Option[Int] = None | |
isb.select[String] //> res1: Option[String] = Some(foo) |
View MergeSort.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MergeSort { | |
// recursive merge of 2 sorted lists | |
def merge(left: List[Int], right: List[Int]): List[Int] = | |
(left, right) match { | |
case(left, Nil) => left | |
case(Nil, right) => right | |
case(leftHead :: leftTail, rightHead :: rightTail) => | |
if (leftHead < rightHead) leftHead::merge(leftTail, right) | |
else rightHead :: merge(left, rightTail) |
View Scala3TypeClassWithUsingGiven
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Scala3TypeClassWithUsingGiven { | |
trait MyMonoid[A] { | |
def combine(x: A, y: A): A | |
def empty: A | |
} | |
def combineList[A](li: List[A])(using monoid: MyMonoid[A]): A = li.foldLeft(monoid.empty)(monoid.combine) | |
def main(args: Array[String]): Unit = { |
View ConsumerExample.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util | |
import org.apache.kafka.clients.consumer.KafkaConsumer | |
import scala.collection.JavaConverters._ | |
object ConsumerExample extends App { | |
import java.util.Properties |
View TryFlatten.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.{Success, Failure} | |
val seq=Seq(Success(1), Failure(new Exception("bang")), Success(2)) | |
// all emit List(1, 2) | |
seq.map(_.toOption).flatten | |
seq.flatMap(_.toOption) | |
seq.filter(_.isSuccess).map(_.get) | |
seq.collect{case Success(x) => x} |
View CatsFlatmap.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats._ | |
import cats.implicits._ | |
// Example usage of Cats Flatmap | |
object CatsFlatmap extends App { | |
val listFlatMap=FlatMap[List] | |
val li=List(1,2,3) |
View Fs2Queues.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effect._ | |
import fs2._ | |
import scala.concurrent.duration._ | |
import cats.effect.std._ | |
// FS2 cats.effect.Queue example using flatMap or for comprehension | |
// Both streams emit nothing, but are effectful, communicating via the queue and updating a sum via the ref | |
object Fs2Queues extends IOApp.Simple { |
View MyErrsIO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MyErrsIO.Controller.{Request, postTransfer} | |
import MyErrsIO.Models.Account | |
import cats.data.Validated.{Invalid, Valid} | |
import cats.data.{EitherT, OptionT, Validated, ValidatedNec} | |
import cats.effect._ | |
import cats.implicits._ | |
import scala.util.control.NonFatal | |
// A simple example of cat-effect and error handling, with MonadTransformers |
View Insert.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val li=List(1,2,10,20,30) //> li : List[Int] = List(1, 2, 10, 20, 30) | |
def insert[T](li:List[T],x:T)(implicit cmp:Ordering[T])={ | |
val (first,last)=li.partition {cmp.lteq(_, x) } | |
first:::x::last | |
} //> insert: [T](li: List[T], x: T)(implicit cmp: Ordering[T])List[T] | |
insert(li,11) //> res0: List[Int] = List(1, 2, 10, 11, 20, 30) | |
insert(li,10) //> res1: List[Int] = List(1, 2, 10, 10, 20, 30) | |
insert(li,9) //> res2: List[Int] = List(1, 2, 9, 10, 20, 30) |
NewerOlder