将元组 2 的事件流解压缩为两个流
Unzip an event stream of tuple 2 into two streams
在 reactive-banana 中,给定 Event t (a, b)
,你将如何引导它达到 (Event t a, Event t b)
?
Traversable#sequence
似乎可以通过提供的 (,)
的 Monad
的一些实例来解决它,但是 Event t
只是 Functor
.
这应该有效:
import Control.Applicative
unzipEvent :: Event t (a, b) -> (Event t a, Event t b)
unzipEvent = liftA2 (,) (fmap fst) (fmap snd)
备注:
liftA2 (,)
是通用的,因为它仅使用函数的 Applicative
实例。在这种情况下,它的等效替代方案是 Control.Arrow
中的 (&&&)
。还有一个不那么花哨的选择,\e -> (fst <$> e, snd <$> e)
.
- 即使
Event t
是 Traversable
,sequenceA
也无济于事。对 are parametrised only on the second component of the pair 的 Functor
和 Applicative
实例。您最终会得到 (a, Event t b)
结果类型,更不用说对 a
. 的 Monoid
约束了
在 reactive-banana 中,给定 Event t (a, b)
,你将如何引导它达到 (Event t a, Event t b)
?
Traversable#sequence
似乎可以通过提供的 (,)
的 Monad
的一些实例来解决它,但是 Event t
只是 Functor
.
这应该有效:
import Control.Applicative
unzipEvent :: Event t (a, b) -> (Event t a, Event t b)
unzipEvent = liftA2 (,) (fmap fst) (fmap snd)
备注:
liftA2 (,)
是通用的,因为它仅使用函数的Applicative
实例。在这种情况下,它的等效替代方案是Control.Arrow
中的(&&&)
。还有一个不那么花哨的选择,\e -> (fst <$> e, snd <$> e)
.- 即使
Event t
是Traversable
,sequenceA
也无济于事。对 are parametrised only on the second component of the pair 的Functor
和Applicative
实例。您最终会得到(a, Event t b)
结果类型,更不用说对a
. 的
Monoid
约束了