Clojure Koans: Trouble Understanding the Section on Sequence Comprehensions
我是Clojure的新手,所以最近几天我一直在研究Clojure Koans。 直到有关序列理解的部分为止,一切进展顺利。 我在本节中苦苦挣扎。 答案是可用的,但我不明白它们是如何得出这些答案的。 最近两天,我已经阅读了很多有关Clojure的文章,但是它与Ruby的区别非常大,需要花一些时间来理解它。
本节中有五个"问题",我无法弄清楚。 这里有两个使我特别困惑的问题的例子:
1 2 3 4 5 6 7 8 9 10 11 12 | "And also filtering" (= '(1 3 5 7 9) (filter odd? (range 10)) (for [index __ :when (odd? index)] index)) "And they trivially allow combinations of the two transformations" (= '(1 9 25 49 81) (map (fn [index] (* index index)) (filter odd? (range 10))) (for [index (range 10) :when __] __)) |
对于具有Clojure经验的人,您能否解释一下他们如何得出此部分的解决方案? 无论我对序列有多少了解,我都无法在本节中全神贯注。 谢谢!
我假设您了解
1 2 3 | (map <some function to map a value> (filter <some function to return true OR false to filter values> )) |
上面的代码使用
1 2 3 | (for [index :when <some expression to return true OR false by check index value>] (<some expression to map a value i.e transform index to something else>)) |
我希望以上示例将使您能够映射如何使用
这种解释很有帮助,并且让我沉迷于Clojure几天后,我对这种语言感到更加自在。为了确保我理解,我将逐步完成这两个测试。
1 2 3 4 5 | "And also filtering" (= '(1 3 5 7 9) (filter odd? (range 10)) (for [index (range 10) :when (odd? index)] index)) |
因此
那是对的吗?
1 2 3 4 5 6 | "And they trivially allow combinations of the two transformations" (= '(1 9 25 49 81) (map (fn [index] (* index index)) (filter odd? (range 10))) (for [index (range 10) :when (odd? index)] (* index index))) |
在此,
我的解释正确吗?
您了解
这些问题的目的是让您推断