gpt4 book ai didi

clojure - 如何将值应用于 Clojure 中的多个函数?

转载 作者:行者123 更新时间:2023-12-02 21:29:15 24 4
gpt4 key购买 nike

基本上,我需要做类似 map 的事情,但我不需要将函数应用于集合中的所有元素,而是需要将相同(一组)值应用于函数集合(此操作有名称吗?)。这可能看起来是一个简单的问题,但我还没有找到在 Clojure 中执行此操作的惯用方法。例如,对于我只需要对每个函数应用一个值的特殊情况,我使用了

(for [f funs] (f value))

哪里value当然,是我需要每个函数作为参数的值,并且 funs是需要用 value 调用的函数的集合作为论点。

那么我的问题是,Clojure 中是否有一个函数可以执行此操作,但也可以泛化为任意数量的参数?或者上面的内容确实是 Clojure 惯用的吗?

最佳答案

您正在寻找juxt

juxt

Takes a set of functions and returns a fn that is the juxtaposition of those fns. The returned fn takes a variable number of args, and returns a vector containing the result of applying each fn to the args (left-to-right). ((juxt a b c) x) => [(a x) (b x) (c x)]

关于clojure - 如何将值应用于 Clojure 中的多个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17875376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com