Zainab Ali
@_zainabali_
Your colleague
Interested in functional programming
www.scala-exercises.org
def collapse(xs: List[Int]): Int = {
xs match {
case Nil => WRITE_YOUR_ANSWER
case h :: t => WRITE_YOUR_ANSWER
}
}
def collapse(xs: List[Int]): Int = WRITE_YOUR_ANSWER
def collapse(xs: List[Int],
b: String,
f: (Int, String) => String): String = WRITE_YOUR_ANSWER
check
next
test
Tutorial
VSCode
Bloop
def collapse(xs: List[Int]): Int = {
xs match {
case Nil => 0
case h :: t => h + collapse(t)
}
}
def collapse(xs: List[Int]): Int = xs.head + collapse(xs.tail)
def collapse(o: Option[String]): String = {
o match {
case None => WRITE_YOUR_ANSWER
case Some(x) => WRITE_YOUR_ANSWER
}
}
def collapse(xs: List[Int]): Int = {
xs match {
case Nil => WRITE_YOUR_ANSWER
case h :: t => WRITE_YOUR_ANSWER
}
}
def collapse(xs: List[Int]): Int = {
xs match {
case Nil => WRITE_YOUR_ANSWER
case h :: t => WRITE_YOUR_ANSWER
}
}
Multiply
Concatenate
Sum
def problem(inputType: String,
outputType: String,
paramName: String,
patterns: List[String]): String = {
val cases = patterns
.map(p => s"case $p => WRITE_YOUR_ANSWER")
.mkString("\n")
s"""
def collapse($paramName: $inputType): $outputType = {
$paramName match {
$cases
}
}
"""
}
val optionProblem =
problem("Option[Int]", "Int", "o", List("None", "Some(x)"))
case class Type(name: String)
case class Pattern(value: String)
case class ParamName(value: String)
Validation
Algebraic Data Types
def validateType(name: String): Either[Error, Type] =
if (name.isEmpty) {
Left("It's empty!")
} else if (name.contains(" ")) {
Left("It contains spaces")
} ...
def problem(inputType: Type,
outputType: Type,
paramName: Term.Name,
patterns: List[Pat]): Tree = {
val cases = patterns
.map(p => p"case $p => WRITE_YOUR_ANSWER")
q"""
def collapse($paramName: $inputType): $outputType = {
$paramName match {
..case $cases
}
}
"""
}
val optionProblem =
problem(t"Option[Int]", t"Int", q"o", List(q"None", p"Some(x)"))
def collapse(xs: List[Int]): Int = WRITE_YOUR_ANSWER
pattern matching
concrete types
functions
higher
kinded
types
partial functions
type parameters
implicits
dependent types
variance
Language subsets
check
next
test
Tutorial
VSCode
Bloop
def bannedFeatures(answer: Tree,
featureChecks: List[Tree => Boolean]): List[Tree] =
answer.collect {
case node if featureChecks.exists(check => check(node)) => node
}
def partialFunctionCheck: Tree => Boolean = {
case partial: Term.PartialFunction => true
case _ => false
}
def newFeatures(code: Tree,
checks: List[Tree => Boolean]): List[Tree] =
code.collect {
case node if checks.exists(check => check(node)) =>
node
}
foldRight(List(1, 2, 3), Nil)(Cons(_, _))
Cons(1, Cons(foldRight(List(2, 3), Nil)(Cons(_, _))))
Cons(1, Cons(2, Cons(3, foldRight(Nil, Nil)(Cons(_, _)))))
Cons(1, Cons(2, Cons(3, Nil)))
Functional Programming Patterns and Their Role in Instruction, Eugene Wallingford
Bringing Scala to a Diverse Group of Students,
Noel Welsh / Elissavet Velli / Yifan Xing