Skip to content

Instantly share code, notes, and snippets.

Dino Fancellu fancellu

Block or report user

Report or block fancellu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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 / OkScalaTorSocks.scala
Created May 21, 2020
Shows how to use okhttp in Scala asynchronously to talk to a Tor onion address via a Tor socks proxy, without trying to resolve DNS outside of proxy
View OkScalaTorSocks.scala
// Shows how to use okhttp 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 += "com.squareup.okhttp3" % "okhttp" % "4.7.2"
import java.io.IOException
import java.net.{InetSocketAddress, Proxy}
import okhttp3.{Call, Callback, OkHttpClient, Request, Response}
@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 / PlayJsonAnyVal.scala
Last active May 18, 2020
PlayJson Example of how to use a strongly typed string via case class, yet not change json
View PlayJsonAnyVal.scala
import play.api.libs.json._
object PlayJsonAnyVal extends App {
final case class Address(path: String) extends AnyVal
object Address {
// this allows Address to be used as a map key, even though it is not a String
implicit val keyReads: KeyReads[Address]= key => JsSuccess(Address(key))
implicit val keyWrites: KeyWrites[Address] = _.path
@fancellu
fancellu / CatFunc.scala
Created May 14, 2020
Example of Functor usage in Cats for a custom type vs standard List
View CatFunc.scala
import cats._
import cats.data._
import cats.syntax._
import cats.implicits._
object CatFunc extends App {
// a very naive linked list implementation and functor for it
trait MyList[+A]
case class MyNode[A]( head: A, tail: MyList[A]) extends MyList[A]
@fancellu
fancellu / SimType.scala
Last active May 18, 2020
Example usage of Simulacrum Typeclass, v similar to math.Ordering/Numeric in standard SDK
View SimType.scala
import simulacrum._
// Example usage of Simulacrum, v similar to math.Ordering/Numeric in standard SDK
object SimType extends App {
@typeclass trait Showing[T] {
def show(x: T): String
}
@typeclass trait Ordering[T] extends Showing[T]{
@fancellu
fancellu / Ev.scala
Created May 13, 2020
Cats Eval example
View Ev.scala
import cats.Eval
object Ev extends App{
// eager, method call
val always: Eval[Int] =Eval.always{
println("running always")
123
}
// lazy, memoized
@fancellu
fancellu / Unapp.scala
Last active May 18, 2020
Simple example of custom extractor objects vs existing case class extractors
View Unapp.scala
object Unapp extends App {
case class MyClass(age: Int, name: String)
private object Age {
def unapply(myClass: MyClass): Option[Int] = myClass match {
case MyClass(age, _) if (age > 0) => Option(age)
case _ => None
}
}
You can’t perform that action at this time.