Skip to content

Instantly share code, notes, and snippets.

View justinhj's full-sized avatar

Justin Heyes-Jones justinhj

View GitHub Profile
@justinhj
justinhj / ContravariantAndDivideExample.scala
Created May 21, 2019 16:51
Example of contramap and divide using Scalaz
package examples
import scalaz._, Scalaz._
object ContravariantMap {
// Example of contravariant using Scalaz
trait Printable[A] {
def format(value: A): String
}
@justinhj
justinhj / MonoidComposition.scala
Created June 12, 2019 16:32
Monoid composition
// Tuples, integers and maps all have monoid
// instances we can easily combine them
@ val data = "Hello Hello This is some data data data yo"
data: String = "Hello Hello This is some data data data yo"
@ def step(word: String) = (1, word.length, Map(word -> 1))
defined function step
@ def run(data: String) = data.split(" ").toList.map(step).combineAll
// Comonad is the dual of Monad
// example for non empty list, extract is the head of the list
// coflatMap gives you successive lists from the whole list, the tail, the tail of that
// and you must return a single value for each list...
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1,2,3))
res26: Int = 1
@ Comonad[NonEmptyList].extract(NonEmptyList.of(1))
res27: Int = 1
@justinhj
justinhj / StateTEither.scala
Created June 23, 2019 21:48
StateT with Either for error handling
import cats._
import cats.implicits._
import cats.data.StateT
object Radio {
case class Radio(volume: Int)
type RadioError = String
type RadioEither[A] = Either[RadioError, A]
type RadioStateEitherT[A] = StateT[RadioEither, Radio, A]
@justinhj
justinhj / ActorWithQueue.scala
Created July 4, 2019 12:50
Pub Sub Akka actor with Zio Queue
import zio._
import zio.console._
import zio.{Queue, UIO}
import akka.actor._
import com.typesafe.config.ConfigFactory
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext}
import scala.concurrent.duration.FiniteDuration
import java.util.concurrent.{Executors, TimeUnit}
import java.util.concurrent.{ExecutorService, Executors, ThreadFactory}
@justinhj
justinhj / lolcompose.scala
Created July 9, 2019 19:08
Flatmap from unit and compose
object ComposePlay {
// Compose takes two Kleisli arrows ( A => F[B], B => F[C] ) and returns A => F[C]
// Here I implement unit and compose for Option
// Then flatMap is implemented using compose showing that you can make a Monad
// with unit and compose...
def unitOption[A](a : A) : Option[A] = Some(a)
@justinhj
justinhj / uPickleRules.scala
Last active July 10, 2019 18:23
Serialize and deserialize a map with a case class key
// "com.lihaoyi" %% "upickle" % "0.7.5",
import upickle.default.{read,write}
import upickle.default.{ReadWriter => RW, macroRW}
object PickleMap {
case class KeyClass(s: String, i: Int)
object KeyClass{
implicit val rw: RW[KeyClass] = macroRW
@justinhj
justinhj / fizzbuzz.scala
Created July 13, 2019 17:57
Fizz buzz as FS2 streams
val fb = Stream.emits(List(false, false, true)).repeat.zip(Stream.emits(List(false,false,false,false,true)).repeat).map{
case (true,true) => "fizzbuzz"
case (true,false) => "fizz"
case (false,true) => "buzz"
case (false,false) => ""
}
//fb: Stream[Nothing, String] = Stream(..)
// @ fb.take(30).toList
// res27: List[String] = List(
@ val fb = Stream.emits(List("", "", "fizz")).repeat.zip(Stream.emits(List("", "", "", "", "buzz")).repeat).map{case (a,b) => a |+| b}
fb: Stream[Nothing, String] = Stream(..)
@ fb.take(30).toList
res37: List[String] = List(
"",
"",
"fizz",
"",
"buzz",
@justinhj
justinhj / FizzBuzzFs.scala
Created July 15, 2019 17:07
Another fizz buzz
import fs2._
import cats._
import cats.implicits._
object Fs2FizzBuzz {
val fb = Stream.emits(List(None, None, "Fizz".some)).
repeat.
zip(Stream.emits(List(None, None, None, None, "Buzz".some)).
repeat).