scalaz 中更像 haskell 的应用语法 [英] More haskell-like applicative syntax in scalaz

查看:39
本文介绍了scalaz 中更像 haskell 的应用语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验 scalaz.我尝试在应用代码中编写代码.我写的代码是这样的:

I am experimenting with scalaz. I tried writing code in applicative code. I wrote code like this:

val max: Option[Int] = (a |@| b) { math.max(_, _) }

我不太喜欢这段代码.我想编写更接近 Haskell 风格的代码,如下所示:

I didn't like this code very much. I would like to code which is closer to Haskell style, something like this:

val max: Option[Int] = { math.max(_, _) } <$> a <*> b

这可能吗.为什么 scalaz 没有以这种方式实现它?

Is this possible. And why scalaz didn't implement it this way?

推荐答案

Scala 的类型推断,比 Haskell 中的限制要多得多(标识符重载,与 JVM 一起出现是原因之一).推论是从左到右的,函数参数的类型可以从前面的上下文中推导出来(如果在定义的地方,需要一个 arg 类型为 A 的函数),而不是从它们在函数中的使用方式推导出来定义.Scalaz 语法使参数类型可用.反转它在大多数情况下会迫使您编写函数参数类型,例如

Scala's type inference, is much more limited than in Haskell (identifier overloading, which comes with the JVM being one of the reasons). Inferences flows from left to right, and the type of the arguments of a function may be deduced from previous context (if at the place of the definition, a function with arg type A is expected), but not from how they are used in the definition. Scalaz syntax makes the arguments types available. Reversing it would most of the time force you to write the function arguments types, e.g.

{math.max(_: Int, _: Int) } <$> a <*> b

这篇关于scalaz 中更像 haskell 的应用语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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