maybe

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

Simplifying nested Maybe pattern matching

余生长醉 提交于 2020-01-13 05:21:08
问题 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"? 回答1: UPDATED 2 Monad Either is for you import Data.Maybe (maybe) maybeE

Why do Maybe/Optional types use a Just/Some type instead of the actual type?

↘锁芯ラ 提交于 2020-01-10 03:47:25
问题 In Idris, the Maybe type is defined as followed: data Maybe a = Just a | Nothing It's defined similarly in Haskell: data Maybe a = Just a | Nothing deriving (Eq, Ord) Here's the ML version: datatype 'a option = NONE | SOME of 'a What are the benefits of using Just and Some ? Why not define the type without them? example: data Maybe a = a | Nothing 回答1: What would then be the difference between Maybe a and Maybe (Maybe a) ? There's supposed to be a difference between Nothing and Just Nothing .

Maybe Task not supported on outbound ports?

拟墨画扇 提交于 2020-01-06 04:13:51
问题 I seem to be getting this error Trying to send an unsupported type through outbound port `projectRequests` port projectRequests : Signal (Maybe (Task String ())) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The specific unsupported type is: Task.Task String () The types of values that can flow through outbound ports include: Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, Tuples, Json.Values, and concrete records. However this seems to be fine port orgRequests : Signal (Task

Real World Haskell Chapter 3 excercise: Binary Tree with 1 value constructor - follow up

倖福魔咒の 提交于 2020-01-04 02:44:05
问题 This question is not a duplicate A question with the same title already exists, but the answer only partially addressed it, in my opinion, and I am interested also in what it left unaswered. Foreword Real World Haskell proposes, in Chapter 3, page 58, the following definition for a binary tree datatype, data Tree a = Node a (Tree a) (Tree a) | Empty deriving (Show) which provides two constructors (for empty and non-empty Tree s). On the other hand, at page 60, an exercise challenges the

apply function on Maybe types?

我只是一个虾纸丫 提交于 2020-01-03 14:56:26
问题 New to Haskell and I can't figure out how apply a function (a -> b) into a list [Maybe a] and get [Maybe b] maybeX:: (a -> b) -> [Maybe a] -> [Maybe b] The function is supposed to do the exact same thing as map, apply the function f on a list of Maybe statements and if it Just it returns me a f Just and if it's a Nothing just a Nothing. Like the following example I want to add +5 on every Element of the following List : [Just 1,Just 2,Nothing,Just 3] and get [Just 6,Just 7,Nothing,Just 8]

Does this Bool-producer to Maybe-producer function appear in any common library?

别说谁变了你拦得住时间么 提交于 2019-12-24 11:34:21
问题 I found myself wanting this tiny little function, but it doesn't seem to be in Data.Maybe . Is it somewhere else? splat :: (a -> Bool) -> a -> Maybe a splat c a | c a = Just a | otherwise = Nothing 回答1: The package monadplus contains exactly this function, named partial: partial :: (a -> Bool) -> a -> Maybe a 回答2: splat :: MonadPlus m => (a -> Bool) -> a -> m a splat c x = guard (c x) >> return x would be a shorter, more general definition, if you decided you want this. But just using guard

F# computation expression for nested Boolean tests?

为君一笑 提交于 2019-12-23 19:12:59
问题 I think I've got enough understanding of F# monads (workflows) that I see a few places in my code where implementing them makes sense. For example, I've got a function with multiple nested if/thens, i.e. the function should continue only so long as the data pass certain "tests" along the way. I'm familiar with the "maybe" monad, but in all the examples that I've seen, it's coded to operate on let! bindings, which I'm not doing. I'm hoping that someone can provide me with an example of the

Constructing minimal Haskell example on error-handling in the State Monad

我是研究僧i 提交于 2019-12-23 13:16:14
问题 I'm twisting my brain into knots trying to understand how to combine the State monad with Maybe . Let's start with a concrete (and intentionally trivial/unnecessary) example in which we use a State monad to find the sum of a list of numbers: import Control.Monad.State list :: [Int] list = [1,4,5,6,7,0,3,2,1] adder :: Int adder = evalState addState list addState :: State [Int] Int addState = do ms <- get case ms of [] -> return 0 (x:xs) -> put xs >> fmap (+x) addState Cool. Now let's modify it

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

匆匆过客 提交于 2019-12-22 05:18:05
问题 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 ->