- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 this answer我当场编造了一些看起来有点像“高阶Traversable
”的东西:比如Traversable
但是对于从 Hask 到 Hask 的 endofunctors 类别的仿函数。
{-# LANGUAGE RankNTypes #-}
import Data.Functor.Compose
import Data.Functor.Identity
class HFunctor t where
hmap :: (forall x. f x -> g x) -> t f -> t g
class HFunctor t => HTraversable t where
htraverse :: Applicative g => (forall x. f x -> g x) -> t f -> g (t Identity)
htraverse eta = hsequence . hmap eta
hsequence :: Applicative f => t f -> f (t Identity)
hsequence = htraverse id
HFunctor
HTraversable
的父类(super class)因为它看起来是对的,但是当我坐下来写
hmapDefault
我被困。
hmapDefault :: HTraversable t => (forall x. f x -> g x) -> t f -> t g
hmapDefault eta = runIdentity . htraverse (Identity . eta)
-- • Couldn't match type ‘x’ with ‘g x’
-- Expected type: f x -> Identity x
-- Actual type: f x -> Identity (g x)
Identity . eta
有一个类型
forall y. f y -> Identity (g y)
,所以当我将它传递给
htraverse
g
与
Identity
合并和
x
必须与两者统一
y
和
g y
,所以它失败了,因为遍历函数不是自然变换。
Compose
对其进行修补:
hmapDefault :: HTraversable t => (forall x. f x -> g x) -> t f -> t g
hmapDefault eta = runIdentity . getCompose . htraverse (Compose . Identity . eta)
Compose . Identity . eta
是一种自然的转变,但你不能
htraverse
因为你不知道
Applicative g
.即使你能做到,
runIdentity
通话返回
g (t Identity)
你没有办法把
g
回到
t
里面.
htraverse
并不真正类似于普通的旧
traverse
.
traverse
的遍历函数将新值放入
Applicative
效果,使类型表达式更大。所以
htraverse
应该看起来像这样:
class HFunctor t => HTraversable t where
htraverse :: Applicative a => (forall x. f x -> a (g x)) -> t f -> a (t g)
Traversable
, 和
hmapDefault
顺利起飞,
hmapDefault :: HTraversable t => (forall x. f x -> g x) -> t f -> t g
hmapDefault eta = runIdentity . htraverse (Identity . eta)
sequenceA
想出一个好的类比.我试过了
hsequence :: (HTraversable t, Applicative f) => t f -> f (t Identity)
hsequence = htraverse (fmap Identity)
htraverse
根据
hsequence
.和以前一样,
f
不是自然的转变。
htraverse f = hsequence . hmap f
-- • Couldn't match type ‘x’ with ‘g x’
-- Expected type: f x -> a x
-- Actual type: f x -> a (g x)
hsequence
类型签名错误。是
Applicative
问题 - 我需要一直到
indexed monads ? “从
Functor
类别到 Hask 的可遍历仿函数”的类应该是什么样的?这样的事情还存在吗?
最佳答案
首先,我们有 sequence = traverse id
.
这里是 htraverse
的第一个参数有类型 forall x. f x -> a (g x)
, 我们不能有 id
,但我们可以尝试使用同构。对于 f x
与 a (g x)
同构,我们可以选择f ~ Compose a g
.
htraverse = hsequence . hmap (Compose . eta)
hsequence :: Applicative a => t (Compose a g) -> a (t g)
hsequence = htraverse getCompose
关于haskell - "higher order Traversable"类应该是什么样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187945/
我收到这些错误... 严重:必须安装 JRE 1.3 或更高版本! 严重:必须安装JDK 1.3或更高版本! ...在 Windows 7 (x64) 上安装 JAI(Java 高级成像)1.1.3
我们在类里面学习高阶函数,我们的教授提到它们对网络编程很有用。我不确定在什么情况下会是这样,并且想知道是否有人在一些常见的 Web 编程任务中有过高阶函数的经验,以及它们在什么情况下会有用。 我看了我
我一直在思考我正在处理的库中的一个设计问题,我意识到使用存在类型可能允许我以一种简化库的许多部分的方式更改我的设计。但是,我似乎无法让它正常工作。 在我看来,myBuilder 符合 MultiSig
以下 Raku 脚本: #!/usr/bin/env raku use v6.d; grammar MyGrammar { rule TOP { '=' } token keywo
根据wiki AND 的优先级高于 OR。 我想知道,是否有一个子句表述为a || b && c 首先应该如何计算? (a||b)还是(b && c)? 最佳答案 由于优先级,它的计算结果为 (a |
有两个类。 B 类派生自 A。 class A { } class B : A { public B() { } public int Number { get
我知道add、get等基本操作的时间复杂度是O(logn)。但是我没有找到lower()和higher()的细节。那么,Java中TreeSet的lower()和higher()的时间复杂度分别是多少
在 this answer我当场编造了一些看起来有点像“高阶Traversable”的东西:比如Traversable但是对于从 Hask 到 Hask 的 endofunctors 类别的仿函数。
我想创建一个包装变异的更高级别的函数。我想为我的函数提供一个表达式参数,并能够在 mutate 中使用该表达式: datas 50) Error in mutate_impl(.data, dots
CLLocationManager 有两种请求权限的方法: requestWhenInUseAuthorization requestAlwaysAuthorization 但是,如果当前授权状态不是
到目前为止,我已经为他们设计了大约5种实验语言和解释器,用于教育,业余爱好和娱乐。 我注意到的一件事:类汇编语言仅包含子例程和条件跳转,因为结构要慢得多,高级语言具有if,while等。我同时开发了它
所以我将 SFML 用于计算机科学项目 - 制作国际象棋游戏。我有一个 Square 类,它是棋盘的一个正方形 - 目前,它包含四个顶点(成员变量 sf 中的四个 sf::Vertex 对象: :Ve
任何人都可以描述 TreeSet 集合的 higher() 方法在按降序排序时的这种行为: 代码: NavigableSet set = new TreeSet<>(); set.add(10); s
来自 Marijn Haverbeke 的Eloquent Javascript一书,有 this example同时引入高阶函数的概念: function greaterThan(n) { re
我有一个 div,它只包含一个高度为 400 像素的图像。 div 没有填充,但它的高度为 406px,导致其 img 下方有一个丑陋的 6px 灰色水平条纹。 灰色 background 的原因是可
我总是使用 System.BitConverter.ToDouble() 将字节转换为 double ,如下所示: double value = BitConverter.ToDouble(flt,
我正在尝试设置一些其他组不应该看到的组维护文件夹。目前,我通过取消单击@@sharing 选项卡中的“从更高级别继承权限”复选框来实现这一点,但我想自动执行此操作。 我在文档或谷歌搜索中找不到任何关于
Data.Array不为 Array 提供折叠类型。 在 Real World Haskell(第 12 章)中,据说原因是 Array s 可以根据程序员的需要以不同的方式折叠: First of
在以下代码中(来自Functional Programming in Scala): trait Functor[F[_]] { def map[A,B](fa: F[A])(f: A => B)
是否可以完全否定子文件夹中的 web.config? 显然,我在根目录中有一个 web.config。 如果我有一个名为“MyApp”的子文件夹,我可以在其中编写一堆代码并让它在不引用根目录下的 we
我是一名优秀的程序员,十分优秀!