Skip to content

Instantly share code, notes, and snippets.

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
import io.Source
import scala.util.control.Breaks._
/**
* Scala TicTacToe game without any side effects
*
* Written in response to following post (which also contains task description):
* http://blog.tmorris.net/scala-exercise-with-types-and-abstraction/
*/
object TicTacToe {
@purijatin
purijatin / TraversableWithStatistics.scala
Last active March 15, 2017 10:03 — forked from MishaelRosenthal/TraversableWithStatistics.scala
Adding mean and variance to Scala collections.
object TraversableWithStatistics {
implicit class RichTraversable[A](collection: Traversable[A])(implicit val numeric : Numeric[A]) {
private def pow2[C](x: C)(implicit num: Numeric[C]) = math.pow(num.toDouble(x), 2)
private def sqrt[C](x: C)(implicit num: Numeric[C]) = math.sqrt(num.toDouble(x))
def mean = {
@purijatin
purijatin / YouAreQuiteGoodAtJava.java
Last active September 3, 2015 15:53 — forked from anonymous/isuckatjava.java
some java code
package com.jp;
import java.io.BufferedReader;
import java.io.FileReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.IntStream;
@purijatin
purijatin / jargon.md
Last active August 29, 2015 14:26 — forked from cb372/jargon.md
Category theory jargon cheat sheet

Category theory jargon cheat sheet

A primer/refresher on the category theory concepts that most commonly crop up in conversations about Scala or FP. (Because it's embarassing when I forget this stuff!)

I'll be assuming Scalaz imports in code samples, and some of the code may be pseudo-Scala.

Functor

A functor is something that supports map.

# Prints the Total heap usage of Java process
jdk1.7.0/bin/jstat -gccapacity 9043 | tail -n 1 | awk '{ print $4, $5, $6, $10 }' | python -c "import sys; nums = [x.split(' ') for x in sys.stdin.readlines()]; print(str(sum([float(x) for x in nums[0]]) / 1024.0) + ' mb');"