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
@huitseeker
huitseeker / Point.scala
Last active December 19, 2015 05:08
Triggering Implicit search by emulating double dispatch using simple dispatch
object Point {
trait PointAdder[P2] {
def add(p2: P2): Point3D
}
case class Point2D(x: Int, y: Int)
case class Point3D(x: Int, y: Int, z: Int)
implicit class Point2Adder(p1:Point2D) extends PointAdder[Point2D] {
@ahoy-jon
ahoy-jon / Point.scala
Last active December 19, 2015 04:49 — forked from fsarradin/Point.scala
object Point extends App {
trait Semigroup[T] {
def append(p1: T, p2: T): T
}
case class Point2D(x: Int, y: Int)
case class Point3D(x: Int, y: Int, z: Int)
object Point2D {
@fsarradin
fsarradin / Point.scala
Created July 1, 2013 08:24
PointAdder and scala type class
object Point {
trait PointAdder[P1, P2] {
def add(p1: P1, p2: P2): Point3D
}
case class Point2D(x: Int, y: Int)
case class Point3D(x: Int, y: Int, z: Int)
implicit object Point2DAdder extends PointAdder[Point2D, Point2D] {
import org.specs2.mutable._
class ImplicitiSpec extends Specification {
type Store=Map[String,String]
object Value {
def apply(k:String)(implicit map:Store):String={
println(s"reads value for ${k}")
map.get(k).getOrElse("")
}
@fbiville
fbiville / ZuperDao
Last active December 12, 2015 05:48
Refactoring puzzler: how to remove duplication here?
StringBuilder whereConstraints = new StringBuilder();
if (!isEmpty(typesActu)) {
whereConstraints.append(" AND a.type_id IN (?) ");
}
if (!isEmpty(newsletters)) {
whereConstraints.append(" AND (ap.profils_id IN (?) OR ap.profils_id IS NULL)");
}
if (!isNullOrEmpty(term)) {
whereConstraints.append(" AND (titre LIKE (?) OR texte_complet LIKE(?) ) ");
}
anonymous
anonymous / Option.java
Created January 4, 2013 13:42
Here is an Option class (and sub-classes) compatible with JDK 8. Its implementation has been inspired from Scala and Tony Morris' article http://blog.tmorris.net/maybe-in-java/
package my.util;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.function.Function;
public abstract class Option<T> implements Iterable<T> {
@tonymorris
tonymorris / Balance.scala
Created September 23, 2012 01:43
Balance Parentheses
// Missing support libraries
object MissingLibraries {
case class State[S, +A](run: S => (A, S)) {
def map[B](f: A => B): State[S, B] =
State(s => {
val (a, t) = run(s)
(f(a), t)
})
def flatMap[B](f: A => State[S, B]): State[S, B] =
@ericlemerdy
ericlemerdy / gist:1721193
Created February 2, 2012 03:15
Cannot decide: guava or plain old for loop
public Boolean hasStraightFlush_plainJava() {
Suit expectedSuit = cards.get(0).getSuit();
int minValue = cards.get(0).getValue().ordinal();
for (int i = 1; i < 5; i++) {
Card card = cards.get(i);
if (card.getValue().ordinal() != minValue + i) {
return false;
}
if (card.getSuit() != expectedSuit) {
return false;
@fsarradin
fsarradin / Isin.scala
Created December 11, 2011 23:06
Check if a given code is a consistent with the ISIN standard
package isin
object Isin {
// based on http://en.wikipedia.org/wiki/International_Securities_Identification_Number
def isIsin(code: String): Boolean = {
val digits: String = code.init.map(c => BigInt(c.toString, 36)).mkString
val parity = digits.size & 1
val total: Int = digits.zipWithIndex.map {
case (c: Char, i: Int) =>