On Friday, 8 November 2013 at 06:25:15 UTC, qznc wrote:
On Friday, 8 November 2013 at 05:46:29 UTC, ProgrammingGhost
wrote:
I'm a D noob. ".map!(a => a.length)" seems like the lambda is
passed into the template. ".map!split" just confuses me. What
is split? I thought only types can be after "!". I would guess
split is a standard function but then shouldn't it be
map!(split)?
const wordCount = file.byLine() // Read
lines
.map!split // Split
into words
.map!(a => a.length) // Count
words per line
.reduce!((a, b) => a + b); // Total
word count
Do you know C++ templates? C++ func<thing> == D func!(thing).
You can pass anything into a template, not just types. So you
are right, "map!split" gives the "split" function into the
"map" template and "map!(split)" is the canonical form. D
allows you to remove the parens for simple cases, hence
"map!split".
Oh I see. Yes I understand C++ templates which is how I guessed
that. This FEELS UNUSUAL. Because it seems like it is
.map(!split.map(!(...))).reduce...
As if split.map was the template parameter. How does it know if
split isn't a class (or if d has them, namespace) and map is a
static function? Thats why it confused me.