小编Sau*_*nda的帖子

iCal 供稿和日期范围

我试图了解 iCal 提要和 iCalendar 客户端如何处理过去和未来的大量事件。iCalendar 客户端是否可以通过任何方式将日期范围与 iCalendar 提要进行通信——这样显示给用户的当前日期范围之外的事件就不会不必要地通过网络发送。

如果不是,iCalendar 提要如何决定发送事件的日期范围?如果一个人继续通过网络发送所有数据,在某些时候它将变得无法管理,无论是供稿还是客户端。如果提要不会在过去很远很远的将来发送数据,那么客户端如何处理数据中的这些“漏洞”?

icalendar rfc2445 rfc5545

5
推荐指数
1
解决办法
1647
查看次数

如何使用Yesod-Persistent记录语法

以下为什么不工作?我有点知道,有很多去引擎盖下和User类型可能不真的有email,createdAtupdatedAt领域.什么是不使用位置参数实例化对象的最佳方法,这很容易失控?

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
  email String
  createdAt UTCTime Maybe default=CURRENT_TIME
  updatedAt UTCTime Maybe default=CURRENT_TIME
  deriving Show
]]

main :: IO ()
main = runSqlite ":memory:" $ do
  runMigration migrateAll
  u <- insert $ User {email="saurabhnanda@gmail.com" createdAt=Nothing updatedAt=Nothing}
Run Code Online (Sandbox Code Playgroud)

编译错误:

trysql.hs:38:23:
    ‘email’ is not a (visible) field of constructor ‘User’

trysql.hs:38:55:
    ‘createdAt’ is not a (visible) field of constructor ‘User’

trysql.hs:38:74:
    ‘updatedAt’ is not a (visible) field of constructor ‘User’
Run Code Online (Sandbox Code Playgroud)

haskell yesod

5
推荐指数
1
解决办法
136
查看次数

如何有效阅读Yesod错误信息?

(新手提醒)

\n\n

鉴于以下错误消息,找到错误根源的最快方法是什么:

