Skip to content

Instantly share code, notes, and snippets.

View DanielaSfregola's full-sized avatar

Daniela Sfregola DanielaSfregola

View GitHub Profile

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
@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 {
@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 = {
@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 / 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 {
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)
@puffnfresh
puffnfresh / SimpleCheck.scala
Created August 9, 2014 17:17
Allow setting the specs2's ScalaCheck seed one sbt's command line
package com.simpleenergy
import java.util.Random
import org.specs2.ScalaCheck
import org.specs2.execute.{AsResult, Failure, Result}
import org.specs2.main.CommandLineArguments
import org.specs2.matcher.Parameters
import org.specs2.mutable.Specification
import org.specs2.specification.{Example, ExampleFactory}
import scalaz.syntax.monoid._
@mumoshu
mumoshu / read-a-file-using-stream.scala
Created December 9, 2011 00:58
Read a file using Stream (Scala)
import java.io.File
import java.io.FileInputStream
case class Chunk(length: Int, bytes: Array[Byte])
def fileContentStream(fileIn: FileInputStream): Stream[Chunk] = {
val bytes = Array.fill[Byte](1024)(0)
val length = fileIn.read(bytes)
Chunk(length, bytes) #:: fileContentStream(fileIn)
}