Skip to content

Instantly share code, notes, and snippets.

Justin Heyes-Jones justinhj

Block or report user

Report or block justinhj

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
View PE2.scala
sealed trait BankAccountCommand[R] extends ReplyType[R]
case class DepositCmd(time: Instant, description: String, amount: Int)
extends BankAccountCommand[Int]
case class WithdrawCmd(time: Instant, description: String, amount: Int)
extends BankAccountCommand[WithdrawalResponse]
View pe1.scala
case class AccountState(balance: Int)
class BankAccountEntity extends PersistentEntity {
override type Command = BankAccountCommand[_]
override type Event = BankAccountEvent
override type State = AccountState
override def initialState : State = AccountState(0)
def behaviour = ??? // To come later
@justinhj
justinhj / CustomEC.scala
Created Nov 12, 2019
Custom Scala execution context
View CustomEC.scala
// Create a custom threadpool and execution context for Akka to use
val executor: ExecutorService = Executors.newFixedThreadPool(4, new ThreadFactory() {
override def newThread(r: Runnable): Thread = {
val t = Executors.defaultThreadFactory.newThread(r)
t.setDaemon(true)
t
}
})
@justinhj
justinhj / listoptionfor.scala
Created Sep 11, 2019
List and Option in for
View listoptionfor.scala
// Ammonite repl output
@ desugar {
for (
s <- List(1,2,3);
o <- if(s % 2 == 1) Some(s) else None
) yield (s + o)
}
res12: Desugared = scala.collection.immutable.List.apply[Int](1, 2, 3).flatMap[Int, Any](((s: Int) => scala.Option.option2Iterable[Int]((if (s.%(2).==(1))
scala.Some.apply[Int](s)
@justinhj
justinhj / EitherTLogging.scala
Created Jul 28, 2019
Error handling and logging
View EitherTLogging.scala
import cats._
import cats.data._
import cats.implicits._
object EitherTLogging {
def divide[F[_]](dividend: Int, divisor: Int)(implicit F: ApplicativeError[F, String]): F[Int] = {
if (divisor === 0) F.raiseError("division by zero")
else F.pure(dividend / divisor)
}
View WriterTFailure.scala
import cats._
import cats.instances._
import cats.syntax._
import cats.data.Validated._
type ErrorOr[A] = Validated[String, A]
def logDivide(a: Int, b: Int) = WriterT[ErrorOr, String, Int](
divide[ErrorOr](a,b) match {
case Invalid(e) => Invalid("Division by zero\n")
@justinhj
justinhj / typeclasspatternexample.scala
Created Jul 16, 2019
From @kaishh on functional programming Slack
View typeclasspatternexample.scala
// kaishh [4:20 AM]
// Found a neat way to always have typeclass syntax available without any wildcards imports:
package mypkg {
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
final class FunctorOps[F[_], A](private val fa: F[A]) extends AnyVal {
def map[B](f: A => B)(implicit F: Functor[F]): F[B] = F.map(fa)(f)
}
View FizzBuzzFs.scala
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).
View fizzbuzz2.scala
@ 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 / fizzbuzz.scala
Created Jul 13, 2019
Fizz buzz as FS2 streams
View fizzbuzz.scala
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(
You can’t perform that action at this time.