I have an association
<| a->f, b->g |> where f and g are pure functions. Is there a nice way to apply arguments to both f and g? E.g.
<| a->f, b->g |>[x] --> <| a->f[x], b->g[x] |> As Kuba provided in a comment we can Map a Function that applies its argument to a specific expression, e.g. x:
#[x] & /@ <|a -> f, b -> g|> <|a -> f[x], b -> g[x]|>
Through[<|a -> f, b -> g|>[x]] ? No, it does not; the body evaluates to Missing["KeyAbsent", x]. Through[Unevaluated[<|a -> f, b -> g|>[x]]] gives Association[(a -> f)[x], (b -> g)[x]] but I don't find that helpful. With[{asc = <|a -> f, b -> g|>}, Through[Unevaluated[asc[x]]]] just kicks back Through[Unevaluated[<|a -> f, b -> g|>[x]]]. $\endgroup$ Using Comap (new in 14.0)
Comap[<|a -> f, b -> g|>, x] <|a -> f[x], b -> g[x]|>
If your version of WL doesn't have Comap (version version 14), you could also use Query:
Query[<|a -> f, b -> g|>][x] <|a -> f[x], b -> g[x]|>
From @Gustavo Delfino's answer and Query[<|a -> f, b -> g|>] // Normal,
GeneralUtilities`ApplyThrough[<|a -> f, b -> g|>][x] (* <|a -> f[x], b -> g[x]|> *) (Community wiki)