August 31, 2014 Clojure transducers | ||||
---|---|---|---|---|
| ||||
Clojure is introducing a new way of composing functions (reducers this time), called transducers. It looks similar to composition/binding of functions, but somehow different. My functional-fu is not that deep to understand the statement "transducers are as fundamental as function composition" but it seems quite interesting. What is D's attitude toward this concept? Here is a blog I read about transducers today: http://thecomputersarewinning.com/post/Transducers-Are-Fundamental/ |
August 31, 2014 Re: Clojure transducers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Puming | > What is D's attitude toward this concept?
Hickey's original announcement is also interesting to read. Elegant, as always.
It seems to me that ranges and range algorithms in D already permit this composition of actions, without the creation of intermediate structures:
import std.algorithm;
import std.functional: pipe;
alias incsum = pipe!(
map!(x => x+1),
reduce!((x,y) => x+y)
);
alias filtermap = pipe!(
filter!(x => x%2==0),
map!(x => x+1)
);
alias mapfilter = pipe!(
map!(x => x+1),
filter!(x => x%2==0)
);
void main() {
auto sequence = iota(10);
writeln(incsum(sequence)); // 55
writeln(filtermap(sequence)); // [1, 3, 5, 7, 9]
writeln(mapfilter(sequence)); // [2, 4, 6, 8, 10]
}
|
Copyright © 1999-2021 by the D Language Foundation