- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有简单的元组(例如从数据库中读取),我不知道元素的数量和内容。例如。(String, Int, Int)
或 (String, Float, String, Int)
.
我想编写一个通用函数,它将采用各种元组并用字符串“NIL”替换所有数据。如果字符串“NIL”已经存在,它应该保持不变。
回到例子:("something", 3, 4.788)
应该导致 ("something", "NIL", "NIL")
("something else", "Hello", "NIL", (4,6))
应该导致 ("something else", "NIL", "NIL", "NIL")
我显然不知道从哪里开始,因为用已知的元组来做这件事不会有问题。在没有 Template Haskell 的情况下,是否有可能达到我想要的结果?
最佳答案
可以使用 GHC.Generics
,我想我会在这里记录它的完整性,尽管我不会推荐它而不是这里的其他建议。
这个想法是将你的元组转换成可以进行模式匹配的东西。典型的方法(我相信 HList
使用)是从 n 元组转换为嵌套元组:(,,,)
-> (,(,(,)))
.GHC.Generics
通过将元组转换为产品的嵌套应用程序来做类似的事情 :*:
构造函数。 to
和 from
是将值与其通用表示形式相互转换的函数。元组字段一般由 K1
表示。 newtypes,所以我们想要做的是通过元数据树( M1
)和产品( :*:
)节点向下递归,直到我们找到 K1
叶节点(常量)并用“NIL”字符串替换它们的内容。Rewrite
type 函数描述了我们如何修改类型。 Rewrite (K1 i c) = K1 i String
声明我们将用 c
替换每个值(String
类型参数) .
给定一个小测试应用程序:
y0 :: (String, Int, Double)
y0 = ("something", 3, 4.788)
y1 :: (String, String, String, (Int, Int))
y1 = ("something else", "Hello", "NIL", (4,6))
main :: IO ()
main = do
print (rewrite_ y0 :: (String, String, String))
print (rewrite_ y1 :: (String, String, String, String))
*Main> :main("something","NIL","NIL")("something else","NIL","NIL","NIL")
Using the built-in Generics
functionality and a typeclass to do the actual transformation:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
import Data.Typeable
import GHC.Generics
rewrite_
:: (Generic a, Generic b, Rewriter (Rep a), Rewrite (Rep a) ~ Rep b)
=> a -> b
rewrite_ = to . rewrite False . from
class Rewriter f where
type Rewrite f :: * -> *
rewrite :: Bool -> f a -> (Rewrite f) a
instance Rewriter f => Rewriter (M1 i c f) where
type Rewrite (M1 i c f) = M1 i c (Rewrite f)
rewrite x = M1 . rewrite x . unM1
instance Typeable c => Rewriter (K1 i c) where
type Rewrite (K1 i c) = K1 i String
rewrite False (K1 x) | Just val <- cast x = K1 val
rewrite _ _ = K1 "NIL"
instance (Rewriter a, Rewriter b) => Rewriter (a :*: b) where
type Rewrite (a :*: b) = Rewrite a :*: Rewrite b
rewrite x (a :*: b) = rewrite x a :*: rewrite True b
instance Rewriter U1 where
type Rewrite U1 = U1
rewrite _ U1 = U1
instance (Rewriter a, Rewriter b) => Rewriter (a :+: b) where
type Rewrite (a :+: b) = Rewrite a :+: Rewrite b
rewrite x (L1 a) = L1 (rewrite x a)
rewrite x (R1 b) = R1 (rewrite x b)
Typeable
可以从
K1
中删除约束例如,由于 Overlapping/UndecidableInstances,它是否更好是有争议的。 GHC 也无法推断结果类型,尽管它似乎应该可以。在任何情况下,结果类型都需要正确,否则您将收到难以阅读的错误消息。
关于haskell - 操作 "arbitrary"元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13436366/
我正在使用 QuickCheck 1,我有以下数据类型: data A = ... instance Arbitrary A where ... data B = ... instance Arbit
背景: Vim 有很多很棒的教程和“技巧”页面,但是非常困难的一件事是找到关于如何做一些可以在自己熟悉的文本编辑器 IDE 中轻松完成的任意事情的具体说明。因此,我要求逐步说明我将如何在 Vim 中执
我不确定正确的名称是什么,到目前为止我理解使用可变模板参数的概念(例如,与 std::initializer_list 相比)。 所以让我们假设有任意数量的类 k 和任意数量的参数 i 这取决于每个类
是否有任何 TypeScript 解决方案可以帮助我拥有“无限”数量的泛型类型,当然不必键入所有这些类型? 示例如下: type Args> = { component: T, presetProps
我有简单的元组(例如从数据库中读取),我不知道元素的数量和内容。例如。(String, Int, Int)或 (String, Float, String, Int) . 我想编写一个通用函数,它将采
rust quickcheck documentation注意对于任何实现 Arbitrary 的类型 They must also be sendable and static since ever
我有以下数据和函数 data Foo = A | B deriving (Show) foolist :: Maybe Foo -> [Foo] foolist Nothing = [A] fool
我尝试计算 math.exp(9500) 但遇到了 OverflowError: math range error(大约是 6.3e4125)。来自 this question似乎是由于 float
我想知道为什么需要任意,因为自动化属性测试需要属性定义,例如 val prop = forAll(v: T => check that property holds for v) 和值 v 生成器。用
@SuppressWarnings("unchecked") public static final Ordering ARBITRARY_ORDERING = (Ordering)Ordering.
我在替换“任意”定界符之间的文本文件部分时遇到一些问题。 这是我想要做的:我有如下标记: // [ADX START] ANYTHING // [ADX END] ANYTHING /*
据我所知,在 HTML5 中组成标签名称是完全合法的,并且它们可以正常使用 CSS 样式和嵌套等。 当然,我的任意标记名称对不理解它们的浏览器没有任何影响,但它大大提高了我的代码的可读性,从而更易于维
这个问题在这里已经有了答案: Typescript Function with Generic Return Type (2 个回答) 4 个月前关闭。 以下代码抛出错误: 'T' could be
请参阅以下 34 个字符的 ASCII 字符串。我试图在这个字符串上有 3 个 MD5 哈希匹配。 8AC905DD4AB2038E5F7EABEAE792AC41BC [A-F0-9]{32}仅匹配
假设我有一个充满 javascript 文件的目录: . +-- my_dir +-- apple.js +-- banana.js +-- main.js 子目录中的每个文件都包含一
找不到如何提出关于 Steve Pomeroy 发布的“使用 Android 播放任意音调”的问题,所以开始了一篇 here . 是否有任何代码需要添加到 xml 文件中? 无法让 sim 发出声音。
SO_RCVTIMEO 根本没有在我的输出函数中创建超时条件。我正在使用 ARQ 滑动窗口协议(protocol)设计文件传输服务。为了保持一切有序,我从基本的“停等”开始。 这是我对 timeval
我将在当前工作的网站上创建竞赛。每个比赛都不会相同,并且可能有不同数量的输入字段,用户必须输入这些字段才能成为比赛的一部分,例如。 比赛 1 可能只需要一个名字 比赛 2 可能需要名字、姓氏和电子邮件
我有一个在 Android OpenGL-ES 上渲染的点云。我可以正确翻译它(我认为)但是当我旋转它时,我无法让它按预期工作。我希望它围绕点云的中心旋转(我有这个 3D 点),但我不知道该怎么做。
是否可以为某些 PostgreSQL 函数定义任意返回类型? 这个想法是,根据调用的不同,可能会返回不同的输出。 例如,假设我们有: TypeA : (name, email) TypeB : (na
我是一名优秀的程序员,十分优秀!