在Clojure Core或Contrib中有Zip函数的等价物吗? [英] Is there an equivalent for the Zip function in Clojure Core or Contrib?
问题描述
在Clojure中,我想要组合两个列表以提供对的列表,
In Clojure, I want to combine two lists to give a list of pairs,
> (zip '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
在Haskell或Ruby中,函数称为 zip 。实现并不困难,但我想确保我没有在Core或Contrib中缺少一个函数。
In Haskell or Ruby the function is called zip. Implementing it is not difficult, but I wanted to make sure I wasn't missing a function in Core or Contrib.
有一个 zip 命名空间,但它是描述了作为提供对拉链功能技术的访问,这似乎不是我的后续。
There is a zip namespace in Core, but it is described as providing access to the Zipper functional technique, which does not appear to be what I am after.
有一个等效的功能,用于组合2个或更多的列表,以这种方式,在Core?
Is there an equivalent function for combining 2 or more lists, in this way, in Core?
如果没有,是否因为有一个惯用的方法,使函数不需要?
If there is not, is it because there is an idiomatic approach that renders the function unneeded?
推荐答案
(map vector '(1 2 3) '(4 5 6))
会执行您想要的操作:
=> ([1 4] [2 5] [3 6])
Haskell需要< c $ c> zipWith ( zipWith3
, zipWith4
,...)他们都需要具有特定的类型;特别是,他们接受的输入列表的数量需要固定。 ( zip
, zip2
, zip3
,...家庭可以被视为 zipWith
家族中用于tupling的常见用例的专业化)。
Haskell needs a collection of zipWith
(zipWith3
, zipWith4
, ...) functions, because they all need to be of a specific type; in particular, the number of input lists they accept needs to be fixed. (The zip
, zip2
, zip3
, ... family can be regarded as a specialisation of the zipWith
family for the common use case of tupling).
,Clojure等lsps对变量arity函数有很好的支持; map
是其中之一,可以类似于Haskell的方式用于tupling
In contrast, Clojure and other Lisps have good support for variable arity functions; map
is one of them and can be used for "tupling" in a manner similar to Haskell's
zipWith (\x y -> (x, y))
在Clojure中构建一个元组的方法是构造一个简短的向量,如上所示。
The idiomatic way to build a "tuple" in Clojure is to construct a short vector, as displayed above.
(为了完整性,注意Haskell有一些基本的扩展变量arity函数;使用它们需要很好地理解语言,但是Haskell 98可能根本不支持它们,因此固定的函数对于标准库是优选的。)
(Just for completeness, note that Haskell with some basic extensions does allow variable arity functions; using them requires a good understanding of the language, though, and the vanilla Haskell 98 probably doesn't support them at all, thus fixed arity functions are preferrable for the standard library.)
这篇关于在Clojure Core或Contrib中有Zip函数的等价物吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!