Skip to content

Instantly share code, notes, and snippets.

View JoolsF's full-sized avatar

Julian Fenner JoolsF

  • London
View GitHub Profile
@JoolsF
JoolsF / writer-monad-example-1.scala
Last active February 2, 2019 16:33
Writer Monad example 1
import cats.data.Writer
import cats.instances.vector._ // for Monoid
import cats.syntax.applicative._ // for pure
import cats.syntax.writer._
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
type Logged[A] = Writer[Vector[String], A]
@JoolsF
JoolsF / eithert-future-either-1.scala
Last active February 4, 2019 11:01
EitherT - FutureEither example 1
import FutureEither.FutureEither
import cats.data.EitherT
import scala.concurrent.{ExecutionContext, Future}
//ServiceError
sealed trait ServiceError {
val msg: String
}
@JoolsF
JoolsF / akka-streams-example-1.scala
Last active January 8, 2019 16:01
Akka stream example 1
import akka.Done
import akka.actor._
import akka.stream._
import akka.stream.scaladsl.Source
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success}
object TestStreamApp extends App {

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@JoolsF
JoolsF / fp-max.scala
Created November 17, 2018 18:05
FP to the max
import scala.util.Try
import scala.io.StdIn.readLine
//https://www.youtube.com/watch?v=sxudIMiOo68
object App0 {
def main: Unit = {
println("What is your name?")
val name = readLine()
@JoolsF
JoolsF / io-example-1.scala
Last active November 6, 2018 16:52
Simple toy example of IO
case class IO[A](unsafeRun: () => A) { self =>
def map[B](f: A => B): IO[B] = IO(() => f(self.unsafeRun()))
def flatMap[B](f: A => IO[B]): IO[B] = IO(() => f(self.unsafeRun()).unsafeRun())
}
def funcA(input: String): IO[Unit] = {
IO(() => println(input))
}
@JoolsF
JoolsF / cats-either-extensions-1.scala
Created September 29, 2018 18:42
cats either extensions 1
def squareString(s: String, p: Int => Boolean): Either[String, Int] = {
Either.fromTry( // Either from Try
Try {
val i = s.toInt
i * i
}
).leftMap { case _: NumberFormatException => "Boom - number format exception!" } // Left map
}.ensure("result must not equal 9")(p) // Predicate to check with result satisfies a predicate
val p: Int => Boolean = _ != 9
@JoolsF
JoolsF / cats-either-asRight.scala
Created September 29, 2018 17:21
Cats either asRight
case class Error(e: String)
/**
* These “smart constructors” have advantages over Left.apply and Right.apply
* because they return results of type Either instead of Left and Right.
* This helps avoid type inference bugs caused by over-narrowing
*/
def sumPositive(l: List[Int]): Either[Error, Int] = {
l.foldLeft(0.asRight[Error]) { (acc: Either[Error, Int], i: Int) =>
if (i > 0) {
@JoolsF
JoolsF / select-example-1.sh
Created August 13, 2018 10:29
db query shell script
#!/usr/bin/env bash
DB_USER="???"
DB_PASS="???"
DB_HOST="???"
if [ ! "$DB_USER" ] || [ ! "$DB_PASS" ] || [ ! "$DB_HOST" ]; then
echo "You need to set DB_USER, DB_PASS and DB_HOST environmental variables."
exit 1
@JoolsF
JoolsF / error_accumulation1.scala
Created July 17, 2018 14:20
Error accumulation 1 naive approach
private def accumulateErrors[A,B](list: List[A], f: A => B): Either[List[Throwable], List[B]] = {
val res = list.foldLeft(List[Error](), List[B]()) { case ((accError, accRes), item) =>
Try(f(item)).toEither match {
case Right(i) => (accError, accRes :+ i)
case Left(e) => (accError :+ new Error(e), accRes)
}
}
if(res._1.isEmpty) Right(res._2) else Left(res._1)
}