clojure地图功能的怪异 [英] weirdness in clojure map function

查看:23
本文介绍了clojure地图功能的怪异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clojure 中 map 的第一个奇怪之处在于以下代码段:

the first strange thing about map in clojure is in following snippet:

(apply map list '((1 a) (2 b) (3 c)))

结果令我惊讶:

((1 2 3) (a b c))

谁能解释一下它是如何工作的?

Anyone could explain how it works?

推荐答案

(apply fx '(yz)) 等价于 (fxyz),所以你的代码是相当于 (map list '(1 a) '(2 b) '(3 c)).

(apply f x '(y z)) is equivalent to (f x y z), so your code is equivalent to (map list '(1 a) '(2 b) '(3 c)).

当使用多个列表调用时,map 并行迭代列表并使用每个列表中的一个元素为每个元素调用给定的函数(即结果列表的第一个元素是调用的结果)以每个列表的第一个元素作为参数的函数,第二个是第二个元素的结果等).

When called with multiple lists, map iterates the lists in parallel and calls the given function with one element from each list for each element (i.e. the first element of the result list is the result of calling the function with the first element of each list as its arguments, the second is the result for the second elements etc.).

所以 (map list '(1 a) '(2 b) '(3 c)) 首先用列表的第一个元素(即数字)作为参数,然后与第二个元素(字母).所以你得到 ((list 1 2 3) (list 'a 'b 'c)).

So (map list '(1 a) '(2 b) '(3 c)) first calls list with the first elements of the lists (i.e. the numbers) as arguments and then with the second elements (the letters). So you get ((list 1 2 3) (list 'a 'b 'c)).

这篇关于clojure地图功能的怪异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