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,fa并fb细分为:
h :: a -> b -> c
fa :: f a
fb :: f b
Run Code Online (Sandbox Code Playgroud)
我不清楚为什么要h提到整个功能(a -> b -> c).
为什么不能h参考a和fa参考(b -> c)?
括号是否(a -> b -> c)有所作为?
编辑 …
我写了一个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的方法调用的内部.
从功能的角度来看,我宁愿不使用异常.
如何在没有例外的情况下编写此函数?
给出以下代数类型:
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 -> …
是否可以在"开头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) 给定AWSS3Client,如何获取所有 S3 对象路径的完整列表?
例子:
存储桶名称:foo有 5 个对象
我想得到一个List[String]由这 5 个项目组成的。
我怎样才能做到这一点?
我开始阅读所有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) 给出以下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) 我试图从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 规则?
综观kind的Monad:
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)
所以,第一个(* -> *)来自我Monad的kind,我相信.但是* -> *呢?
给出以下类型:
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)