monads

Managing array of monads in fp-ts and Functional Programming

血红的双手。 提交于 2020-07-07 12:04:29
问题 I'm very new to Functional Programming and I'm struggling a lot with running traverse on arrays. When I read this book it seems that I should be able to simply traverse between Monads but I can't wrap my head around this concept with fp-ts. Can someone explain the following using array.traverse/sequence or any other ways please? How can I go from TaskEither<Error, string[]> to TaskEither<Error, Either<Error, string>[]> ; or is there a better way to go from a single error to nested errors

What's the idiomatic way to handle multiple `Option<T>` in Rust?

不想你离开。 提交于 2020-06-27 07:39:09
问题 Since I'm fairly new to Rust, I need guidance on how error handling is done idiomatically. I find the error-handling boilerplate really annoying. I'm stuck with multiple Option<T> s. It's too verbose to handle each None case manually. In Haskell, for example, you can chain optional value ( Maybe ) operations with a variety of operators: fmap , <*> , >>= , etc.: f x = x * x g x = x ++ x main = print $ g <$> show <$> f <$> Just 2 The same looks impossible in Rust. I'm trying to parse a two

Making Read-Only functions for a State in Haskell

倖福魔咒の 提交于 2020-06-27 07:07:48
问题 I often end up in a situation where it's very convenient to be using the State monad, due to having a lot of related functions that need to operate on the same piece of data in a semi-imperative way. Some of the functions need to read the data in the State monad, but will never need to change it. Using the State monad as usual in these functions works just fine, but I can't help but feel that I've given up Haskell's inherent safety and replicated a language where any function can mutate

When should one use a Kleisli?

一个人想着一个人 提交于 2020-06-24 03:24:09
问题 I recently stumbled on the concept of a Kleisli and every tutorial/link/reference that I read motivates the use of Kleisli via the following constructs: Composing functions that return monads : f: a -> m[b] with g: b -> m[c] - I think the very definition of a monad already captures this case - do/bind/for/flatMap do that. One needn't lean on the Kleisli construct to achieve this. So this cannot be the "primary" use case of a Kleisli IMO. Inserting configuration : This one states that if

When should one use a Kleisli?

瘦欲@ 提交于 2020-06-24 03:23:49
问题 I recently stumbled on the concept of a Kleisli and every tutorial/link/reference that I read motivates the use of Kleisli via the following constructs: Composing functions that return monads : f: a -> m[b] with g: b -> m[c] - I think the very definition of a monad already captures this case - do/bind/for/flatMap do that. One needn't lean on the Kleisli construct to achieve this. So this cannot be the "primary" use case of a Kleisli IMO. Inserting configuration : This one states that if

How to convert Either to MonadThrow

一笑奈何 提交于 2020-06-16 07:18:30
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

How to convert Either to MonadThrow

夙愿已清 提交于 2020-06-16 07:18:15
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

Two polymorphic classes in one function

99封情书 提交于 2020-05-29 07:03:31
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes

Two polymorphic classes in one function

走远了吗. 提交于 2020-05-29 07:03:17
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes

Two polymorphic classes in one function

谁说我不能喝 提交于 2020-05-29 07:02:13
问题 I have this code with State monads: import Control.Monad.State data ModelData = ModelData String data ClientData = ClientData String act :: String -> State ClientData a -> State ModelData a act _ action = do let (result, _) = runState action $ ClientData "" return result addServer :: String -> State ClientData () addServer _ = return () scenario1 :: State ModelData () scenario1 = do act "Alice" $ addServer "https://example.com" I am trying to generalise it with polymorphic type-classes