Casting MapResult

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 15 08:18:09 PDT 2015


On Mon, 15 Jun 2015 15:10:20 +0000, jmh530 wrote:

you shouldn't cast it like that. use `std.array.array` to get the actual 
array. like this:

  import std.array;

  auto y = x.map!(a => exp(a)).array;

the thing is that `map` returns so-called "lazy range". lazy ranges 
trying to not do any work until they are explicitely asked. i.e.

  y = x.map!(a => exp(a))

doesn't do any real processing yet, it only prepares everything for it. 
and only when you're calling `y.front`, `map` is processing one element. 
only one, as it has no need to process next until you call `popFront`.

tl;dr: you can't simply cast that lazy range back to array, you have to 
use `std.std.array` to get the array from it.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150615/3439ab3c/attachment.sig>


More information about the Digitalmars-d-learn mailing list