Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / ConsumerExample.scala
Last active Jul 8, 2020
Kafka Producer/Consumer Example in Scala
View ConsumerExample.scala
import java.util
import org.apache.kafka.clients.consumer.KafkaConsumer
import scala.collection.JavaConverters._
object ConsumerExample extends App {
import java.util.Properties
@fancellu
fancellu / CatsSemigroupK.scala
Created Jun 10, 2020
Cats SemigroupK vs Semigroup
View CatsSemigroupK.scala
import cats._
import cats.implicits._
// Cats SemigroupK vs Semigroup
object CatsSemigroupK extends App {
// semigroupK doesn't care about contents
@fancellu
fancellu / CatsFlatmap,scala
Created Jun 10, 2020
Example usage of Cats FlatMap
View CatsFlatmap,scala
import cats._
import cats.implicits._
// Example usage of Cats Flatmap
object CatsFlatmap extends App {
val listFlatMap=FlatMap[List]
val li=List(1,2,3)
@fancellu
fancellu / CatsAp.scala
Created Jun 9, 2020
Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align)
View CatsAp.scala
import cats._
import cats.implicits._
// Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align)
object CatsAp extends App {
val apOption=Apply[Option]
val option1: Option[(Int, Int)] =apOption.product(Option(1), Option(2))
@fancellu
fancellu / OkScalaTorSocksSttp.scala
Last active Jun 3, 2020
Shows how to use okhttp and sttp in Scala asynchronously to talk to a Tor onion address via a Tor socks proxy, without trying to resolve DNS outside of proxy
View OkScalaTorSocksSttp.scala
// Shows how to use okhttp and sttp in Scala asynchronously to talk to a Tor onion address via a Tor socks proxy,
// without trying to resolve DNS outside of proxy (few http clients do this properly and instead return UnknownHostException)
// libraryDependencies ++= Seq("com.squareup.okhttp3" % "okhttp" % "4.7.2",
// "com.softwaremill.sttp.client" %% "okhttp-backend" % "2.1.2"))
import java.io.IOException
import java.net.{InetSocketAddress, Proxy}
import okhttp3.{Call, Callback, OkHttpClient, Request, Response}
@fancellu
fancellu / CovariantExample.scala
Created May 29, 2020
Scala example of declaration side Type Covariance
View CovariantExample.scala
// take out the + and see the last few lines fail as doit then only takes Animal, and not its subtypes
// we output A, so + is fine, we are covariant
sealed trait ThisThatValue[+A]
object ThisThatValue {
final case class This[+A](value: A) extends ThisThatValue[A]
final case class That[+A](value: A) extends ThisThatValue[A]
}
@fancellu
fancellu / CatsBi.scala
Created May 26, 2020
Example usage of Cats Bifunctor/Bifoldable/Bitraverse functions
View CatsBi.scala
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
object CatsBi extends App {
val fail = Left("failed")
val ok = Right(123)
@fancellu
fancellu / CatsAlign.scala
Created May 26, 2020
Example usage of Cats Align
View CatsAlign.scala
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
object CatsAlign extends App {
val alOption=Align[Option]
val iorO: Option[Ior[Int, Int]] =alOption.align(Option(1), Option(2))
@fancellu
fancellu / CatFold.scala
Created May 20, 2020
Example of Cats Foldable usage
View CatFold.scala
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
object CatFold extends App{
// list of Tuple[Int, Double]
println(List((20,1.1),(29,2.2)).combineAllOption)
@fancellu
fancellu / FunctorApp.scala
Created Apr 26, 2020
Simple Scala Functor example, no cats
View FunctorApp.scala
package example
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
def lift[A, B](f: A => B): F[A] => F[B] = map(_)(f)
}
object Functor {
def apply[F[_] : Functor]: Functor[F] = implicitly[Functor[F]]
You can’t perform that action at this time.