maybe

Maybe monad construction

谁都会走 提交于 2019-12-10 11:20:30
问题 I'm currently struggling with a new element of Haskell: Monads. Therefore I was introduced to this by an example of creating a (>>=) operator that executes a function on a Maybe type (taking its actual integer value as argument to it) only if it's not equal to Nothing , and otherwise return Nothing : (>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b Nothing >>= _ = Nothing (Just x) >>= f = f x However, I'm not quite sure how this works with the following usage of it: eval (Val n) = Just n eval

How to filter out “Nothing” values from Elm Array?

旧城冷巷雨未停 提交于 2019-12-09 14:51:56
问题 I'd like to define the following function: compactAndConvertToList : Array (Maybe String) -> List String This function should remove all Nothing appearances in the given array, and convert it to List . I came up with the solution below, but it feels dirty a bit. Is there a better way to achieve this? import Graphics.Element exposing (..) import Array model : Array.Array (Maybe String) model = Array.fromList [ Just "Hello", Just "Stack", Nothing, Just "Overflow" ] compactAndConvertToList :

Shorter way to write this code

流过昼夜 提交于 2019-12-09 02:20:17
问题 The following pattern appears very frequently in Haskell code. Is there a shorter way to write it? if pred x then Just x else Nothing 回答1: You're looking for mfilter in Control.Monad: mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a -- mfilter odd (Just 1) == Just 1 -- mfilter odd (Just 2) == Nothing Note that if the condition doesn't depend on the content of the MonadPlus , you can write instead: "foo" <$ guard (odd 3) -- Just "foo" "foo" <$ guard (odd 4) -- Nothing 回答2: Hm... You are

Why does “return Nothing” return Nothing?

雨燕双飞 提交于 2019-12-08 15:24:49
问题 "return a" is supposed to wrap a in the context of some Monad: *Main> :i return class Applicative m => Monad (m :: * -> *) where ... return :: a -> m a ... -- Defined in ‘GHC.Base’ If I ask GHCI what the type of "return Nothing" is, it conforms to that: *Main> :t return Nothing return Nothing :: Monad m => m (Maybe a) But if I evaluate it, I see no outer Monad, just the inner Maybe: *Main> return Nothing Nothing 回答1: When GHCi goes to print a value, it tries two different things. First, it

Is there an idiomatic alternative to nil-punning in Clojure?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 22:23:07
问题 I'm reading some Clojure code at the moment that has a bunch of uninitialised values as nil for a numeric value in a record that gets passed around. Now lots of the Clojure libraries treat this as idiomatic. Which means that it is an accepted convention. But it also leads to NullPointerException , because not all the Clojure core functions can handle a nil as input. (Nor should they). Other languages have the concept of Maybe or Option to proxy the value in the event that it is null, as a way

How do I concisely write a || b where a and b are Optional values?

对着背影说爱祢 提交于 2019-12-05 17:09:31
问题 I'm happy with an answer in any language, but I ultimately want an answer in Java. (Java 8+ is fine. Not limited to Java 8. I've tried to fix the tags.) If I have two Optional<Integer> values, how do I concisely compute the equivalent of a || b , meaning: a , if it's defined; otherwise b , if it's defined; otherwise empty() ? Optional<Integer> a = ...; Optional<Integer> b = ...; Optional<Integer> aOrB = a || b; // How to write this in Java 8+? I know that I can write a.orElse(12) , but what

Is there a point-free way to convert a conditional check into a Maybe type of the input?

本小妞迷上赌 提交于 2019-12-05 08:13:07
I am just working through some simple exercises in haskell and was wondering if there was a point-free way of converting an if-then-else statement into a Maybe type: Nothing being returned if the condition is false, and Just the input if the condition is true. In short, given some: maybeIf :: (a -> Bool) -> a -> Maybe a maybeIf cond a = if cond a then Just a else Nothing Is there an implementation that is point-free with respect to a ? I've also been looking at a more concrete version, a -> Maybe a , and feel like there may be an answer somewhere in Control.Arrow . However, since Maybe is a

Haskell maps returning a monad

谁说胖子不能爱 提交于 2019-12-05 01:22:40
The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I have to use an older version of the library? Don's lift converts Maybe 's elements to their general Monad

Simplifying nested Maybe pattern matching

懵懂的女人 提交于 2019-12-04 13:42:40
I have the following construct in my code: f :: Maybe A -> X f a = case a of Nothing -> x (Just b) -> case b of Nothing -> y (Just c) -> case c of Nothing -> z (Just d) -> d I'm not seeing an obvious way to simplify this instead of using nested maybe functions, which wouldn't make the whole thing look much better. Are there any clever - but still understandable - tricks that would help make this construct more "elegant"? UPDATED 2 Monad Either is for you import Data.Maybe (maybe) maybeE :: e -> Maybe a -> Either e a maybeE e = maybe (Left e) Right f :: Maybe (Maybe (Maybe d)) -> Either e d f a

Implementing the Haskell-MaybeMonad in F# - how can we get this lazy?

╄→гoц情女王★ 提交于 2019-12-04 12:33:27
问题 we are trying to build the Haskell-MaybeMonad sample from http://www.haskell.org/all_about_monads/html/maybemonad.html in F#. The idea is to search for a mailaddress in two dictionaries. If one of the both lookups returns a result we look into the third one. let bindM x k = match x with | Some value -> k value | None -> None let returnM x = Some x type MaybeBuilder() = member this.Bind(x, k) = bindM x k member this.Return(x) = returnM x member this.ReturnFrom(x) = x member this.Delay(f) = f()