Skip to content

Instantly share code, notes, and snippets.

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

A sample cla file.

<?xml version="1.0" encoding="UTF-8"?>
<!--Generated by Standards Editor (build:R1.6.16) on 2020 Mar 05 10:41:58, ISO 20022 version : 2013-->
<xs:schema xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.09" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.09">
<xs:element name="Document" type="Document"/>
<xs:complexType name="AccountIdentification4Choice">
<xs:choice>
<xs:element name="IBAN" type="IBAN2007Identifier"/>
<xs:element name="Othr" type="GenericAccountIdentification1"/>
</xs:choice>
</xs:complexType>
object TicTocToe {
sealed trait Action
object Action {
case object X extends Action
case object O extends Action
}
sealed trait Player
object Player {
case object X extends Player
@khajavi
khajavi / Languages.java
Last active February 16, 2020 15:38
How to wrap Java Raw Types in Scala
import java.util.HashSet;
import java.util.Set;
// This is a Java class with wildcards
public class Wild {
Set<?> contents() {
Set<String> languages = new HashSet<>();
languages.add("Scala");
languages.add("Haskell");
languages.add("Closure");
/**
* Represents a mode that decides how to deal exceed rate for Throttle operator
*/
sealed abstract class ThrottleMode
object ThrottleMode {
/**
* Tells throttle to make pauses before emitting messages to meet throttle rate
*/
@khajavi
khajavi / factorial.scala
Created March 19, 2019 17:07
Akka strems factorial
object Factorial extends App {
implicit val system = ActorSystem("QuickStart")
implicit val materializer = ActorMaterializer()
val source: Source[Int, NotUsed] = Source(1 to 100)
val factorials: Source[BigInt, NotUsed] = source.scan(BigInt(1))((acc, next) => acc * next)
val result: Future[IOResult] =
factorials
@khajavi
khajavi / parse.scala
Created January 29, 2019 08:53
Add custom formatter for json4s
import org.json4s._
import org.json4s.jackson.JsonMethods._
implicit val formats: DefaultFormats = new DefaultFormats {
override def dateFormatter: SimpleDateFormat = {
val f = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy")
f.setTimeZone(DefaultFormats.UTC)
f
}
@khajavi
khajavi / three-flavours-of-request-response.scala
Created August 5, 2017 10:24
Three flavours of request-response pattern in Akka
// More info: http://www.nurkiewicz.com/2014/01/three-flavours-of-request-response.html
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Cancellable, Props, Terminated}
import akka.event.LoggingReceive
import akka.util.Timeout
import akka.pattern.pipe
import scala.concurrent.duration._
import scala.language.postfixOps
@khajavi
khajavi / QuickSort.scala
Created August 5, 2017 10:21
Recursive QuickSort
object QuickSort extends App {
val list = List(3, 5, 7, 1, 4, 9, 44, 2)
println(quickSort(list))
def quickSort(list: List[Int]): List[Int] = {
list match {
case Nil => Nil
case pivot :: tail =>
val (less, greater) = tail.partition(_ < pivot)
quickSort(less) ::: pivot :: quickSort(greater)