在 for 理解中绑定单个值
学习 Haskell 教程有一个 在列表理解中使用 let
绑定器:
calcBmis xs = [bmi | (w, h) <- xs, let bmi = w / h ^ 2, bmi >= 25.0]
该函数获取身高/体重对的列表,并返回超过某个限制的相应身体质量指数的列表,例如:
ghci> calcBmis [(70, 1.85), (50, 2.00), (130, 1.62)]
[49.53513183965858]
这里对我来说有趣的是,理解中绑定的值 bmi
可以在防护和结果中使用表达。我知道如何在 Scala 中执行类似操作的唯一方法是编写:
def calcBmis(xs : Seq[(Double,Double)]) =
for((w,h) <- xs ; bmi <- Some(w / (h*h)) if bmi >= 25.0) yield bmi
必须将我的值包装在 Some
中感觉不对。有人知道更好的方法吗?
The Learn You a Haskell tutorial has an example of using a let
binder in a list comprehension:
calcBmis xs = [bmi | (w, h) <- xs, let bmi = w / h ^ 2, bmi >= 25.0]
The function takes a list of height/weight pairs, and returns a list of the corresponding body-mass indices that exceed some limit, eg:
ghci> calcBmis [(70, 1.85), (50, 2.00), (130, 1.62)]
[49.53513183965858]
What's interesting to me here is that the value bmi
that is bound within the comprehension can be used both in a guard and in the resulting expression. The only way I know how to do something similar in Scala is to write:
def calcBmis(xs : Seq[(Double,Double)]) =
for((w,h) <- xs ; bmi <- Some(w / (h*h)) if bmi >= 25.0) yield bmi
Having to wrap my value in a Some
here feels wrong. Anyone knows of a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,有这样的语法。
单行替代方案(但不是理解):
Yes, there is such syntax.
One-liner alternative (but not comprehension):
我刚刚复制了 om-nom-nom 的答案,以展示如何使用大括号而不使用分号来布局它;这样更清晰一些
,我不使用元组,而是使用
case
表达式(同样,需要花括号)I've just copied om-nom-nom's answer to show how you can lay it out using curly braces without the semicolons; it's a bit clearer this way
and instead of using tuples, I'd use a
case
expression (again, requires curly braces)