选项的功能组成
我有两个我想组合的选项(或可能的对象),以便得到以下结果:
|| first operand
second ++-------------+-------------
operand || empty | optional(x)
============||=============|=============
empty || empty | optional(x)
------------++-------------+-------------
optional(y) || optional(y) |optional(x+y)
换句话说,非空的可选始终替换/覆盖一个空的一个空,并且根据两个非空选项组合。一些+
功能。
最初,我假设标准的Monadic flatmap
方法可以解决问题,但是(至少在Java中)空(我不确定是否有其他实施符合Monad法律)。
然后,由于这两个操作数都以相同的单声道类型包裹,所以我认为这对于应用函数可能是一项好工作。我尝试了几个不同的功能库,但是我无法使用我尝试过的任何zip
/ap
方法来实现所需的行为。
在我看来,我想做的事情似乎是一个相当普遍的操作,它可能会使用选项进行,我意识到我只能用所需的行为编写自己的操作员。尽管如此,我想知道功能编程中是否有标准函数/方法可以实现此通用操作?
更新:我删除了java
标签,因为我很好奇其他语言如何处理这种情况
I have 2 Optionals (or Maybe objects) that I would like to combine so that I get the following results:
|| first operand
second ++-------------+-------------
operand || empty | optional(x)
============||=============|=============
empty || empty | optional(x)
------------++-------------+-------------
optional(y) || optional(y) |optional(x+y)
In other words, a non-empty Optional always replaces/overwrites an empty one, and two non-empty Optionals are combined according to some +
function.
Initially, I assumed that the standard monadic flatMap
method would do the trick, but (at least in Java) Optional.flatMap
always returns an empty optional when the original Optional was already empty (and I'm not sure if any other implementation would comply with the Monad Laws).
Then, as both operands are wrapped in the same monadic type, I figured that this might be a good job for an Applicative Functor. I tried a couple different functional libraries, but I couldn't implement the desired behavior with any of the zip
/ap
methods that I tried.
What I'm trying to do seems to me a fairly common operation that one might do with Optionals, and I realize that I could just write my own operator with the desired behavior. Still, I am wondering if there is a standard function/method in functional programming to achieve this common operation?
Update: I removed the java
tag, as I'm curious how other languages handle this situation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在功能性语言中,您将使用模式匹配来执行此操作,例如(Haskell):
还有其他写作方法,但是您基本上是在情况下匹配的。请注意,这仍然涉及“解开”选项,但是由于它内置在语言中,因此不太明显。
In a functional language, you'd do this with pattern matching, such as (Haskell):
There are other ways to write it, but you are basically pattern matching on the cases. Note that this still involves "unpacking" the optionals, but because its built into the language, it is less obvious.
在Haskell中,您可以通过包装任何可能的semigroup。具体而言,如果您想将数字添加在一起:
上面的链接文章包含更多解释,以及一些C#示例。
In Haskell you can do this by wrapping any semigroup in a Maybe. Specifically, if you want to add numbers together:
The above linked article contains more explanations, and also some C# examples.
这是一种方法:
ideone demo
Here's one way:
Ideone Demo
不可能在不“解开”它们的情况下组合可选对象。
我不知道您案件的细节。对我来说,创建这样的逻辑只是为了融合这两个选项是过度杀伤。
但是,尽管如此,流也有一个解决方案。
我认为您不会将可选对象作为参数(因为不建议这样做)。因此,有两种虚拟方法返回
可选< t>
。方法
combine()
期望二进制操作器< t>
作为参数,并通过串联从getx(getx(getx)返回的每个可选对象中产生的singleton-streams创建流( )
和gety()
。redair(binaryOperator)
将产生可选的结果。如果我们将问题推广到“如何组合
n
可选对象” ,则可以这样解决:It's not possible to combine optional objects without "unpacking" them.
I don't know the specifics of your case. For me, creating such a logic just in order to fuse the two optionals is an overkill.
But nevertheless, there's a possible solution with streams.
I assume that you're not going to pass optional objects as arguments (because such practice is discouraged). Therefore, there are two dummy methods returning
Optional<T>
.Method
combine()
expects aBinaryOperator<T>
as an argument and creates a stream by concatenating singleton-streams produced from each of the optional objects returned bygetX()
andgetY()
.The flavor of
reduce(BinaryOperator)
will produce an optional result.If we generalize the problem to "how to combine
N
optional objects" then it can be solved like this:由于Java 9,您可以将可选的流变成流。
使用Concat,您将获得0、1或2个元素的流。
将其简化为
empty
在0个元素时将其添加到上一个可选的intionalInt,默认为0。由于需要而不是很直(
.sum()
)对于空()
。Since java 9 you can turn an Optional into a Stream.
With concat you get a Stream of 0, 1 or 2 elements.
Reduce it to an
empty
when 0 elements,and for more add it to the previous OptionalInt, defaulting to 0.Not very straight (
.sum()
) because of the need for anempty()
.您可以通过组合
flatmap
和map
:更通用的示例:
由于Flatmap和Map是可选/也许(通常(通常是单案类型)),因此方法应使用任何其他语言(尽管大多数FP语言都具有更简洁的解决方案)。例如在Haskell中:
You can implement your function in Java by combining
flatMap
andmap
:More general example:
Since flatMap and map are standard functions for Optional/Maybe (and monad types generally), this approach should work in any other language (though most FP languages will have a more concise solution). E.g. in Haskell:
在f#中,我称此逻辑
redail
。原因:
该函数必须是类型
'a - &gt; 'a - &gt; 'a
,因为它只能结合相等类型的思想。类似于其他
降低
操作,例如在列表上,您始终需要至少一个值,否则会失败。使用
选项
和其中两个,您只需要介绍四个情况即可。在f#中,它将以这种方式写。另一个可以说,像伪式的C#语言一样,看起来像。
In F#, i would call this logic
reduce
.Reason:
The function must be of type
'a -> 'a -> 'a
as it only can combine thinks of equal type.Like other
reduce
operations, like on list, you always need at least one value, otherwise it fails.With a
option
and two of them, you just need to cover four cases. In F# it will be written this way.In another lets say Pseudo-like C# language, it would look like.