- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关注 Nick Partidge's presentation关于推导 scalaz ,我必须看看这个例子,这真是太棒了:
import scalaz._
import Scalaz._
def even(x: Int) : Validation[NonEmptyList[String], Int]
= if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail
println( even(3) <|*|> even(5) ) //prints: Failure(NonEmptyList(not even: 3, not even: 5))
<|*|>
方法是这样做的,这里是源代码:
def <|*|>[B](b: M[B])(implicit t: Functor[M], a: Apply[M]): M[(A, B)]
= <**>(b, (_: A, _: B))
<**>
方法,其声明如下:
def <**>[B, C](b: M[B], z: (A, B) => C)(implicit t: Functor[M], a: Apply[M]): M[C]
= a(t.fmap(value, z.curried), b)
M[B]
)的高级类型,但可以通过 Validation
(它有两种类型的参数)? (_: A, _: B)
定义函数 (A, B) => Pair[A,B]
第二种方法期望的是:失败情况下 Tuple2/Pair 发生了什么?看不到元组! 最佳答案
类型构造函数作为类型参数M
是 Scalaz 的主要皮条客之一的类型参数,MA , 表示拉皮条值的类型构造函数(又名高级类型)。此类型构造函数用于查找 Functor
的适当实例。和 Apply
,这是方法的隐含要求 <**>
.
trait MA[M[_], A] {
val value: M[A]
def <**>[B, C](b: M[B], z: (A, B) => C)(implicit t: Functor[M], a: Apply[M]): M[C] = ...
}
We distinguish between first-order types and type constructors, which take type parameters and yield types. A subset of first-order types called value types represents sets of (first-class) values. Value types are either concrete or abstract. Every concrete value type can be represented as a class type, i.e. a type designator (§3.2.3) that refers to a class1 (§5.3), or as a compound type (§3.2.7) representing an intersection of types, possibly with a refinement (§3.2.7) that further constrains the types of itsmembers. Abstract value types are introduced by type parameters (§4.4) and abstract type bindings (§4.3). Parentheses in types are used for grouping. We assume that objects and packages also implicitly define a class (of the same name as the object or package, but inaccessible to user programs).
Non-value types capture properties of identifiers that are not values (§3.3). For example, a type constructor (§3.3.3) does not directly specify the type of values. However, when a type constructor is applied to the correct type arguments, it yields a first-order type, which may be a value type. Non-value types are expressed indirectly in Scala. E.g., a method type is described by writing down a method signature, which in itself is not a real type, although it gives rise to a corresponding function type (§3.3.1). Type constructors are another example, as one can write type Swap[m[_, _], a,b] = m[b, a], but there is no syntax to write the corresponding anonymous type function directly.
List
是一个类型构造函数。您可以申请类型
Int
获取值类型,
List[Int]
,它可以对一个值进行分类。其他类型构造函数接受多个参数。
scalaz.MA
要求它的第一个类型参数必须是一个类型构造函数,它接受一个类型来返回一个值类型,语法为
trait MA[M[_], A] {}
.类型参数定义描述了类型构造函数的形状,称为它的 Kind。
List
据说有那种'
* -> *
.
MA
包装
Validation[X, Y]
类型的值?型号
Validation
有种
(* *) -> *
, 并且只能作为类型参数传递给声明为
M[_, _]
的类型参数.
Validation[X, Y]
类型的值到
MA
:
object Scalaz {
implicit def ValidationMA[A, E](v: Validation[E, A]): MA[PartialApply1Of2[Validation, E]#Apply, A] = ma[PartialApply1Of2[Validation, E]#Apply, A](v)
}
Validation
,修复错误的类型,但不应用成功的类型。
PartialApply1Of2[Validation, E]#Apply
最好写成
[X] => Validation[E, X]
.我最近提议在 Scala 中添加这样的语法,它可能会在 2.9 中发生。
def validation[A, B](a: A, b: B) = ...
def partialApply1Of2[A, B C](f: (A, B) => C, a: A): (B => C) = (b: B) => f(a, b)
Validation[String, Int]
与
Validation[String, Boolean]
,因为两者共享类型构造函数
[A] Validation[String, A]
.
<**>
需要类型构造函数
M
必须有
Apply 的关联实例和
Functor .这构成了一个 Applicative Functor,它和 Monad 一样,是一种通过某种效果来构建计算的方法。在这种情况下,结果是子计算可能会失败(并且当它们失败时,我们会累积失败)。
Validation[NonEmptyList[String], A]
可以包装类型为
A
的纯值在这个“效果”中。
<**>
运算符采用两个有效值和一个纯函数,并将它们与该容器的 Applicative Functor 实例组合在一起。
Option
的工作原理应用仿函数。这里的“效果”是失败的可能性。
val os: Option[String] = Some("a")
val oi: Option[Int] = Some(2)
val result1 = (os <**> oi) { (s: String, i: Int) => s * i }
assert(result1 == Some("aa"))
val result2 = (os <**> (None: Option[Int])) { (s: String, i: Int) => s * i }
assert(result2 == None)
(String, Int) => String
的纯函数。 ,适用于有效的论据。请注意,结果与参数包装在相同的效果(或容器,如果您愿意)中。
ZipStream
.
Option
和
[A]Validation[X, A]
都是 Monad,所以你也可以使用
Bind
(又名 flatMap):
val result3 = oi flatMap { i => os map { s => s * i } }
val result4 = for {i <- oi; s <- os} yield s * i
<|**|>
真的很像
<**>
,但它为您提供了纯函数,可以根据结果简单地构建一个 Tuple2。
(_: A, _ B)
是
(a: A, b: B) => Tuple2(a, b)
的简写
(fa ⊛ fb ⊛ fc ⊛ fd) {(a, b, c, d) => .... }
what is happening to the Tuple2/Pair in the failure case?
<**>
的两个参数)都成功时才会运行它。如果是这样,它将这些组合成一个
Success
.这个逻辑在哪里?这定义了
Apply
[A] Validation[X, A]
的实例.我们要求类型 X 必须有
Semigroup
可用,这是组合单个错误的策略,每个错误类型为
X
, 转化为相同类型的聚合错误。如果您选择
String
作为您的错误类型,
Semigroup[String]
连接字符串;如果您选择
NonEmptyList[String]
,来自每个步骤的错误被连接成一个更长的
NonEmptyList
的错误。当两个
Failures
发生在下面时,这种连接发生合并,使用
⊹
运算符(它用隐式扩展为,例如,
Scalaz.IdentityTo(e1).⊹(e2)(Semigroup.NonEmptyListSemigroup(Semigroup.StringSemigroup))
。
implicit def ValidationApply[X: Semigroup]: Apply[PartialApply1Of2[Validation, X]#Apply] = new Apply[PartialApply1Of2[Validation, X]#Apply] {
def apply[A, B](f: Validation[X, A => B], a: Validation[X, A]) = (f, a) match {
case (Success(f), Success(a)) => success(f(a))
case (Success(_), Failure(e)) => failure(e)
case (Failure(e), Success(_)) => failure(e)
case (Failure(e1), Failure(e2)) => failure(e1 ⊹ e2)
}
}
Option
的子计算或
[A] Validation[E, A]
可以与
Apply
结合使用或与
Bind
.你什么时候会选择一个?
Apply
,计算的结构是固定的。将执行所有子计算;一个人的结果不能影响其他人。只有“纯”函数对发生的事情有一个概述。另一方面,一元计算允许第一个子计算影响后面的子计算。
Success
值以提供给后续验证。但是,我们很高兴子验证是独立的,因此我们可以通过 Applicative 将它们组合起来,并收集我们遇到的所有失败。 Applicative Functors 的弱点变成了强项!
关于scala - scalaz 中的函数语法谜题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2545248/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!