Skip to content

Instantly share code, notes, and snippets.

View fancellu's full-sized avatar

Dino Fancellu fancellu

View GitHub Profile
@fancellu
fancellu / HK1.scala
Last active May 18, 2020 14:08
Simple Scala higher kinded type example plus type class resolution, pure scala, no Cats
// Higher kinded type example
object HK1 extends App{
// Container C must take a type parameter
trait Maker[C[_]] {
def apply(i: Int): C[Int]
}
implicit object MakerList extends Maker[List] {
@fancellu
fancellu / LensApp.scala
Created April 28, 2020 14:29
Example usage of the Monocle Optics library showing manipulation of deeply nested immutable objects
import monocle.{Iso, Prism}
import monocle.macros.syntax.lens._
import monocle.macros.GenIso
object LensApp extends App{
case class Street(number: Int, name: String)
case class Address(city: String, street: Street, postcode: Option[String]=None)
case class Company(name: String, address: Address)
@fancellu
fancellu / FunctorApp.scala
Created April 26, 2020 12:45
Simple Scala Functor example, no cats
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]]
@fancellu
fancellu / MyMonoid.scala
Last active February 25, 2021 13:47
Little Monoid/Typeclass example from the ground up (no cats)
object MyMonoid extends App {
import scala.collection.immutable.HashMap
trait Monoid[A] {
def empty: A
def combine(x: A, y: A): A
}
object Monoid {
@fancellu
fancellu / SumGreaterThanOrEquals.scala
Last active February 25, 2021 13:48
tail-recursive check to see if sum of list greater or equals to max, better than simply summing every element on a long list
import Numeric.Implicits._
import Ordering.Implicits._
def sumGreaterThanOrEquals[T](xs: List[T], max: T)(implicit num: Numeric[T]): Boolean = {
@tailrec
def inner(xs: List[T], accum: T): Boolean = {
xs match {
case x :: tail => if (accum+x>= max) true else inner(tail, accum+x)
case Nil => false
@fancellu
fancellu / Sem1.scala
Created April 14, 2020 14:40
Zio example of Semaphore usage
import zio._
import zio.clock._
import zio.console._
import zio.duration._
import java.util.concurrent.TimeUnit
object Sem1 extends scala.App{
val runtime = Runtime.default
@fancellu
fancellu / FinchRestWithNoNulls.scala
Created April 8, 2020 12:58
Little Finch/Circe app, that shows how to get None not being emitted as Null
import cats.effect.IO
import com.twitter.app.Flag
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, ListeningServer, Service}
import com.twitter.server.TwitterServer
import com.twitter.util.Await
import io.finch.catsEffect._
import io.finch._
@fancellu
fancellu / UpdateWith.scala
Created February 28, 2020 23:39
Using updateWith/updatedWith to atomically update Map values
val m=Map("one"->1, "two"->2)
println(m)
// Map(one -> 1, two -> 2)
val newmap=m.updatedWith("one")(_.map(_+10))
println(newmap)
@fancellu
fancellu / TryTorSocks.scala
Last active February 25, 2021 13:48
Tiny App to show how to talk via Tor Socks Proxy
// run up Tor Browser first for it it kick off Tor Proxy)
import java.net.{InetSocketAddress, URL}
import java.net.Proxy
object TryTorSocks extends App {
val sockAddr = new InetSocketAddress("localhost", 9150)
val proxy = new Proxy(Proxy.Type.SOCKS, sockAddr)
val url = new URL("https://check.torproject.org/")
@fancellu
fancellu / FoldableFoldMExample.scala
Created October 15, 2019 18:05
Example usage of the Cats Foldable FoldM function
import cats._
import cats.data._
import cats.implicits._
// Example usage of the Cats Foldable FoldM function
object FoldableFoldMExample extends App{
def addIfNotTooBig(acc: Int, x: Int): Option[Int] = if (x > 8) none[Int] else (acc + x).some