7
$\begingroup$

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] |>
$\endgroup$
0

1 Answer 1

7
$\begingroup$

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]|>
$\endgroup$
3
  • $\begingroup$ Still, as did OP, I'd expect Through to work (does it?) $\endgroup$
    – LLlAMnYP
    Sep 26, 2017 at 8:16
  • $\begingroup$ @LLlAMnYP you mean like 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$
    – Mr.Wizard
    Sep 26, 2017 at 8:37
  • $\begingroup$ Thanks, I do not mind a non cw answer next time :) $\endgroup$
    – Kuba
    Sep 26, 2017 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.