小编Kev*_*ith的帖子

绑定到参数的函数类型

Brent Yorgey出色的UPenn Haskell课程介绍:

fmap2 :: Functor f => (a -> b -> c) -> (f a -> f b -> f c)
fmap2 h fa fb = undefined
Run Code Online (Sandbox Code Playgroud)

的类型h,fafb细分为:

h  :: a -> b -> c
fa :: f a
fb :: f b
Run Code Online (Sandbox Code Playgroud)

我不清楚为什么要h提到整个功能(a -> b -> c).

为什么不能h参考afa参考(b -> c)

括号是否(a -> b -> c)有所作为?

编辑 …

haskell

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

将字符串解析为选项[LocalDateTime]

我写了一个TimeFormatter来解析String一个Option[LocalDateTime].

API注意到可能抛出任何异常.

 private def convertToDateTime(date: String): Option[LocalDateTime] =
   try {
     Some( timeFormatter.parseLocalDateTime(date) )
   }
   catch { // DateTimeFormatter#parseLocalDateTime API throws these possible exceptions
     case e @ (_: IllegalArgumentException | _: UnsupportedOperationException) =>
       log.error(s"Could not convert $date to LocalDateTime", e); None

   }
Run Code Online (Sandbox Code Playgroud)

Joshua Bloch指出:

第39项:仅在特殊情况下使用例外.

我想过使用正则表达式来捕获错误.但是,我不确定我的reg-ex是否总是匹配jodatime解析Stringa的方式LocalDateTime.我可以相信API会抛出这些异常,但我宁愿不依赖于reg-ex的方法调用的内部.

从功能的角度来看,我宁愿不使用异常.

如何在没有例外的情况下编写此函数?

scala jodatime

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

自动函程实例

给出以下代数类型:

ghci> data Foo a = Foo a
Run Code Online (Sandbox Code Playgroud)

然后我实例化其中一个.

ghci> let f = Foo "foo"
Run Code Online (Sandbox Code Playgroud)

最后,我想打电话fmap申请一个函数,(a -> b) -> Foo a -> Foo b.

ghci> fmap (++ "bar") f

<interactive>:78:1:
    No instance for (Functor Foo) arising from a use of ‘fmap’
    In the expression: fmap (++ "bar") f
    In an equation for ‘it’: it = fmap (++ "bar") f
Run Code Online (Sandbox Code Playgroud)

但是,由于我没有实现一个Functor实例Foo,我无法使用fmap.

有没有办法Functor免费获得实例?我对Haskell的编译器一无所知,但也许它很聪明地知道fmapon Foo a只是适用(a -> …

haskell

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

列表的第一个和最后一个项目的模式匹配

是否可以在"开头f",然后是任何文本上进行模式匹配,并以"结尾b

我试过了:

f :: String -> Bool
f ('f':xs:'b') = True
f _            = False
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个错误:

explore/PatternMatching.hs:2:11:
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’
    In the pattern: 'b'
    In the pattern: xs : 'b'
    In the pattern: 'f' : xs : 'b'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

haskell

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

获取存储桶中每个 S3 对象的绝对路径

给定AWSS3Client,如何获取所有 S3 对象路径的完整列表?

例子:

存储桶名称:foo有 5 个对象

  • foo/bip/baz
  • foo/bip/bap
  • 富人/酒吧/1
  • 富人/酒吧/2
  • 富/1234

我想得到一个List[String]由这 5 个项目组成的。

我怎样才能做到这一点?

java amazon-s3

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

使用ContT构造器创建新的Monad Cont

我开始阅读所有Monads的母亲,并输入了这个例子:

import Control.Monad.Cont

ex1 = do
  a <- return 1
  b <- return 10
  return $ a+b
Run Code Online (Sandbox Code Playgroud)

但是我遇到了一个编译时错误:

ghci> :l ContMonad.hs 
[1 of 1] Compiling Main             ( ContMonad.hs, interpreted )

ContMonad.hs:4:4:
    No instance for (Monad m0) arising from a do statement
    The type variable ‘m0’ is ambiguous
    Relevant bindings include
      ex1 :: m0 Integer (bound at ContMonad.hs:3:1)
    Note: there are several potential instances:
      instance Monad ((->) r) -- Defined in ‘GHC.Base’
      instance Monad IO -- Defined in ‘GHC.Base’ …
Run Code Online (Sandbox Code Playgroud)

haskell

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

将`do notation`转换为>> = v.map

给出以下do notation代码:

do
 a <- return 1
 b <- [10,20]
 return $ a+b
Run Code Online (Sandbox Code Playgroud)

是否有更惯用的转换:

ghci> return 1 >>= (\x -> map (+x) [10, 20])
[11,21]
Run Code Online (Sandbox Code Playgroud)

ghci> return 1 >>= (\x -> [10, 20] >>= (\y -> [y+x]))
[11,21]
Run Code Online (Sandbox Code Playgroud)

haskell

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

Parboiled2 Parser示例

我试图从parboiled2尝试这个例子:

scala> class MyParser(val input: org.parboiled2.ParserInput) 
            extends org.parboiled2.Parser { 
                def f = rule { capture("foo" ~ push(42)) 
                } 
        }
defined class MyParser
Run Code Online (Sandbox Code Playgroud)

然后,我创建一个新MyParser的输入"foo".

scala> new MyParser("foo").f
res11: org.parboiled2.Rule[shapeless.HNil,shapeless.::
            [Int,shapeless.::[String,shapeless.HNil]]] = null
Run Code Online (Sandbox Code Playgroud)

但回报值是null.

如何从REPL 运行这个简单的f 规则

parsing scala parboiled2

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

理解MonadTrans的"亲切"

综观kindMonad:

ghci>:k Monad
Monad :: (* -> *) -> Constraint
Run Code Online (Sandbox Code Playgroud)

我相信那是因为它需要一个a来自m a并返回Monad约束.

看着MonadTrans*kind,我看到:

ghci>:i MonadTrans
class MonadTrans (t :: (* -> *) -> * -> *) where
  lift :: Monad m => m a -> t m a
        -- Defined in `Control.Monad.Trans.Class'
instance MonadTrans MaybeT
  -- Defined in `Control.Monad.Trans.Maybe'
Run Code Online (Sandbox Code Playgroud)

所以,第一个(* -> *)来自我Monadkind,我相信.但是* -> *呢?

haskell

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

重写模式匹配与理解

给出以下类型:

sealed trait Pet { 
  val name: String
}
case class Dog(override val name: String) extends Pet 
case class Cat(override val name: String) extends Pet 

sealed trait Error
case object DBConnection extends Error
case object NoResults extends Error
Run Code Online (Sandbox Code Playgroud)

我们编写了一个按名称搜索宠物的函数.

def foo(petName: String): Either[Error, Pet] = {
  val results: Either[Error, List[Pet]] = ??? // does not matter
  val foundPet: Option[Pet] = results match {
     case left @ Left(_) => None
     case Right(ps)      => ps.find(_.name == petName)
  }
  foundPet match { 
    case …
Run Code Online (Sandbox Code Playgroud)

monads scala

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

标签 统计

haskell ×6

scala ×3

amazon-s3 ×1

java ×1

jodatime ×1

monads ×1

parboiled2 ×1

parsing ×1