1. Aleksey Khudyakov
  2. fixed-vector

Commits

Aleksey Khudyakov  committed 35299af

Add two variants of fromList

  • Participants
  • Parent commits c7eb619
  • Branches default

Comments (0)

Files changed (2)

File Data/Vector/Fixed/Cont.hs

View file
  • Ignore whitespace
   , cvec
   , empty
   , fromList
+  , fromList'
+  , fromListM
   , replicate
   , replicateM
   , generate
     step (T_flist []    ) = error "Data.Vector.Fixed.Cont.fromList: too few elements"
     step (T_flist (a:as)) = (a, T_flist as)
 
+-- | Same as 'fromList' bu throws error is list doesn't have same
+--   length as vector.
+fromList' :: forall m n a. Arity n => [a] -> ContVecT m n a
+{-# INLINE fromList' #-}
+fromList' xs = ContVecT $ \(Fun fun) ->
+  let (r,rest) = applyFun step (T_flist xs :: T_flist a n) fun
+      step (T_flist []    ) = error "Data.Vector.Fixed.Cont.fromList': too few elements"
+      step (T_flist (a:as)) = (a, T_flist as)
+  in case rest of
+       T_flist [] -> r
+       _          -> error "Data.Vector.Fixed.Cont.fromList': too many elements"
+
+-- | Convert list to continuation-based vector. Will fail with
+--   'Nothing' if list doesn't have right length.
+fromListM :: forall n a. Arity n => [a] -> ContVecT Maybe n a
+fromListM xs = ContVecT $ \(Fun fun) -> do
+  (r,rest) <- applyFunM step (T_flist xs :: T_flist a n) fun
+  case rest of
+    T_flist [] -> return r
+    _          -> Nothing
+  where
+    step (T_flist []    ) = Nothing
+    step (T_flist (a:as)) = return (a, T_flist as)
+
+
 data T_flist a n = T_flist [a]
 
 
+
 -- | Execute monadic action for every element of vector. Synonym for 'pure'.
 replicate :: forall m n a. (Arity n)
           => a -> ContVecT m n a

File Data/Vector/Fixed/Internal.hs

View file
  • Ignore whitespace
 fromList :: (Vector v a) => [a] -> v a
 {-# INLINE fromList #-}
 fromList = C.vector . C.fromList
+
+-- | Create vector form list. Will throw error if list has different
+--   length from resulting vector.
+fromList' :: (Vector v a) => [a] -> v a
+{-# INLINE fromList' #-}
+fromList' = C.vector . C.fromList'
+
+-- | Create vector form list. Will return 'Nothing' if list has different
+--   length from resulting vector.
+fromListM :: (Vector v a) => [a] -> Maybe (v a)
+{-# INLINE fromListM #-}
+fromListM = C.vectorM . C.fromListM