Assignment on Programming Techniques for Big Data

Functional programming is the basis of most modern Big Data processing systems. Before going forward to the course, it is important to master data processing techniques using a functional programming style. In this assignment, your task is to train yourselves in thinking in a functional way when processing data.

Many of the the tasks below are easy, but some are not and require many iterations (and extensive testing!) to get right. For some of them, you can find ready-made solutions on the internet. Even though it may be tempting, I advise you against copying and pasting them here, as you will regret it later on in the course. Wax on, wax off!

This assignment has a total of 110 points.

For each function you implement, you also need to define at least one working example.

folds and friends

T(5+5pts): Implement reduceL and reduceR using iteration for lists/arrays

In [ ]:
 

T (10pts): Implement reduceL by reusing reduceR

In [ ]:
 

T (10pts): Implement a function flatten(xs: [A]): [A] that converts a list of lists into a list formed by the elements of these lists. For example:

>>> a = [[1,2],[2,3],[3,[4]]]
>>> flatten(a)
[1,2,2,3,3,[4]]
In [ ]:
 

T (10pts): You may have noticed that the original flatten definition is not recursive, as it will only flatten 1 level nested lists. Write a function deep_flatten(xs: [A]): [A] that converts a list of lists into a list formed by the elements of these lists recursively. For example:

>>> a = [[1,2],[2,3],[3,[4]]]
>>> flatten(a)
[1,2,2,3,3,4]
In [ ]:
 

T (10pts): Implement group_by by reusing reduceL.

In [ ]:
 

Simple data processing

From this point forward, use the reduceL, reduceR or flatten implementations you created above.

T (5pts): Implement distinct using reduceL. Write its function signature.

In [ ]:
 

T (10pts): Implement a function reverse that reverses a list/array. As an example:

>>> a = [1,2,3,4]
>>> reverse(a)
[4,3,2,1]

Also, write down the function signature.

In [ ]:
 

T (5pts): Implement a function mean that calculates the mean of an list of integers.

Also, write down the function signature.

In [ ]:
 

Higher-order functions

T (10pts): Implement a function called drop_while(xs: [A], f: A -> Boolean) : [A] that drops the longest prefix of elements from xs that satisfy f.

>>> a = [1,2,3,4,5,6]
>>> dropWhile(a, lambda x: x <= 3)
[4,5,6]
In [ ]:
 

T (10pts): Implement a function zip(xs: [A], ys: [B]): List[(A,B)] that returns a list formed from this list and another list by combining the corresponding elements in pairs. If one of the two lists is longer than the other, its remaining elements are ignored.

>>> a = [2,3,4]
>>> b = [a,b,c,d]
>>> zip(a,b)
[[2,a],[3,b],[4,c]]
In [ ]:
 

T (10pts): Implement a function scanL(xs: [A], init: B, f: (acc: B, x: A) -> B) -> [B] that works like foldL but instead of producing one final result, it also returns all the intermediate results.

>>> a = [2,3,4]
>>> scanL(a, 0, lambda x, y: x + y)
[0, 2, 5, 9]
In [ ]:
 

Credits

Many of the textual descriptions of the functions where addapted from the Scala language documentation, see here.