Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add links to diff preview
  • Loading branch information
hhandoko committed Sep 13, 2017
commit e776285b44fbb5e0085fc82d1b1ae40f9336ac49
26 changes: 18 additions & 8 deletions exercises/intro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Loop {
}
```

Source: [Loop1.java]
> Source: [Loop1.java]

This program is, on purpose, easy enough to understand. Consider however, that
in your real programming work you might deal with several complexities at the same
Expand Down Expand Up @@ -105,7 +105,7 @@ object Main extends App { // 1.
}
```

Source: [Loop2.scala]
> Source: [Loop2.scala]

First you notice that Scala is less verbose: no semicolons needed, no `public
static void main`, no explicit reassignment of `x` in `for` loop. Here are
Expand Down Expand Up @@ -200,7 +200,7 @@ def sum(xs: List[Int]): Int = {
val result = sum(square(filterEven(iterate(10))))
```

Source: [Loop3.scala]
> Source: [Loop3.scala]

Wow! The number of lines of code just exploded and we introduced a lot of
duplication along the way. Let's review the function body implementation:
Expand Down Expand Up @@ -342,7 +342,8 @@ def sum(xs: List[Int]): Int = {
}
```

Source: [Loop4.scala]
> Source: [Loop4.scala]<br>
> Diff: [Loop3.scala => Loop4.scala]

Here we pattern match on a list instead of integer. Note how we used the same
list construction syntax in the `case` expression to capture list's head `x`
Expand Down Expand Up @@ -390,7 +391,8 @@ def square(xs: List[Int]): List[Int] =
flatMap(xs, x => List(x * x))
```

Source: [Loop5.scala]
> Source: [Loop5.scala]<br>
> Diff: [Loop4.scala => Loop5.scala]

1. `flatMap` takes function `f` as its second argument;
2. `A => B` is type annotation for a function that takes an argument of type
Expand Down Expand Up @@ -441,7 +443,8 @@ def sum(xs: List[Int]): Int =
foldLeft(xs)(0)((acc, x) => acc + x)
```

Source: [Loop6.scala]
> Source: [Loop6.scala]<br>
> Diff: [Loop5.scala => Loop6.scala]

1. `foldLeft[A, R]` is parametrized on type of the input list `A` as well as
the type of its return value `R` thus `foldLeft` is a *generic* function;
Expand Down Expand Up @@ -486,7 +489,8 @@ def sumOfEvenSquares(max: Int): Int = {
}
```

Source: [Loop7.scala]
> Source: [Loop7.scala]<br>
> Diff: [Loop6.scala => Loop7.scala]

Here, `square` really does only what it says and doesn't mess with lists
anymore, which is now the responsibility of `map`.
Expand All @@ -511,7 +515,8 @@ def sumOfEvenSquares(max: Int): Int = {
}
```

Source: [Loop8.scala]
> Source: [Loop8.scala]<br>
> Diff: [Loop7.scala => Loop8.scala]

We use `1 to 10` directly instead of our own `iterate` function. `filter`,
`map`, `sum` as well as `flatMap`, `foldLeft` and many more functions are
Expand All @@ -532,8 +537,13 @@ transformations without changing the existing ones.
[Loop1.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop1.scala
[Loop2.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop2.scala
[Loop3.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop3.scala
[Loop3.scala => Loop4.scala]: https://rawgit.com/SingaporeScalaProgrammers/scala-workshop/master/references/intro/diff/Loop3-Loop4.html
[Loop4.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop4.scala
[Loop4.scala => Loop5.scala]: https://rawgit.com/SingaporeScalaProgrammers/scala-workshop/master/references/intro/diff/Loop4-Loop5.html
[Loop5.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop5.scala
[Loop5.scala => Loop6.scala]: https://rawgit.com/SingaporeScalaProgrammers/scala-workshop/master/references/intro/diff/Loop5-Loop6.html
[Loop6.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop6.scala
[Loop6.scala => Loop7.scala]: https://rawgit.com/SingaporeScalaProgrammers/scala-workshop/master/references/intro/diff/Loop6-Loop7.html
[Loop7.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop7.scala
[Loop7.scala => Loop8.scala]: https://rawgit.com/SingaporeScalaProgrammers/scala-workshop/master/references/intro/diff/Loop7-Loop8.html
[Loop8.scala]: https://github.com/SingaporeScalaProgrammers/scala-workshop/blob/master/references/intro/src/main/scala/Loop8.scala