Skip to content

Instantly share code, notes, and snippets.

@fancellu
fancellu / NullBooleanReads.scala
Created August 27, 2020 10:26
NullBooleanReads for play-json. Sometimes dirty json has a boolean of "null". This takes it to mean false
implicit object NullBooleanReads extends Reads[Boolean] {
def reads(json: JsValue): JsSuccess[Boolean] = json match {
case JsBoolean(b) => JsSuccess(b)
case _ => JsSuccess(false)
}
}
@fancellu
fancellu / CatsSemigroupK.scala
Created June 10, 2020 11:33
Cats SemigroupK vs Semigroup
import cats._
import cats.implicits._
// Cats SemigroupK vs Semigroup
object CatsSemigroupK extends App {
// semigroupK doesn't care about contents
@fancellu
fancellu / CatsFlatmap.scala
Last active August 15, 2022 14:45
Example usage of Cats FlatMap
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 June 9, 2020 11:15
Cats Apply, does cartesian maps, extends Functor and Semigroupal (which does cartesian joins, unlike pairwise Align)
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 / CovariantExample.scala
Created May 29, 2020 14:51
Scala example of declaration side Type Covariance
// 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 11:26
Example usage of Cats Bifunctor/Bifoldable/Bitraverse functions
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 07:44
Example usage of Cats Align
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 / OkScalaTorSocksSttp.scala
Last active March 2, 2024 08:12
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
// 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 / CatFold.scala
Created May 20, 2020 20:13
Example of Cats Foldable usage
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 13:27
PlayJson Example of how to use a strongly typed string via case class, yet not change json
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