monads

What is the point of this monadic law?

大憨熊 提交于 2019-12-13 04:49:33
问题 I am reading an article about "monadic laws". The first law the article mentions is: m map f ≡ m flatMap {x => unit(f(x))} For Scala Option it means: option map f ≡ option flatMap {x => Option(f(x))} Now I wonder what the law point is. Why is the law important ? What if Scala Option does not obey this law ? 回答1: If it does not obey the monad laws it's not a monad. That's actually why the unit of Option is Some.apply and not Option.apply . Just look at this case: scala> val f = (x: Int) =>

Monad to catch multiple exceptions (not just fail on single)

孤街醉人 提交于 2019-12-13 00:15:31
问题 I have a similar question to what is asked here (Multiple or Single Try Catch), however in my case I need to follow this pattern for functional (and not performance reasons) Essentially I am handling parameter errors in Scalatra, and I need an easy way to catch if any conversions fail without the first failure skipping the rest of the try calls In other words, I need something that follows a pattern like this def checkMultiple(a:Seq[Try]):Either[Map[_,Throwable],Map[_,Success]] = { ??? } I

Is is possible to convert this recursive function to tail-recursive using continuation passing style?

自作多情 提交于 2019-12-12 18:26:41
问题 I have recently written an ETL, which works just fine. I would like to remind myself how to use free monads, so would like to convert my ETL as such. Note: my intention here is not to write a better ETL, but to re-familiarize myself with free monads. In re-learing how free monads work, I got side tracked with the topic of this question. So I asked a related question some months ago. Someone commented that my recursive function could be made tail-recursive using continuation passing style. I

Typeclass instances for functions

随声附和 提交于 2019-12-12 17:35:20
问题 I've just realized, that Functions have instances for Monad, Functor and Applicative. What I usually do, when I see some typeclass instance I don't get, is write some well-typed expression and see what it returns: Can somebody explain these instances? You usually hear about the instances for List and Maybe, which by now are natural to me, but I don't understand how Functions can be a Functor or even a Monad. EDIT: Okay, this is a valid well-typed expression that doesn't compile: fmap (+) (+)

Making the type signature independent of a specific monad transformer stack (Scala)

岁酱吖の 提交于 2019-12-12 13:09:04
问题 I'm learning Scala by translating a Haskell function to Scala. I have a monad transformer stack containing the StateMonad type TI a = ... One function using this monad transformer stack is: fresh :: TI Int fresh = do n <- get put (n + 1) return n Since this function only depends on the State monad I may also change the type to: fresh :: (MonadState Int m) => m Int How does this translate to Scala? In Scala, I use a monad transformer stack composing the state and the identity monad: type TI[A]

C++ monad library

一个人想着一个人 提交于 2019-12-12 12:07:37
问题 Does anyone know of a good monad template library in C++. Perhaps, one that provides some of the common monads that you would see in Haskell like Maybe. 回答1: You may want to check out the "monad.h" header in FC++. You can read more about this in the "Monads" part of this page: http://people.cs.umass.edu/~yannis/fc++/New1.5/lambda.html#monad However, this may not be suited to actual industrial use -- it's still a nice exercise to implement and use them in C++ though. 回答2: Something like Maybe

Scoping for temporary type variables

╄→гoц情女王★ 提交于 2019-12-12 10:58:57
问题 I have a large number of in place vector functions of the type f :: (M.MVector v r, PrimMonad m) => v (PrimState m) r -> v (PrimState m) r -> m () These functions mostly work in-place, so it is convenient to have their argument be a mutable vector so that I can compose, iterate, etc. However, at the top level, I only want to work with immutable "Haskell"/pure vectors. Here is an example of the problem: {-# LANGUAGE TypeFamilies, ScopedTypeVariables, MultiParamTypeClasses, FlexibleInstances #-

How are all graphic and web libraries implemented in Haskell?

北战南征 提交于 2019-12-12 10:49:20
问题 I only begin to learn Haskell. I've read that it is a pure functional language and everything in it is immutable. So things like input output, writing and reading databases cause mutability of the state. I know there is a thing in Haskell called monads which allow to use imperative features in Haskell like IO Monad . But I'm interesting is everything imperative in Haskell is implemented with the help of monads? On the HackageDB there are a lot of packages which allow to work with 3d-graphics,

Why is “bind” written as >>= in Haskell Monads?

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:49:05
问题 What are the origins of the syntax ">>=" and ">>" in Haskell Monads? I'm not looking for a explanation of how Monads work but instead why the language designers chose that syntax. To me, ">>=" and ">>" seems kind of arbitrary and never made intuitive sense. Does anyone have a intuitive explanation? Is it syntax that comes from category theory? 回答1: m >>= k suggests "feed the result of computation m to the function k "; m >> n suggests "run the m computation and then the n computation". 来源:

Validating list of strings

与世无争的帅哥 提交于 2019-12-12 03:39:45
问题 This is a follow up to my previous question. Suppose I need to write a function validate in order to make sure that a given list of strings consists of "a", "b", and one or more "c". def validate(ss: List[String]): Either[NonEmptyList[MyError], Unit] = ??? Suppose I have three functions to check if a given string is "a", "b", or "c": def validateA(str: String): Either[MyError, Unit] = ??? def validateB(str: String): Either[MyError, Unit] = ??? def validateC(str: String): Either[MyError, Unit]