Skip to content

Instantly share code, notes, and snippets.

View DanielaSfregola's full-sized avatar

Daniela Sfregola DanielaSfregola

View GitHub Profile
@DanielaSfregola
DanielaSfregola / demo-part1.scala
Last active November 22, 2015 09:31
Scala Italy 2015: An Introduction to Akka and the Actor-Based Model: Demo. See article http://danielasfregola.com/2015/05/11/scala-italy-2015-highlights/
import akka.actor._
class SimpleActor extends Actor {
def receive = {
case msg => println(s"${self.path} - $msg")
}
}
@DanielaSfregola
DanielaSfregola / perfomance-script.scala
Last active March 29, 2016 06:09
A quick and dirty experiment to compare the performance of scala immutable collections (Seq, List, Vector) of integers when accessing randomly, appending, prepending an element. See article http://danielasfregola.com/2015/06/15/which-immutable-scala-collection.
import scala.concurrent.duration._
import scala.util.Random
import collection.immutable.Seq
private def prettyPrint(text: String)(duration: Duration): Unit =
println(s"[$text] - ${duration}")
private def ranges(base: Int, n: Int): Seq[Range] = for {
Overall, quite easy -- but I had to read ALL the release notes quite carefully before doing anything else.
My approach is to usually bump the version and go through each compilation error and check the release
notes for instructions on how to fix them.
Key points where I struggled:
- I initially just bumped the version of found that it couldn't find the `cats` module anymore
- I had to ask for help on gitter cause it wasn't able to make `mapN` (substitute of `|@|`) compile
because of SI-2712 fix missing
- when reading the documentation, more of us thought that `mapN` actually meant `map2`, `map3`,
`map4`, etc...maybe an explicit before/after example could help?
package io.paytouch.ordering.graphql
import org.json4s.JsonAST.JObject
/**
* Created by danielasfregola on 19/10/2017.
*/
case class GraphQLRequest(query: String, operationName: Option[String], variables: JObject = JObject())
@DanielaSfregola
DanielaSfregola / deadLettersChannelExample.scala
Last active May 23, 2018 19:03
An example on how to use the Dead Letters Channel in Akka. See article http://danielasfregola.com/2015/05/04/akka-dead-letters-channel/
import akka.actor._
class EchoActor extends Actor {
def receive = {
case msg => println(s"${self.path.name} - New msg received: $msg")
}
}
@DanielaSfregola
DanielaSfregola / customEventBusExample.scala
Last active October 12, 2019 07:46
An example on how to customize an Event Bus in Akka. See article http://danielasfregola.com/2015/04/20/peer-to-many-communication-in-akka/
import akka.actor._
import akka.event.{LookupClassification, EventBus}
import akka.event.ActorEventBus
case class Book(title: String, authors: List[String])
class AuthorBookBus(author: String) extends EventBus
with LookupClassification
with ActorEventBus {
@DanielaSfregola
DanielaSfregola / Applicative.scala
Last active August 16, 2020 03:05
tutorial-cat-solutions
package com.danielasfregola.tutorial.cat.applicative
import com.danielasfregola.tutorial.cat.functor.Functor
trait Applicative[Box[_]] extends Functor[Box] {
def pure[A](a: A): Box[A]
def ap[A, B](boxF: Box[A => B])(boxA: Box[A]): Box[B]

Advanced Functional Programming with Scala - Notes

Copyright © 2017 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
@DanielaSfregola
DanielaSfregola / MyConfig.scala
Last active March 20, 2022 18:33
A simple script that will try to load configurations in the following order: 1) From properly named environment variables 2) From command line paramenters 3) From the configuration file. See article http://danielasfregola.com/2015/06/01/loading-configurations-in-scala/
import com.typesafe.config.ConfigFactory
import scala.util.Properties
class MyConfig(fileNameOption: Option[String] = None) {
val config = fileNameOption.fold(
ifEmpty = ConfigFactory.load() )(
file => ConfigFactory.load(file) )
def envOrElseConfig(name: String): String = {
import akka.actor._
case class Book(title: String, authors: List[String])
class BookPublisher extends Actor {
def receive = {
case book: Book => {
println(s"Yeah! Publishing a new book: $book")
context.system.eventStream.publish(book)