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 scala.reflect.ClassTag
//ClassTag will make type info available that is erased by the JVM at runtime
def filterT[T :ClassTag](t:Any) : Boolean = t match {
case _:T => true
case _ => false
}
val anyL = List(1,"fe", 2, 3, 'a')
val examinePattern: (String, String, String) => String = {
case (pattern, "", _) => pattern
case (pattern, tail, orig) if tail.take(pattern.length) == pattern =>
examinePattern(pattern, tail.drop(pattern.length), orig)
case (pattern, _, orig) =>
examinePattern(orig.take(pattern.length + 1), orig, orig)
}
val runEp : String => String =
@richard-gibson
richard-gibson / mult-github-accounts.md
Created December 16, 2015 09:44
ssh config and usage for multiple github accounts

Add to ~/.ssh/config

Host github.com-foo
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_foo

Host github.com-bar
 HostName github.com
@richard-gibson
richard-gibson / MagnetExample.scala
Created June 14, 2015 23:40
Simple magnet example
object MagnetExample extends App {
sealed trait AdditionMagnet {
type Result
def apply():Result
}
object AdditionMagnet {
no_funs({M, _}) ->
{M,length(M:module_info(exports))}.
max_funs({Lm,Lsize},{Rm,Rsize}) ->
case Lsize =< Rsize of
true -> {Rm,Rsize};
false -> {Lm,Lsize}
end.
largest_module(Modules) ->