I am looking for something better than
Select[Map[f,a],test]
As Map will work over the entire association (a) and then Select will work over all of the results. Something like
Cases[a,x_/;test[f[x]]:>f[x]]
But Cases only returns values (or functions of the values) not the keys. I also tried
Map[If[test[#],#]&,a]
But it keeps the keys that fail the test and maps them to Null.
My sample association is
a = <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|>;
I would like to do something like
Select[Map[(3^#-2)&,a],PrimeQ]
Giving
<|b->7,d->79,e->241|>
My actual case is much more complicated but if I have a way to Map and Select in one pass for this case it will solve my more difficult problem. Thanks!
key -> value
sequence as an argument. Alternatively, in yourIf
attempt, you could explicitly returnMissing[]
when the test fails, then clean up the results by runningDeleteMissing
on the results, which will remove suchkey -> Missing[]
entries from your association. $\endgroup$ – MarcoB Mar 26 '16 at 12:10Reap[Do[If[test@f@Lookup[ass, x], Sow[x -> f@Lookup[ass, x]]], {x, Keys@ass}]][[2, 1]] // Association
might help. $\endgroup$ – The Vee Mar 26 '16 at 12:27AssociationMap
. Occurrences ofMissing[]
will not clog the intermediate result too much either as they would all refer to a single instance of the object. $\endgroup$ – The Vee Mar 26 '16 at 12:29AssociationMap
will work fine for my application. I can map all of the delete cases to a single key-value pair that is easily and quickly removed (or ignored). $\endgroup$ – JJM Mar 26 '16 at 12:42