Thread overview
Tuple List
Sep 04
drug007
Sep 04
Sergey
September 04

Why doesn't t2 give the same result as t1?

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"])
);
September 04
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.
September 04

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:

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,
September 04

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:

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:

l2.byPair.filter!"a.value > 0".map!(a => a.key).writeln;
September 05

On Wednesday, 4 September 2024 at 08:58:34 UTC, Sergey wrote:

>

Something like:

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:

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