Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CS-210 Functional programming
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
lamp
CS-210 Functional programming
Commits
1a7d6ef0
Commit
1a7d6ef0
authored
3 years ago
by
Matt Bovel
Committed by
Olivier Blanvillain
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add exercises/solution-09.md
parent
601c7d57
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
exercises/solution-09.md
+30
-14
30 additions, 14 deletions
exercises/solution-09.md
with
30 additions
and
14 deletions
exercises/solution-09.md
+
30
−
14
View file @
1a7d6ef0
...
...
@@ -2,18 +2,18 @@
## Question 1
```
scala
```
scala
mdoc
def groupBy[T, S](f: T => S)(xs: List[T]): Map[S, List[T]] =
var
acc
=
Map
[
S
,
List
[
T
]]()
var
result
= Map[S, List[T]]()
xs.reverse.foreach(el =>
val key = f(el)
val
prevValue
=
acc
.
getOrElse
(
key
,
List
())
acc
=
acc
.
updated
(
key
,
el
::
prevValue
)
val prevValue =
result
.getOrElse(key, List())
result = result
.updated(key, el :: prevValue)
)
acc
result
```
```
scala
```
scala
mdoc
def groupBy2[T, S](f: T => S)(xs: List[T]): Map[S, List[T]] =
xs.foldRight(Map[S, List[T]]())((el: T, acc: Map[S, List[T]]) =>
val key = f(el)
...
...
@@ -24,21 +24,20 @@ def groupBy2[T, S](f: T => S)(xs: List[T]): Map[S, List[T]] =
Example run:
```
scala
```
scala
mdoc
groupBy[Int, Int](_ % 3)((0 to 10).toList)
// res0: Map[Int, List[Int]] = Map(
// 1 -> List(1, 4, 7, 10),
// 0 -> List(0, 3, 6, 9),
// 2 -> List(2, 5, 8)
// )
```
## Question 2
### Question 2.1
```
scala mdoc:invisible
trait Logger:
def log(message: String): Unit
```
```
scala
```
scala
mdoc
class LoggerBuffered extends Logger:
private var output: String = ""
def log(message: String): Unit = output = output + message + "\n"
...
...
@@ -47,8 +46,25 @@ class LoggerBuffered extends Logger:
### Question 2.2
```
scala mdoc:invisible
enum Expr:
case Var(name: String)
case Op(name: String, args: List[Expr])
import Expr._
final case class UnknownVarException(name: String) extends Exception
final case class UnknownOpException(name: String) extends Exception
def eval(e: Expr, context: Map[Var, Int]): Int = e match
case sym: Var if context.contains(sym) => context(sym)
case sym: Var => throw UnknownVarException(sym.name)
case Op("*", args) => args.map(eval(_, context)).foldLeft(1)(_ * _)
case Op("+", args) => args.map(eval(_, context)).foldLeft(0)(_ + _)
case Op(name, _) => throw UnknownOpException(name)
```
```
scala
```
scala
mdoc
def evalTracing(e: Expr, context: Map[Var, Int])(using logger: Logger): Int = e match
case sym: Var if context.contains(sym) =>
val value = context(sym)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment