{-# LANGUAGE FlexibleContexts #-}
module Simulation.Aivika.Trans.Vector
(Vector,
newVector,
copyVector,
vectorCount,
appendVector,
readVector,
writeVector,
vectorBinarySearch,
vectorInsert,
vectorDeleteAt,
vectorDeleteRange,
vectorDelete,
vectorDeleteBy,
vectorIndex,
vectorIndexBy,
vectorContains,
vectorContainsBy,
freezeVector) where
import Data.Array
import Control.Monad
import Simulation.Aivika.Trans.Simulation
import Simulation.Aivika.Trans.Event
import Simulation.Aivika.Trans.Ref.Base.Lazy
data Vector m a = Vector { Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef :: Ref m (Array Int (Ref m a)),
Vector m a -> Ref m Int
vectorCountRef :: Ref m Int,
Vector m a -> Ref m Int
vectorCapacityRef :: Ref m Int }
newVector :: MonadRef m => Simulation m (Vector m a)
{-# INLINABLE newVector #-}
newVector :: Simulation m (Vector m a)
newVector =
do [Ref m a]
xs <- [Integer]
-> (Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Integer
0 .. Integer
4 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1] ((Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a])
-> (Integer -> Simulation m (Ref m a)) -> Simulation m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Integer
i -> a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
forall a. HasCallStack => a
undefined
let arr :: Array Int (Ref m a)
arr = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs
Ref m (Array Int (Ref m a))
arrRef <- Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a)))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a))))
-> Array Int (Ref m a)
-> Simulation m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr
Ref m Int
countRef <- Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
0
Ref m Int
capacityRef <- Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
4
Vector m a -> Simulation m (Vector m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall (m :: * -> *) a.
Ref m (Array Int (Ref m a)) -> Ref m Int -> Ref m Int -> Vector m a
Vector { vectorArrayRef :: Ref m (Array Int (Ref m a))
vectorArrayRef = Ref m (Array Int (Ref m a))
arrRef,
vectorCountRef :: Ref m Int
vectorCountRef = Ref m Int
countRef,
vectorCapacityRef :: Ref m Int
vectorCapacityRef = Ref m Int
capacityRef }
copyVector :: MonadRef m => Vector m a -> Event m (Vector m a)
{-# INLINABLE copyVector #-}
copyVector :: Vector m a -> Event m (Vector m a)
copyVector Vector m a
vector =
do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
[Ref m a]
xs' <-
[Int] -> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m (Ref m a)) -> Event m [Ref m a])
-> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
Simulation m (Ref m a) -> Event m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m a) -> Event m (Ref m a))
-> Simulation m (Ref m a) -> Event m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
x
let arr' :: Array Int (Ref m a)
arr' = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs'
Ref m (Array Int (Ref m a))
arrRef' <- Simulation m (Ref m (Array Int (Ref m a)))
-> Event m (Ref m (Array Int (Ref m a)))
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m (Array Int (Ref m a)))
-> Event m (Ref m (Array Int (Ref m a))))
-> Simulation m (Ref m (Array Int (Ref m a)))
-> Event m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$ Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a)))
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Array Int (Ref m a) -> Simulation m (Ref m (Array Int (Ref m a))))
-> Array Int (Ref m a)
-> Simulation m (Ref m (Array Int (Ref m a)))
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr'
Ref m Int
countRef' <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
count
Ref m Int
capacityRef' <- Simulation m (Ref m Int) -> Event m (Ref m Int)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m Int) -> Event m (Ref m Int))
-> Simulation m (Ref m Int) -> Event m (Ref m Int)
forall a b. (a -> b) -> a -> b
$ Int -> Simulation m (Ref m Int)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef (Int -> Simulation m (Ref m Int))
-> Int -> Simulation m (Ref m Int)
forall a b. (a -> b) -> a -> b
$! Int
count
Vector m a -> Event m (Vector m a)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector :: forall (m :: * -> *) a.
Ref m (Array Int (Ref m a)) -> Ref m Int -> Ref m Int -> Vector m a
Vector { vectorArrayRef :: Ref m (Array Int (Ref m a))
vectorArrayRef = Ref m (Array Int (Ref m a))
arrRef',
vectorCountRef :: Ref m Int
vectorCountRef = Ref m Int
countRef',
vectorCapacityRef :: Ref m Int
vectorCapacityRef = Ref m Int
capacityRef' }
vectorEnsureCapacity :: MonadRef m => Vector m a -> Int -> Event m ()
{-# INLINABLE vectorEnsureCapacity #-}
vectorEnsureCapacity :: Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector Int
capacity =
do Int
capacity' <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCapacityRef Vector m a
vector)
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
capacity' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
capacity) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
do Array Int (Ref m a)
arr' <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Int
count' <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
let capacity'' :: Int
capacity'' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
capacity') Int
capacity
[Ref m a]
xs'' <-
[Int] -> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
capacity'' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m (Ref m a)) -> Event m [Ref m a])
-> (Int -> Event m (Ref m a)) -> Event m [Ref m a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
Simulation m (Ref m a) -> Event m (Ref m a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
SimulationLift t m =>
Simulation m a -> t m a
liftSimulation (Simulation m (Ref m a) -> Event m (Ref m a))
-> Simulation m (Ref m a) -> Event m (Ref m a)
forall a b. (a -> b) -> a -> b
$ a -> Simulation m (Ref m a)
forall (m :: * -> *) a. MonadRef m => a -> Simulation m (Ref m a)
newRef a
forall a. HasCallStack => a
undefined
let arr'' :: Array Int (Ref m a)
arr'' = (Int, Int) -> [(Int, Ref m a)] -> Array Int (Ref m a)
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
capacity'' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, Ref m a)] -> Array Int (Ref m a))
-> [(Int, Ref m a)] -> Array Int (Ref m a)
forall a b. (a -> b) -> a -> b
$ [Int] -> [Ref m a] -> [(Int, Ref m a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Ref m a]
xs''
[Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
count' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr' Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr'' Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
Ref m (Array Int (Ref m a)) -> Array Int (Ref m a) -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector) (Array Int (Ref m a) -> Event m ())
-> Array Int (Ref m a) -> Event m ()
forall a b. (a -> b) -> a -> b
$! Array Int (Ref m a)
arr''
Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCapacityRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! Int
capacity''
vectorCount :: MonadRef m => Vector m a -> Event m Int
{-# INLINABLE vectorCount #-}
vectorCount :: Vector m a -> Event m Int
vectorCount Vector m a
vector = Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
appendVector :: MonadRef m => Vector m a -> a -> Event m ()
{-# INLINABLE appendVector #-}
appendVector :: Vector m a -> a -> Event m ()
appendVector Vector m a
vector a
item =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
count) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item
Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
readVector :: MonadRef m => Vector m a -> Int -> Event m a
{-# INLINABLE readVector #-}
readVector :: Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index =
do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
writeVector :: MonadRef m => Vector m a -> Int -> a -> Event m ()
{-# INLINABLE writeVector #-}
writeVector :: Vector m a -> Int -> a -> Event m ()
writeVector Vector m a
vector Int
index a
item =
do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item
vectorBinarySearch' :: (MonadRef m, Ord a) => Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
{-# INLINABLE vectorBinarySearch' #-}
vectorBinarySearch' :: Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
left Int
right =
if Int
left Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
right
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ - (Int
right Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
else
do let index :: Int
index = (Int
left Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
right) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
a
curr <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
if a
item a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
curr
then Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
left (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
else if a
item a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
curr
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
right
vectorBinarySearch :: (MonadRef m, Ord a) => Vector m a -> a -> Event m Int
{-# INLINABLE vectorBinarySearch #-}
vectorBinarySearch :: Vector m a -> a -> Event m Int
vectorBinarySearch Vector m a
vector a
item =
do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Ord a) =>
Array Int (Ref m a) -> a -> Int -> Int -> Event m Int
vectorBinarySearch' Array Int (Ref m a)
arr a
item Int
0 (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
freezeVector :: MonadRef m => Vector m a -> Event m (Array Int a)
{-# INLINABLE freezeVector #-}
freezeVector :: Vector m a -> Event m (Array Int a)
freezeVector Vector m a
vector =
do Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
[a]
xs' <-
[Int] -> (Int -> Event m a) -> Event m [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m a) -> Event m [a])
-> (Int -> Event m a) -> Event m [a]
forall a b. (a -> b) -> a -> b
$ \Int
i ->
Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i)
let arr' :: Array Int a
arr' = (Int, Int) -> [(Int, a)] -> Array Int a
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (Int
0, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) ([(Int, a)] -> Array Int a) -> [(Int, a)] -> Array Int a
forall a b. (a -> b) -> a -> b
$ [Int] -> [a] -> [(Int, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [a]
xs'
Array Int a -> Event m (Array Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Array Int a
arr'
vectorInsert :: MonadRef m => Vector m a -> Int -> a -> Event m ()
{-# INLINABLE vectorInsert #-}
vectorInsert :: Vector m a -> Int -> a -> Event m ()
vectorInsert Vector m a
vector Int
index a
item =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorInsert."
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be greater " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorInsert."
Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorEnsureCapacity Vector m a
vector (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
[Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
count, Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index) (a -> Event m ()) -> a -> Event m ()
forall a b. (a -> b) -> a -> b
$! a
item
Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
vectorDeleteAt :: MonadRef m => Vector m a -> Int -> Event m ()
{-# INLINABLE vectorDeleteAt #-}
vectorDeleteAt :: Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorDeleteAt."
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"Index must be less " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorDeleteAt."
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
[Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1))
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) a
forall a. HasCallStack => a
undefined
Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
vectorDeleteRange :: MonadRef m
=> Vector m a
-> Int
-> Int
-> Event m ()
{-# INLINABLE vectorDeleteRange #-}
vectorDeleteRange :: Vector m a -> Int -> Int -> Event m ()
vectorDeleteRange Vector m a
vector Int
index Int
len =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"The first index cannot be " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"negative: vectorDeleteRange."
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error ([Char] -> Event m ()) -> [Char] -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char]
"The last index must be less " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++
[Char]
"than the count: vectorDeleteRange."
Bool -> Event m () -> Event m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0) (Event m () -> Event m ()) -> Event m () -> Event m ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Event m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"Negative range length: vectorDeleteRange."
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
[Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
index, Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 .. (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len) Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
len))
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
x
[Int] -> (Int -> Event m ()) -> Event m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len) .. Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> Event m ()) -> Event m ())
-> (Int -> Event m ()) -> Event m ()
forall a b. (a -> b) -> a -> b
$ \Int
i ->
Ref m a -> a -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
i) a
forall a. HasCallStack => a
undefined
Ref m Int -> Int -> Event m ()
forall (m :: * -> *) a. MonadRef m => Ref m a -> a -> Event m ()
writeRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector) (Int -> Event m ()) -> Int -> Event m ()
forall a b. (a -> b) -> a -> b
$! (Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
len)
vectorIndex :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Int
{-# INLINABLE vectorIndex #-}
vectorIndex :: Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
let loop :: Int -> Event m Int
loop Int
index =
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
if a
item a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> Event m Int
loop (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Int -> Event m Int
loop Int
0
vectorIndexBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m Int
{-# INLINABLE vectorIndexBy #-}
vectorIndexBy :: Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred =
do Int
count <- Ref m Int -> Event m Int
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m Int
forall (m :: * -> *) a. Vector m a -> Ref m Int
vectorCountRef Vector m a
vector)
Array Int (Ref m a)
arr <- Ref m (Array Int (Ref m a)) -> Event m (Array Int (Ref m a))
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Vector m a -> Ref m (Array Int (Ref m a))
forall (m :: * -> *) a. Vector m a -> Ref m (Array Int (Ref m a))
vectorArrayRef Vector m a
vector)
let loop :: Int -> Event m Int
loop Int
index =
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
count
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ -Int
1
else do a
x <- Ref m a -> Event m a
forall (m :: * -> *) a. MonadRef m => Ref m a -> Event m a
readRef (Array Int (Ref m a)
arr Array Int (Ref m a) -> Int -> Ref m a
forall i e. Ix i => Array i e -> i -> e
! Int
index)
if a -> Bool
pred a
x
then Int -> Event m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
index
else Int -> Event m Int
loop (Int -> Event m Int) -> Int -> Event m Int
forall a b. (a -> b) -> a -> b
$ Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Int -> Event m Int
loop Int
0
vectorDelete :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Bool
{-# INLINABLE vectorDelete #-}
vectorDelete :: Vector m a -> a -> Event m Bool
vectorDelete Vector m a
vector a
item =
do Int
index <- Vector m a -> a -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Eq a) =>
Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index
Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
vectorDeleteBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m (Maybe a)
{-# INLINABLE vectorDeleteBy #-}
vectorDeleteBy :: Vector m a -> (a -> Bool) -> Event m (Maybe a)
vectorDeleteBy Vector m a
vector a -> Bool
pred =
do Int
index <- Vector m a -> (a -> Bool) -> Event m Int
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do a
a <- Vector m a -> Int -> Event m a
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index
Vector m a -> Int -> Event m ()
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m ()
vectorDeleteAt Vector m a
vector Int
index
Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
else Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
vectorContains :: (MonadRef m, Eq a) => Vector m a -> a -> Event m Bool
{-# INLINABLE vectorContains #-}
vectorContains :: Vector m a -> a -> Event m Bool
vectorContains Vector m a
vector a
item =
do Int
index <- Vector m a -> a -> Event m Int
forall (m :: * -> *) a.
(MonadRef m, Eq a) =>
Vector m a -> a -> Event m Int
vectorIndex Vector m a
vector a
item
Bool -> Event m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0)
vectorContainsBy :: MonadRef m => Vector m a -> (a -> Bool) -> Event m (Maybe a)
{-# INLINABLE vectorContainsBy #-}
vectorContainsBy :: Vector m a -> (a -> Bool) -> Event m (Maybe a)
vectorContainsBy Vector m a
vector a -> Bool
pred =
do Int
index <- Vector m a -> (a -> Bool) -> Event m Int
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> (a -> Bool) -> Event m Int
vectorIndexBy Vector m a
vector a -> Bool
pred
if Int
index Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
then do a
a <- Vector m a -> Int -> Event m a
forall (m :: * -> *) a.
MonadRef m =>
Vector m a -> Int -> Event m a
readVector Vector m a
vector Int
index
Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
a)
else Maybe a -> Event m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing