Functional Programming in Scala: Weeks 5 and 6
How to create the methods like
append, etc. on Lists.
Implementation of mergesort.
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.
One way is to parameterize the type, which would not work unless we pass
a ordering function as well since not every type has
And then we can go ahead and use
Ordering trait so that the class
using msort has to have
lt method defined for it.
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.
- 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
(_ * _)
Provides a better access to the elements
In number of elements < 32,
I started with the implementation of
because I totally forgot that the
for expression allows for multiple
which provides a clearer way of doing things.
In the lecture this was one of the reasons of providing
syntax but I could not think about it.
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 use
xs.foldLeft("")(_ ++ _)
Why did they use a
List[Char]instead of a
Stringas 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 using
mapname(key). But this explodes if there is no key. One way of safely handling this is to use the
getmethod, which returns an
Optionand the value can be extracted in case it is
Some(v). The alternative is to use
withDefaultValue(v)in the map which returns
vin 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
createItersmethod 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
consmethod on the combinations of tail elements. The case of
Nillist 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
Scalaspecific code patterns.
Author Tushar Tyagi
LastMod Aug 31, 2016