\n\n
08/Jul/2016:11:39:01 +0530 [Error#yesod-core] expected EPlain but got Nothing for: DerefBranch (DerefIdent (Ident "show")) (DerefString "abcdef") @(yesod_3MCr4WfhviiELXmo3fAaXL:Yesod.Core.Class.Yesod ./Yesod/Core/Class/Yesod.hs:625:5)\nGET /\n  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\n  Status: 500 Internal Server Error 0.054158s\n\nHandler/Home.hs:38:11:\n    No instance for (Text.Julius.ToJavascript String)\n      arising from a use of \xe2\x80\x98Text.Julius.toJavascript\xe2\x80\x99\n    In the second argument of \xe2\x80\x98(GHC.Base..)\xe2\x80\x99, namely\n      \xe2\x80\x98Text.Julius.toJavascript\xe2\x80\x99\n    In the second argument of \xe2\x80\x98(GHC.Base..)\xe2\x80\x99, namely\n      \xe2\x80\x98(Text.Julius.unJavascript GHC.Base.. Text.Julius.toJavascript)\xe2\x80\x99\n    In the expression:\n      Text.Shakespeare.EPlain\n      GHC.Base..\n        (Text.Julius.unJavascript GHC.Base.. Text.Julius.toJavascript)\nBuild failure, pausing...\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在使用一个简单的脚手架网站(没有数据库),并且故意搞乱了homepage.julius. 在这个特定的实例中,我确切地知道错误是什么,但是如何仅通过查看错误消息就知道错误是什么?

\n

haskell yesod shakespeare-text

5
推荐指数
1
解决办法
325
查看次数

如何编写一个可以序列化/反序列化类似地图结构的任何记录的通用函数?

我一直在使用每个可用的泛型库(EOT、SOP、Data/SYB 和 GHC.Generics)来解决这个问题。我对每个库都编写了一半的代码示例,它们要么无法编译,要么抛出运行时错误。

核心问题是这样的:

type FieldName = String
type FieldValue = String
type MapType = [(String, String)] -- can be an actual HashMap as well, but doesn't really matter
data User = User {name :: String, email :: String}
data Post = User {title :: String, body :: String}

gFromMap :: MapType -> Maybe a
gToMap :: a -> MapType

-- the following should work
gFromMap [("name", "Saurabh"), ("email", "redacted@redacted.com")] :: User -- Just (User{..})
gFromMap [("title", "Will this work?"), …
Run Code Online (Sandbox Code Playgroud)

generics haskell

4
推荐指数
1
解决办法
380
查看次数

如何根据Reader环境中的设置有条件地解析JSON?

有没有办法将读者环境传递给Aeson的JSON(de)序列化功能?这是一个真实的例子,说明为什么需要这样做?

-- JSON instances for decimal -- ORPHAN instances

defaultPrecision :: Word8
defaultPrecision = fromInteger 2

instance ToJSON Data.Decimal.Decimal  where
  toJSON d = toJSON $ show d

instance FromJSON Data.Decimal.Decimal  where
  -- TODO: New problem! How do we get the precision dynamically, based on
  -- the currency settings of the logged-in user
  parseJSON (Number n) = return $ Data.Decimal.realFracToDecimal defaultPrecision n
  parseJSON x = fail $ "Expectig a number in the JSON to parse to a Decimal. Received " ++ …
Run Code Online (Sandbox Code Playgroud)

haskell aeson

4
推荐指数
1
解决办法
236
查看次数

如何获得类型家族更好的错误消息?

我正在尝试构建一个静态类型的授权系统,并具有以下工作代码片段:

{-# LANGUAGE DataKinds, ScopedTypeVariables, TypeFamilies #-}

module Try where

import Data.Singletons.TH

data FeatureFlag = Feature1 | Feature2 deriving (Eq, Show)
$(genSingletons [''FeatureFlag])

type family Feature (f :: FeatureFlag) (fs :: [FeatureFlag]) where
  Feature f '[] = 'False
  Feature f (f:fs) = 'True
  Feature f (q:fs) = Feature f fs  


lockedFeatureAction :: (MonadIO (m fs), Feature 'Feature1 fs ~ 'True) => m fs ()
lockedFeatureaction = undefined

checkFeatureFlagsAndRun :: forall (fs :: [FeatureFlag]) . (SingI fs) => Proxy fs -> …
Run Code Online (Sandbox Code Playgroud)

haskell type-families data-kinds

4
推荐指数
2
解决办法
89
查看次数

Haskell 中可变但可锁定的数据结构?

Haskell 中是否有一个标准数据结构,它像 一样可变IORef,但是,如果需要,也可以“锁定”,例如MVar?这是我想要实现的目标:

  • 有多个线程调用基于 OAuth 的 API,它们都需要 AccessToken
  • 但是,AccessToken可能会过期,并且其中一个线程将是第一个知道的(因为它将得到401响应)。让我们调用这个线程T1
  • T1refreshToken在重试原始 API 调用之前立即调用该函数。此时,代码需要确定两件事:
    1. 尝试读取AccessToken--时所有新线程都被阻塞,直到它被刷新,并且AccessToken在此共享数据结构中提供了一个新线程
    2. 所有其他线程,可能在401之后不久收到了T1,在调用refreshToken函数时被阻塞。

我已经使用 anIORefAccessToken可变方式存储。但是,我不确定是否应该使用单独的MVar来保护对refreshToken函数的并发访问。是否有内置的数据结构可以做到这一点?

concurrency haskell

4
推荐指数
1
解决办法
108
查看次数

如何拥有可由用户覆盖的“依赖”默认值?

的零工作业队列库中具有以下功能。有一堆配置参数,其中默认实现取决于另一个配置参数。例如:

  • cfgJobToHtml取决于cfgJobType,默认为defaultJobType。但是,在调用 之后defaultConfig,用户可以选择覆盖 的值cfgJobType 而不更改cfgJobToHtml。预期的行为是cfgJobToHtml现在应该使用用户提供的值而不是defaultJobType
  • 同样,cfgAllJobTypes取决于cfgJobTypeSql,这反过来默认为defaultJobTypeSql。同样,在调用 之后defaultConfig,如果用户覆盖 的值cfgJobTypeSqlcfgAllJobTypes则应使用覆盖的值,而不是defaultJobTypeSql

下面的代码不像我期望的那样工作。如果更改,cfgJobType则更改不会被cfgJobToHtml. 同样,对于cfgJobTypeSql.

拥有这些“依赖”默认值的最佳方法是什么?

-- | This function gives you a 'Config' with a bunch of sensible defaults
-- already applied. It requires the bare minimum arguments that this library
-- cannot assume on your behalf. …
Run Code Online (Sandbox Code Playgroud)

haskell

4
推荐指数
1
解决办法
155
查看次数

使用Maybe遍历嵌套记录的更短方法

是否有更短/更清晰的方式编写以下代码片段:

fromMaybe "" $ fmap (^. fullName) (bi ^. bookerContact)
Run Code Online (Sandbox Code Playgroud)

bi ^. bookerContact可能会导致Maybe Contact记录,这就是^. fullName需要fmap的原因.在嵌套遍历之后,如果我们最终得到一个Nothing我们用fromMaybe ""它来默认它为空字符串.

haskell lenses haskell-lens

3
推荐指数
1
解决办法
285
查看次数

如何创建一个允许IO但不是MonadIO的monad?

我正在尝试创建一个只允许特定IO功能的monad.这意味着这个假设的monad不能是a MonadIO并且不能被允许liftIO被调用.

这是我到现在所拥有的,但我坚持使用以下Monad实例AppM:

data AppM a = AppM {unwrapAppM :: ReaderT Env (LoggingT IO) a}

instance Functor AppM where
  fmap fn appm = AppM $ fmap fn (unwrapAppM appm)


instance Applicative AppM where
  pure a = AppM $ pure a
Run Code Online (Sandbox Code Playgroud)

monads haskell monad-transformers

3
推荐指数
1
解决办法
167
查看次数