- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定以下类(class):
class ListIsomorphic l where
toList :: l a -> [a]
fromList :: [a] -> l a
如何使用 Data.Vector.Generic
编写向量类型的实例?这不起作用:
instance (V.Vector v a) => ListIsomorphic v where
toList = V.toList
fromList = V.fromList
给我:
test.hs:31:10:
Variable ‘a’ occurs more often than in the instance head
in the constraint: V.Vector v a
(Use UndecidableInstances to permit this)
In the instance declaration for ‘ListIsomorphic v’
最佳答案
不要。将所有 v
的实例添加到 Listable
类中,由于实例重叠,使用起来会变得很麻烦。
Vector v a => v
与列表不同构,因为它受到哪些项可以是列表元素的限制。您需要一个捕获此约束的类,例如
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Constraint
class ConstrainedList l where
type Elem l a :: Constraint
toList :: Elem l a => l a -> [a]
fromList :: Elem l a => [a] -> l a
我们不会为所有类型 Vector v a => v
添加 ConstrainedList
实例(这会让我们陷入重叠实例领域),而是只为我们想要的类型定义它。下面将涵盖所有带有 Vector
的类型矢量包中的实例。
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Generic as VG
instance ConstrainedList VP.Vector where
type Elem VP.Vector a = VG.Vector VP.Vector a
toList = VG.toList
fromList = VG.fromList
您可以为常规列表 []
编写一个 ConstrainedList
实例,该实例仅需要其元素为空约束。
instance ConstrainedList [] where
type Elem [] a = ()
toList = id
fromList = id
任何使用 toList
或 fromList
的地方也需要 Elem l a
实例。
cmap :: (ConstrainedList l, Elem l a, Elem l b) => (a -> b) -> l a -> l b
cmap f = fromList . map f . toList
当我们知道列表和元素的具体类型时,这些函数将很容易使用,而不会受到约束的影响。
cmap (+1) [1,2,3,4]
不要尝试下面的操作。如果您对与列表同构且没有附加约束的事物类感兴趣,只需为其创建另一个类即可。这只是展示了当您将自己逼入绝境时可以做什么:召唤一条龙。
您还可以编写需要证明 ConstrainedList
的元素没有约束的函数。这离 GHC 并不真正支持的 constraints
包和编程风格的领域很远,但是没有足够的 constraints
示例,所以我会离开这里就是这个。
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
map' :: forall l a b. (ConstrainedList l, () :=> Elem l a, () :=> Elem l b) =>
(a -> b) -> l a -> l b
map' f = case (ins :: () :- Elem l a) of { Sub Dict ->
case (ins :: () :- Elem l b) of { Sub Dict ->
fromList . map f . toList
}}
我们可以通过检查 Elem l a ~ ()
来检查 ConstrainedList
是否没有约束,但如果它的约束是用不同的语言编写的,那么这将不起作用方式。
{-# LANGUAGE FlexibleInstances #-}
class Any a
instance Any a
data AList a = AList {getList :: [a]}
deriving (Show)
instance ConstrainedList AList where
type Elem AList a = Any a
toList = getList
fromList = AList
()
与 Any a
的类型不同,尽管 ()
隐含 Any a
。约束包通过将它们具体化为类型类 Class
来捕获这样的关系。和 :=>
{-# LANGUAGE MultiParamTypeClasses #-}
-- class () => Any a
instance Class () (Any a) where
cls = Sub Dict
-- instance () => Any a
instance () :=> Any a where
ins = Sub Dict
所有这些工作让我们可以轻松地重用函数,而无需在已知具体列表类型时提供所有这些字典。
map'' :: (a -> b) -> AList a -> AList b
map'' = map'
关于haskell - 如何为通用向量创建列表同构实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32129603/
据我所知,Aurelia 不支持提到的服务端渲染 here . 但问题是:是否可以通过一些技巧/解决方法来做到这一点? 最明显的想法是使用 Phantom、Nightmare.js 或其他任何工具在服
我目前正在开发一个在客户端和服务器之间使用一些共享 JS 的项目。技术堆栈包括 Node v6、Webpack、React 等。 有一个“共享”目录,服务器和客户端需要一个名为rules.js的文件。
我正在运行一个使用 React 和 webpack 的同构应用程序,一切都很好。不过,我正在努力处理的一件事是在渲染组件之前预加载 css。 我一直在尝试使用 导入我的 sass 文件(以便 webp
我找到了 2 个工具来解决服务器端模块加载器的问题:webpack-isomorphic-tools和 universal-webpack .有人可以向我解释这些东西如何工作的关键步骤吗?它如何捕获/
我基于 this repo 中的初学者工具包构建了一个同构 React 应用程序.它使用 webpack 构建生产代码。 问题是,我需要将服务器上的一些环境变量的值暴露给浏览器中的客户端代码,而无需重
我有一些同构的 JavaScript。我在客户端使用 RequireJS。 (function() { 'use strict'; function wrapper(require)
在 recursion-schemes 包定义了以下类型: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a
我正在努力了解服务器端路由和重定向与服务器端呈现/同构 javascript 之间的区别。如果它们不同,它们有何不同。 最佳答案 Universal (Isomorphic)Javascript 可以
我对 JavaScript 知识相当了解。我们计划使用 React、Redux、es6 等启动同构 Web 应用程序。我们有很多 API 调用需要集成。在开始之前,我想知道是否有可用的样板,或者最好一
我将应用程序重建为同构方法。在我的本地环境中一切正常(本地和在线 Node 版本相同)但不幸的是,将文件上传到我的网络服务器后,我收到以下错误消息: SyntaxError: .../index.js
我正在尝试创建一个 universal javascript (正式名称为同构 javascript)包。这意味着它应该作为 Node 模块工作,但也可以在浏览器中顺利运行 假设它看起来像这样: //
所以我几乎完成了我的第一个(同构)ReactJS,当我们部署它时,完成 build.js 有点慢。一个建议是使用 CDN 来分离 Assets 获取(cdn0、cdn1、cdn2...),我想知道如何
所以,我最近一直在摆弄一些同构的 React + Flux,说实话,发现一些概念相当令人困惑。我一直在研究有关如何构建同构应用程序的最佳实践并寻求建议。 假设您正在创建一个由同一 REST API 支
我正在 Next.js remote fetching 上做以下练习。我无法理解和适应的是练习从 http://api.tvmaze.com 获取数据。 . 数据如下: [ {"score":24,
我似乎遇到了一个奇怪的错误。我目前正在使用 Redux 同构,并且还包括 redux-thunk 作为异步操作的中间件。这是我的商店配置: // Transforms state date from
我在 CNTK 中调用 trainer.restore_from_checkpoint 时遇到以下异常。 'This' function is not equivalent (isomorphic)
我已经使用 nodejs 创建了库,并使用 webpack 将其捆绑,以便可以在客户端使用。 但如果我尝试将捆绑文件用于我的 Node 应用程序,它就无法工作。那么,我该如何创建同时适用于客户端和服务
我开始创建基于 Node.js 的同构 React/Redux 应用程序。该项目的一个要求是基于“移动”和“桌面” View 的特定组件的“自适应”渲染。我已经实现了 Redux Action 和缩减
关于同构通量应用程序中存储数据填充的问题。 (我使用的是 react、alt、iso 和 node,但理论适用于其他示例) 我有一个 flux 'store' ( http://alt.js.org/
我最近使用 React-Redux-Express-Mongoose 堆栈构建了一些同构/通用项目。 在我的 Mongoose 模型中包含很多业务逻辑。作为一个非常基本的示例(请原谅我的 ES6):
我是一名优秀的程序员,十分优秀!