2016-04-21 15:57:27 +02:00
|
|
|
Title: WTFunctional: Modifying yout WTF-Count using functional programming
|
|
|
|
Author: Oliver Rümpelein
|
|
|
|
html header: <link rel="stylesheet"
|
|
|
|
href="http://yandex.st/highlightjs/7.3/styles/default.min.css">
|
|
|
|
<script src="http://yandex.st/highlightjs/7.3/highlight.min.js"></script>
|
|
|
|
<script>hljs.initHighlightingOnLoad();</script>
|
|
|
|
|
|
|
|
|
|
|
|
# [%title] #
|
2016-04-19 17:13:44 +02:00
|
|
|
|
|
|
|
## Content ##
|
|
|
|
|
|
|
|
* Dafunc? Understanding functional programming with Haskell.
|
|
|
|
+ no side-effects, no mutability => maintainability
|
|
|
|
+ recursion
|
|
|
|
+ functions as first class “objects”: i.e. C++, 5; is a valid statement
|
|
|
|
+ lambdas
|
|
|
|
+ lists, maps, filters, folds
|
|
|
|
+ currying => just convenience
|
2016-04-21 15:57:27 +02:00
|
|
|
+ code example: Pythagoraian triangles, bubble sort
|
2016-04-19 17:13:44 +02:00
|
|
|
* Phuncy: The pythonic way is functional!
|
2016-04-25 16:20:31 +02:00
|
|
|
+ Not strictly functional
|
|
|
|
- recursion
|
|
|
|
+ fafco
|
2016-04-21 15:57:27 +02:00
|
|
|
- map, fold => example (sum of squares?)
|
2016-04-25 16:20:31 +02:00
|
|
|
+ python lambda syntax: lambda a,b: a+b
|
|
|
|
+ don't return lists, but iterators!
|
|
|
|
+ fold with reduce
|
|
|
|
+ Note: 2: map, filter, reduce, list comprehension
|
2016-04-21 15:57:27 +02:00
|
|
|
3: map, filter, functools.reduce(), list comprehension
|
|
|
|
|
|
|
|
|
2016-04-25 16:20:31 +02:00
|
|
|
+ The python2 to 3 page states:
|
2016-04-21 15:57:27 +02:00
|
|
|
“Removed `reduce()`. Use `functools.reduce()` if you really need it;
|
|
|
|
however, 99 percent of the time an explicit `for` loop is more
|
|
|
|
readable.”
|
2016-04-25 16:20:31 +02:00
|
|
|
+ Why use it? => Multi-processing, WTF Count.
|
2016-04-21 15:57:27 +02:00
|
|
|
|
|
|
|
```python
|
2016-04-19 17:13:44 +02:00
|
|
|
a = list(range(10))
|
|
|
|
|
|
|
|
b = 0
|
|
|
|
for i in a:
|
|
|
|
b += i**2
|
|
|
|
print(b)
|
|
|
|
```
|
|
|
|
vs.
|
2016-04-21 15:57:27 +02:00
|
|
|
|
|
|
|
```python
|
2016-04-19 17:13:44 +02:00
|
|
|
from functools import reduce
|
|
|
|
print(reduce(lambda x,y: x+y,map(lambda x: x**2,range(10))))
|
|
|
|
```
|
2016-04-25 16:20:31 +02:00
|
|
|
+ currying: not really, but binding via lambdas or functools.partial() or
|
2016-04-19 17:13:44 +02:00
|
|
|
https://mtomassoli.wordpress.com/2012/03/18/currying-in-python/
|
2016-05-24 11:14:43 +02:00
|
|
|
+ decorators!
|
|
|
|
+ still FP has advantages and is heavily used, i.e. in genomics (works on
|
2016-04-19 17:13:44 +02:00
|
|
|
tons of lengthy lists)
|
2016-05-24 11:14:43 +02:00
|
|
|
* FunC++tional: On the fast lane
|
|
|
|
+ "Classical" C++ has some functional stuff, bust i.e. no lambdas (hardly usable)
|
|
|
|
+ Changed with the new C++11-standard
|
|
|
|
+ Buzzwords:
|
|
|
|
+ `map` defines a Datatype in C++!
|
|
|
|
+ lambdas in C++
|
2016-04-19 17:13:44 +02:00
|
|
|
```[](int x, int y) { return a<b;} ;```
|
|
|
|
[] can be used to capture variables, i.e. by reference or value
|
2016-05-24 11:14:43 +02:00
|
|
|
+ `std::for_each` from `algorithm`: Apply `void fun(T &a)` to iterator
|
2016-04-19 17:13:44 +02:00
|
|
|
containing `T` values
|
2016-05-24 11:14:43 +02:00
|
|
|
+ `std::transform` from `algorithm`: same as for_each, but stores return
|
2016-04-19 17:13:44 +02:00
|
|
|
value in another range
|
2016-05-24 11:14:43 +02:00
|
|
|
+ `std::accumulate` from `numeric`: Wants binary operation, i.e. `std::minus<int>`
|