From c210311be639e8774dca25a332542d6b64ce3ca3 Mon Sep 17 00:00:00 2001 From: Tracy Wadleigh Date: Fri, 24 Apr 2009 13:58:33 +0000 Subject: More defined list-to-V/M operators: (|>|), (>|<). Provides analogs of the (|>) and (><) operators that first apply an appropriate 'take' to the given lists so that they may be safely used on lists that are too long (or infinite) -- a feature I find particularly useful from the interactive prompt. As these operators are more defined, I would ask the package maintainer to consider, rather than adding (|>|) and (>|<), just updating the definitions of (|>) and (><) with the new semantics. --- lib/Data/Packed/Internal/Vector.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/Data/Packed/Internal/Vector.hs') diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index d410c4d..0e6b3f4 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -96,6 +96,19 @@ toList v = safeRead v $ peekArray (dim v) infixl 9 |> n |> l = if length l == n then fromList l else error "|> with wrong size" +{- | Like '(|>)', but explicitly truncates the list, if it is too long. + +It may safely be used, for instance, with infinite lists. +-} +(|>|) :: (Storable a) => Int -> [a] -> Vector a +infixl 9 |>| +n |>| l = if length l' == n then + fromList l' + else + error "|>|: list too short" + where l' = take n l + + -- | access to Vector elements without range checking at' :: Storable a => Vector a -> Int -> a at' v n = safeRead v $ flip peekElemOff n -- cgit v1.2.3