Given a Dataset
:
myDs = {<|"id" -> 5, "name" -> "a", "quantity" -> 8.47|>,
<|"id" -> 1, "name" -> "b", "quantity" -> 1.22`|>,
<|"id" -> 2, "name" -> "c", "quantity" -> 9.61|>,
<|"id" -> 4, "name" -> "d", "quantity" -> 6.56|>,
<|"id" -> 3, "name" -> "e", "quantity" -> 5.17`|>} // Dataset
I'm using Select
as a query operator in this way:
myDs[Select[#["id"] == 1&], "name"][[1]] // Normal
(* output: "b" *)
I now want to pass a list of values (the ids) to Select
so that they are tested, but I can't simply map it because of the ampersand right next to where my Map
slot would go.
My current solution is having an ad hoc function:
querySelect[dataset_Dataset, testKey_String, testValue_, wantedKey_String]:=
dataset[Select[#[testKey] == testValue &], wantedKey][[1]] // Normal
querySelect[dataset_Dataset, testKey_String, testValues_List, wantedKey_String]:=
querySelect[dataset, testKey, #, wantedKey] & /@ testValues
Which does the work:
ids = myDs[All, "id"] // Normal;
querySelect[myDs, "id", ids, "name"]
(* out: {"a", "b", "c", "d", "e"} *)
I wonder, however, if there are ways to pass the values and bypass the clash of slots without resorting to user-defined functions.