- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一棵树,其中包含不同类型的节点。它们使用数据类型进行标记:
data Wrapping = A Int
| B String
我想写两个函数:
scatter :: Wrapping -> a
gather :: a -> Output
这个想法是我可以使用 (scatter.gather)::Wrapping -> Output。当然,分散和聚集函数都会有几种不同的变体(每个分散变体都有一个唯一的 Wrappingn 数据类型,但中间类型集始终是相同的),我希望能够干净地组合它们。
我遇到的问题是类型参数 a 并不是真正自由的,它是一个小的显式类型集(这里是 {Int,String})。如果我尝试将迄今为止的内容编码到 Haskell 类型类中,那么我会得到:
{-# LANGUAGE FlexibleInstances #-}
data Wrapping = A Int | B String
class Fanin a where
gather :: a -> String
instance Fanin Int where
gather x = show x
instance Fanin String where
gather x = x
class Fanout a where
scatter :: Fanout a => Wrapping -> a
instance Fanout Int where
scatter (A n) = n
instance Fanout String where
scatter (B x) = x
combined = gather.scatter
这两个类类型检查正常,但显然最后一行会抛出错误,因为 ghc 知道类型参数在每种情况下都匹配,仅在我定义的两个情况下匹配。我尝试过从一个类扩展另一个类的各种组合:
class Fanin a => Fanout a where ...
class Fanout a => Fanin a where ...
最后,我研究了 GADT 和存在类型来解决这个问题,但我在黑暗中跌跌撞撞。我找不到向 GHC 表达合法合格类型签名的方法,我尝试了以下组合:
{-# LANGUAGE RankNTypes #-}
class (forall a. Fanout a) => Fanin a where
class (forall a. Fanin a) => Fanout a where
问题:如何向 GHC 表达我想要将 a
限制为集合中的两种类型?
我感觉解决方案在于我研究过的一种技术,但我太迷失了,看不到它是什么......
最佳答案
The idea is that I can use (scatter.gather) :: Wrapping -> Output. There will of course be several different variations on both the scatter and the gather function (with each scatter variant having a unique Wrappingn datatype, but the set of intermediate types will always be the same) and I want to be able to cleanly compose them.
如果我理解正确的话,你想要不同的Wrapping
类型但中间 a
类型不断Either Int String
。我们可以在我们的类中反射(reflect)这些信息:
data Wrapping = A Int
| B String
class Fanout wrap where
scatter :: wrap -> Either Int String
instance Fanout Wrapping where
scatter (A n) = Left n
scatter (B str) = Right str
class Fanin output where
gather :: Either Int String -> output
instance Fanin String where
gather = either show id
combined :: Wrapping -> String
combined = gather . scatter
此外,从我从问题中收集到的信息来看,这个用例似乎不太适合输入类。特别是,我们可以去掉 Fanin
,然后 combined = either show id . scatter
在我看来,比之前的定义更好。
仅当只有一个 Either Int String -> a
时,类型类解决方案才有意义。或a -> Either Int String
函数对每个 a
都有意义,并且您想强制执行此操作。
关于haskell - 如何对 Haskell 中的类型执行分散/聚集操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32473227/
我想沿着一个轴从特定位置的X数组中分散并收集元素。 因此,给定一个索引数组idx,我想选择第0列的idx(0) th元素,第1列的idx(1) th元素,等等。 在Numpy中,以下语句: X = n
我有一个在集群硬件中运行的 Java 应用程序。我使用 Hashmap 作为缓存。我也希望缓存能够集群。有没有一些开源java项目可以在两台连接的机器上同步Hashmap? 最佳答案 看看 Hazel
这个问题在这里已经有了答案: Count number of times that an item occurred in each of multiple columns (4 个答案) 关闭 5
我一直在尝试“组合”一个列表 I mean putting items together depending on the item inbetween, so ['d','-','g','p','q
非聚集文件存储在数据文件中,聚集索引按逻辑索引顺序存储数据。 逻辑顺序位于哪里? 最佳答案 大多数 MySQL 索引的结构为 BTree。 (参见维基百科) (我在此讨论中排除了 FULLTEXT 和
所有源和目标都位于不可缓存的内存中。毫无疑问。在启动 DMA(即分散-聚集 DMA)之前,我构建了 DMA 链表(包含 src、dest、size 和 next)。我想我不必将列表放入不可缓存的内存中
我需要将输入字符串拆分为输出字符串(按一定顺序)通过在输入字符串上应用一组正则表达式。我想实现这个功能与 akka Actor 集群一起使用,我将其分散正则表达式和输入字符串并收集字符串。不过我想知道
Windows 文件系统支持scatter/gather IO .(当然,其他平台也可以) 但是不知道什么时候用IO机制。 你能给我解释一个合适的案例吗? 使用I/O机制我们能得到什么好处?(只是一点
我正在尝试使用克隆调用两个端点并收集它们的信息以通过聚合发送,我必须将其与分散收集中介器一起使用。每个端点返回一个 json 字符串。但我一直遇到“期望 SOAP Envelope 的实现作为父级”错
我想要实现的目标是使用户空间中的内存块可以通过 PCIe 直接由 FPGA 板中的 DMA 内核访问(不受内核的任何干扰)。 为此,我在用户空间中使用 posix_memalign() 在用户空间中分
假设我有以下数据框: > a a Source: local data frame [3 x 2] my_type_1_num_widgets my_type_2_num_widgets 1
我已经搜索了一段时间,但似乎无法在文档或 SO 上找到任何有用的信息。 This question并没有真正帮助我,因为它引用了修改程序集,而我正在用 C 语言编写。 我有一些代码进行间接访问,我想对
我已经坚持了一段时间了。我的代码基于 https://webrtc.org/getting-started/firebase-rtc-codelab .我基本上只是将其更改为 React 和 fire
我可以合并 Mono> 的列表吗?数据源整合为单个Mono>包含所有项目而不阻塞? 在我的带有 Lombok 分散收集应用程序的 JDK 9 Spring Boot 2 中,此阻塞版本有效:
我是一名优秀的程序员,十分优秀!