Skip to content

Instantly share code, notes, and snippets.

@vdichev
Created June 17, 2017 06:46
Show Gist options
  • Save vdichev/c6accbc0d609f3c6aa633caa944e19f7 to your computer and use it in GitHub Desktop.
Save vdichev/c6accbc0d609f3c6aa633caa944e19f7 to your computer and use it in GitHub Desktop.
Scala exercises
Exercises
=========
Scala intro
-----------
1. Check if a string has balanced parentheses.
2. There is an inventory of items in a shop costing 615, 452, 356, 184, 172, 156, 135, 115, 78, 56, 48 lv. Find at least one way to spend exactly 992 lv. How about exactly 796 lv.?
Functional programming with Scala
---------------------------------
1. Implement a tail-recursive function "size" that takes a list and returns its length
2. Implement tail-recursive "filter" and "map" that take a list and a function as parameters
3. Implement "map" and "filter" in terms of "foldLeft"
Extra: Try to implement previous homework solutions recursively
Extra: Try to implement previous homework solutions with for comprehensions in terms of flatMap/filter/map
ADTs
----
1. Using the Option API (http://www.scala-lang.org/api/current/scala/Option.html) rewrite the following pattern matches with higher-order functions:
option match {
case None => None
case Some(x) => foo(x)
}
option match {
case None => None
case Some(x) => x
}
option match {
case None => None
case Some(x) => Some(foo(x))
}
option match {
case None => {}
case Some(x) => foo(x)
}
option match {
case None => false
case Some(_) => true
}
option match {
case None => true
case Some(_) => false
}
option match {
case None => true
case Some(x) => foo(x)
}
option match {
case None => false
case Some(x) => foo(x)
}
option match {
case None => foo
case Some(x) => Some(x)
}
option match {
case None => foo
case Some(x) => x
}
option match {
case None => Nil
case Some(x) => x :: Nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment