www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tuple List

reply Salih Dincer <salihdb hotmail.com> writes:
Why doesn't t2 give the same result as t1?

```d
import std.algorithm;
import std.typecons;

alias T = Tuple!(string, "key", int, "value");

auto t1 = [T("WC", 0), T("Atelye", 0),
       T("Mutfak", 41),
       T("Salon", 42)
];
assert(t1.find!"a.value > 0"
          .map!"a.key"
          .equal(["Mutfak", "Salon"])
);

auto t2 = [T("WC", 0),
       T("Mutfak", 41),
       T("Salon", 42),
       T("Atelye", 0)
];
assert(t2.find!"a.value > 0"
          .map!"a.key"
          .equal(["Mutfak", "Salon", "Atelye"])
);
```
Sep 04
parent reply drug007 <drug2004 bk.ru> writes:
On 04.09.2024 10:51, Salih Dincer wrote:
 import std.algorithm; import std.typecons; alias T = Tuple!(string, 
 "key", int, "value"); auto t1 = [T("WC", 0), T("Atelye", 0),     
 T("Mutfak", 41),     T("Salon", 42) ]; assert(t1.find!"a.value > 0"     
    .map!"a.key" .equal(["Mutfak", "Salon"]) ); auto t2 = [T("WC", 0),   
    T("Mutfak", 41),      T("Salon", 42),     T("Atelye", 0) ]; 
 assert(t2.find!"a.value > 0"       .map!"a.key"         
 .equal(["Mutfak", "Salon", "Atelye"]) );
You should use filter instead of find. Find finds the first element and returns the range from that first element to the end of the original range.
Sep 04
parent reply Salih Dincer <salihdb hotmail.com> writes:
On Wednesday, 4 September 2024 at 08:04:58 UTC, drug007 wrote:
 
 You should use filter instead of find. Find finds the first 
 element and returns the range from that first element to the 
 end of the original range.
Thank you, it's my mistake. We confused HOF, which has the same first letter. So, if we turn the question towards the associative array, can a similar one be done without using each or foreach? For example: ```d auto l2 = ["WC": 0, "Mutfak": 41, "Salon": 42, "Atelye": 0 ]; l2.values.filter!"a > 0".writeln; // [41, 42] foreach(key, value; l2) if(value > 0) key.write(", "); // Mutfak, Salon, ```
Sep 04
parent reply Sergey <kornburn yandex.ru> writes:
On Wednesday, 4 September 2024 at 08:45:04 UTC, Salih Dincer 
wrote:
 On Wednesday, 4 September 2024 at 08:04:58 UTC, drug007 wrote:
 Thank you, it's my mistake. We confused HOF, which has the same 
 first letter. So, if we turn the question towards the 
 associative array, can a similar one be done without using each 
 or foreach? For example:

 ```d
 auto l2 = ["WC": 0, "Mutfak": 41,
     "Salon": 42, "Atelye": 0
   ];

   l2.values.filter!"a > 0".writeln;
   // [41, 42]
   foreach(key, value; l2)
     if(value > 0) key.write(", ");

   // Mutfak, Salon,
 ```
Something like: ```d l2.byPair.filter!"a.value > 0".map!(a => a.key).writeln; ```
Sep 04
parent Salih Dincer <salihdb hotmail.com> writes:
On Wednesday, 4 September 2024 at 08:58:34 UTC, Sergey wrote:
 Something like:
 ```d
 l2.byPair.filter!"a.value > 0".map!(a => a.key).writeln;
 ```
Thank you for your answers, my forum friends. For tuples that are not fully integrated into the language yet, byPair() is a nice possibility. Because you don't need to import std.typecons. Moreover, instead of a complicated syntax, associative array makes life easier. Here is an example: ```d import std.range, std.algorithm, std.stdio; void main() { //import std.array : byPair;/* import std.typecons : Tuple; alias T = Tuple!(string, "key", int, "value"); auto list = [T("Mutfak", 41), T("WC", 0), T("Salon", 42), T("Atelye", 0) ]; list.map!"a.key".writeln; // ["Mutfak", "WC", "Salon", "Atelye"] ✓ /*/ auto list = ["Mutfak": 41, "WC": 0, "Salon": 42, "Atelye": 0 ]; list.byPair.map!"a.key".writeln; // ["WC", "Mutfak", "Atelye", "Salon"] ❓ // but the order is uncertain! //*/ } ``` SDB 79
Sep 05