- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Clojure 新手,所以我一直在浏览 Clojure Koans最后几天。事情进展得相当顺利,直到 section on sequence comprehensions 。我在这部分遇到困难。 answers可用,但我不明白他们如何得出这些答案。过去两天我读了很多有关 Clojure 的内容,但它与 Ruby 有很大不同,我花了一段时间才理解它。
该部分有五个“问题”,但我无法解决。以下是两个让我特别困惑的问题示例:
"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 经验的人,您能否解释一下他们是如何得出本节的解决方案的?无论我读了多少有关序列的内容,我都无法理解这一部分。谢谢!
最佳答案
我假设您理解map
和filter
函数,我想它们也存在于 Ruby 中。让我给你举个例子,也许可以帮助你理解for
在这种情况下使用。
(map <some function to map a value>
(filter <some function to return true OR false to filter values>
<a sequence of values>))
上面的代码使用 filter
对一系列值进行一些过滤然后使用 map
将过滤后的序列的每个值映射到其他值功能。
for
基本上允许你做同样的事情,如下所示
(for [index <a sequence of values>
: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>))
我希望上面的例子能够让您能够映射map
和filter
代码可以用 for
来表示
关于Clojure 公案 : Trouble Understanding the Section on Sequence Comprehensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916782/
我最近尝试使用这个工具来提高我的 Rails 技能: http://github.com/edgecase/ruby_koans 但我无法通过一些测试。此外,我不确定我是否正确地做了一些事情,因为目标
我正在完成 Kotlin Koans 的 Comparison练习并想知道为什么 compareTo() 是被重写的函数,但 compare() 是正在使用的函数。 这两个函数有何关联? data c
我正在解决 the python koans .直到 34 号我才遇到任何真正的问题。 问题是: Project: Create a Proxy Class In this assignment, c
在 about_symbols.rb Ruby Koan (https://github.com/edgecase/ruby_koans) 中,我有以下代码: RubyConstant = "
我是 Clojure 新手,所以我一直在浏览 Clojure Koans最后几天。事情进展得相当顺利,直到 section on sequence comprehensions 。我在这部分遇到困难。
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!