从包含“Just”的Maybe返回操作。 [英] Operating on a return from a Maybe that contains "Just"
问题描述
我有一个函数返回类型为 Maybe([(Int,Int)],(Int,Int))
我想从另一个函数调用它并对数据执行操作。
但是,返回值包含在只是
。第二种方法使用([(Int,Int)],(Int,Int))
,因此不会接受 Just([(Int,Int )],(Int,Int))
。
有没有办法修剪 Just
在应用第二个方法之前?
我不完全理解在
Maybe
- 但是,我被告知第一个Method的返回类型必须是 Maybe
。
您的问题有几种解决方案,都基于模式匹配。我假设你有两种算法(因为你没有命名它们,我会这样做):
algorithm1 :: a - >也许b
算法2 :: b - > c
input :: a
1)模式匹配通常是从一个case语句(下面)或一个函数中完成。
let val = algorithm1输入
,如果val为
Nothing - > defaultValue
只需x - >算法2 x
所有其他解决方案都使用模式匹配,我只是提供执行模式的标准函数匹配你。
2)前奏(和Data.Maybe)有一些内置函数来处理也许
秒。 也许功能是一个伟大的,我建议你使用它。它在标准库中定义为:
也许:: c - > (b→c)→>也许b - > c
也许n _什么= n
也许_f(只要x)= fx
您的代码如下所示:
也许是defaultValue算法2(算法1输入)
<3>由于Maybe是 functor 你可以使用 fmap 。如果你没有默认值,这会更有意义。定义:
$ b $ pre $ $ $ c $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' = Just(fa)
所以你的代码看起来像:
fmap algorithm2(算法1输入)
如果algorithm1的结果是 Nothing ,则此输出将为
可能
值( Nothing
code $)。
4)最后,强烈不鼓励,是 fromJust
。只有在积极时才使用它,第一个算法将返回 Just x
(而不是 Nothing
)。小心!如果你在 val = Nothing
时从,那么你会得到一个异常,这在Haskell中是不被理解的。它的定义:
fromJust :: Maybe b - > b
fromJust Nothing = errorMaybe.fromJust:Nothing - yuck
fromJust(just x)= x
让代码看起来像:
algorithm2(fromJust(algorithm1 input))
I have a function that has a return type of Maybe ([(Int,Int)],(Int,Int))
I would like to call this from another function and perform an operation on the data.
However, the return value is contained within Just
. The second method takes ([(Int,Int)],(Int,Int))
and therefore will not accept Just ([(Int,Int)],(Int,Int))
.
Is there a way I can trim the Just
before applying the second method?
I don't fully understand the use of Just
within Maybe
- however, I have been told that the return type for the first Method must be Maybe
.
There are several solutions to your problem, all based around pattern matching. I'm assuming you have two algorithms (since you didn't name them, I will):
algorithm1 :: a -> Maybe b
algorithm2 :: b -> c
input :: a
1) Pattern matching is typically done from either a case statement (below) or a function.
let val = algorithm1 input
in case val of
Nothing -> defaultValue
Just x -> algorithm2 x
All other presented solutions use pattern matching, I'm just presenting standard functions that perform the pattern matching for you.
2) The prelude (and Data.Maybe) have some built-in functions to deal with Maybe
s. The maybe function is a great one, I suggest you use it. It's defined in standard libraries as:
maybe :: c -> (b -> c) -> Maybe b -> c
maybe n _ Nothing = n
maybe _ f (Just x) = f x
Your code would look like:
maybe defaultValue algorithm2 (algorithm1 input)
3) Since Maybe is a functor you could use fmap. This makes more sense if you don't have a default value. The definition:
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
So your code would look like:
fmap algorithm2 (algorithm1 input)
This output will be a Maybe
value (Nothing
if the result of algorithm1 is Nothing
).
4) Finally, and strongly discouraged, is fromJust
. Only use it if you are positive the first algorithm will return Just x
(and not Nothing
). Be careful! If you call fromJust val
when val = Nothing
then you get an exception, which is not appreciated in Haskell. Its definition:
fromJust :: Maybe b -> b
fromJust Nothing = error "Maybe.fromJust: Nothing" -- yuck
fromJust (Just x) = x
Leaving your code to look like:
algorithm2 (fromJust (algorithm1 input))
这篇关于从包含“Just”的Maybe返回操作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!