DEV Community

loading...

Map Reduce in D

Jesse Phillips
Senior Quality Assurance (SDET) ¶ Avid hobby D programmer ¶ Telling people what to do because I am right.
Updated on ・1 min read

I didn't want to confuse function mapping and dictionaries, so I decided to include reduce.

map is a range (list) based function which applies an operation to each element and their result is a range which lazily applies the operation.

I haven't been referencing how other languages apply a solution, but as this started with Python and their list comprehension is an interesting approach to this.

https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

new_range  = [i + i for i in range(5)   if i % 2 == 0]
Enter fullscreen mode Exit fullscreen mode
import std.range;
auto new_range = iota(5)
                 .filter!(i => i % 2 == 0)
                 .map!(i => i + i);
Enter fullscreen mode Exit fullscreen mode

Putting aside the strange name iota the structure is similar. Personally I'd argue D is more clear about the operation order.

import std.range;
auto new_range = iota(5)
                 .map!(i => i + i)
                 .filter!(i => i % 2 == 0);
Enter fullscreen mode Exit fullscreen mode

In the first situation filtering came before the operation, filtering after has no value since two odds make an even. In Python order of operation is a spiral like C function pointers.


reduce is an operation on a range which produces a single result.

import std.algorithm;
import std.range;

assert(
       iota(5).fold!((a, b) => a + b)
       == 1 + 2 + 3 + 4);
Enter fullscreen mode Exit fullscreen mode

I realize my example uses fold rather than reduce. This has many more names, but I chose fold in the example as this provides better argument order in chained expressions.

Discussion (0)