Functional Programming in Scala: Weeks 5 and 6
Contents
List methods:
-
How to create the methods like
take
,drop
,reverse
,append
, etc. on Lists. -
Implementation of mergesort.
|
|
Tuple class:
The Tuple
class is implemented using TupleN
class, where N is the
number of elements in the tuple:
|
|
Therefore, a tuple creates fields with names \_1, \_2, etc.
Implicit Parameters
One way is to parameterize the type, which would not work unless we pass
a ordering function as well since not every type has <
defined:
|
|
And then we can go ahead and use Ordering
trait so that the class T
using msort has to have lt
method defined for it.
|
|
Implicit Ordering:
We can also use the implicit
keyword which will implicitly pass the
ordering operator based on the given type:
|
|
For this to work, the compiler looks at any definition which - is marked implicit - has a compatible type T - is visible at the point of the function call, or is defined at the companion object.
Higher order functions:
- I already used these in the previous section’s assignment.
filter
,filterNot
,partition
takeWhile
,dropWhile
,span
- This is how the map method may be defined:
|
|
-
Then come folds, accumulate, etc.
-
In a function, confusingly enough, each
_
defines a new parameter:(x, y) => (x * y)
is equivalent to(_ * _)
|
|
Other Sequences:
-
Vector:
-
Provides a better access to the elements
-
In number of elements < 32,
-
Telephone problem:
I started with the implementation of charCode
as:
|
|
because I totally forgot that the for
expression allows for multiple
generators:
|
|
which provides a clearer way of doing things.
In the lecture this was one of the reasons of providing for
expression
syntax but I could not think about it.
Exercise
-
Given a List of strings, the first way I could think of was to concatenate the strings using
xs mkString ""
, but that seems rather odd and inelegant. The second approach was to usefold
likexs.foldLeft("")(_ ++ _)
-
Why did they use a
List[Char]
instead of aString
as the key of the dictionary object? -
Some of the questions were using
Map
, which is both a function as well as a collection. Therefore, we can get the value stored in it by usingmapname(key)
. But this explodes if there is no key. One way of safely handling this is to use theget
method, which returns anOption
and the value can be extracted in case it isSome(v)
. The alternative is to usewithDefaultValue(v)
in the map which returnsv
in case the key is not present in the map. -
The most difficult exercise was
combinations
. It took me close to a day in order to think about how to get the result they asked in the exercise. The limiting thought was to think that recursion takes a little bit of “wishful thinking” which was absent for the major portion of time when I was focusing on this problem.Initially I started with implementing some sort of power-set for the entire thing. I remembered that there is a recursive way of finding the powerset, so a little bit of time was spent on that. Turns out, it was not the best approach of moving forward.
The next day, I started with the way as per the instructions written in the course page: using for-comprehensions. This was in the afternoon and I had wasted a lot of my time with pen and paper, so I started with writing
createIters
method which would return the elements from('a', n)
down to('a', 0)
. I then needed to do this each element of the list and create combinations out of it. This was time for a quick break.As I walked around, I knew that recursion was the best way of moving forward. This was my way of wishful thinking. I imagined that if I already have everything sorted for the tail of the list, I can
createIters(head)
, andmap
thecons
method on the combinations of tail elements. The case ofNil
list was straightforward.This took a bit of time, a bit of fighting with the type-checker and then I was able to create a result. Phew!
-
I find my code to be decent, but there are still some places where I could add a bit more functional stuff. Although the code does not use any mutable data-type, which is itself a functional thing, I still need to understand a few
Scala
specific code patterns.
Author Tushar Tyagi
LastMod Aug 31, 2016