[英]Clojure - map function
As we know, (map f [abc])
is equivalent to [(fa) (fb) (fc)]
. 众所周知, (map f [abc])
等效于[(fa) (fb) (fc)]
。
My question is: The evaluation result of (map #(- (int %) (int \\0)) "1234")
is (1 2 3 4)
, why does it return the results of applying #(- (int %) (int \\0))
to every digits of "1234", rather than the string "1234" as a whole? 我的问题是: (map #(- (int %) (int \\0)) "1234")
的评估结果是(1 2 3 4)
,为什么它返回应用#(- (int %) (int \\0))
到“ 1234”的每个数字,而不是整个字符串“ 1234”? How should I understand this code example? 我应该如何理解此代码示例?
map
calls seq
on all arguments after the first. map
在第一个参数之后对所有参数调用seq
。 seq
turns a string into a sequence of characters. seq
将字符串转换为字符序列。
Clojure can treat a string as a sequence - of characters. Clojure可以将字符串视为字符序列。 This is useful because you can: 这很有用,因为您可以:
It's perhaps a bit annoying having to remember to put the resulting sequence back into a string by wrapping the sequence manipulating expression in a call to str
. 不得不记住通过将序列操纵表达式包装在对str
的调用中来将结果序列放回字符串中可能会有些烦人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.