www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - map! evaluates twice

reply Antonio <antonio abrevia.net> writes:
When mapping and filtering, the last mapped element is evaluated 
twice... Is it the expected behaviour?

```d
void main()
{
     import std.algorithm, std.stdio;
	
     [1,2,3,4,5].
		map!((x){
         	writeln("mapping ", x);
         	return x;
     	}).
		filter!(x=>x>2).
		front.
		writeln();
}
```

Output
```
mapping 1
mapping 2
mapping 3
mapping 3
3

```
Jun 10 2022
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 6/10/22 4:33 PM, Antonio wrote:
 When mapping and filtering, the last mapped element is evaluated 
 twice... Is it the expected behaviour?
 
 ```d
 void main()
 {
      import std.algorithm, std.stdio;
 
      [1,2,3,4,5].
          map!((x){
              writeln("mapping ", x);
              return x;
          }).
          filter!(x=>x>2).
          front.
          writeln();
 }
 ```
 
 Output
 ```
 mapping 1
 mapping 2
 mapping 3
 mapping 3
 3
 
 ```
`map` calls the lambda for each call to `front`. If you want a cached version, use `cache`: ```d void main() { import std.algorithm, std.stdio; [1,2,3,4,5]. map!((x){ writeln("mapping ", x); return x; }). cache. filter!(x=>x>2). front. writeln(); } ``` -Steve
Jun 10 2022
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/10/22 13:47, Steven Schveighoffer wrote:

 `map` calls the lambda for each call to `front`. If you want a cached
 version, use `cache`:
Others don't know but as I will likely show during a lightning talk at DConf, I am trying to finish a .cached range algorithm that caches all elements that are in use. (It must drop old elements so that an infinite range does not require infinite cache.) It is supposed to evaluate elements only once. Ali
Jun 10 2022
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Friday, 10 June 2022 at 22:10:10 UTC, Ali Çehreli wrote:
 [...] I am trying to finish a .cached range algorithm that 
 caches all elements that are in use. (It must drop old elements 
 so that an infinite range does not require infinite cache.)

 It is supposed to evaluate elements only once.
I guess the developed cached() and cache() are different things, right? I tried cached(), it works fine and cleverly designed... SDB 79
Jun 16 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/16/22 00:58, Salih Dincer wrote:

 I guess the developed cached() and cache() are different things,
 right?
cache caches only the front element. https://dlang.org/library/std/algorithm/iteration/cache.html
 I tried cached()
cached() is supposed to cache as many elements as needed as long as there are ranges that still reference the elements. Technically, cached() does not exist because only a couple of people have seen a draft of it. :) Ali
Jun 16 2022
prev sibling parent Antonio <antonio abrevia.net> writes:
On Friday, 10 June 2022 at 20:47:14 UTC, Steven Schveighoffer 
wrote:
 On 6/10/22 4:33 PM, Antonio wrote:
 ...

 `map` calls the lambda for each call to `front`. If you want a 
 cached version, use `cache`:
Thank you very much, Steve
Jun 12 2022