diff options
author | Tracy Wadleigh <tracy.wadleigh@gmail.com> | 2009-04-24 13:58:33 +0000 |
---|---|---|
committer | Tracy Wadleigh <tracy.wadleigh@gmail.com> | 2009-04-24 13:58:33 +0000 |
commit | c210311be639e8774dca25a332542d6b64ce3ca3 (patch) | |
tree | 032457af80ff755d97a3e7e1922ceaf5eaf2aa62 /lib/Data/Packed/Internal | |
parent | 3ccfccc82aa1cf374af1b822087fb6c4fd41b3c3 (diff) |
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.
Diffstat (limited to 'lib/Data/Packed/Internal')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 13 |
1 files changed, 13 insertions, 0 deletions
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) | |||
96 | infixl 9 |> | 96 | infixl 9 |> |
97 | n |> l = if length l == n then fromList l else error "|> with wrong size" | 97 | n |> l = if length l == n then fromList l else error "|> with wrong size" |
98 | 98 | ||
99 | {- | Like '(|>)', but explicitly truncates the list, if it is too long. | ||
100 | |||
101 | It may safely be used, for instance, with infinite lists. | ||
102 | -} | ||
103 | (|>|) :: (Storable a) => Int -> [a] -> Vector a | ||
104 | infixl 9 |>| | ||
105 | n |>| l = if length l' == n then | ||
106 | fromList l' | ||
107 | else | ||
108 | error "|>|: list too short" | ||
109 | where l' = take n l | ||
110 | |||
111 | |||
99 | -- | access to Vector elements without range checking | 112 | -- | access to Vector elements without range checking |
100 | at' :: Storable a => Vector a -> Int -> a | 113 | at' :: Storable a => Vector a -> Int -> a |
101 | at' v n = safeRead v $ flip peekElemOff n | 114 | at' v n = safeRead v $ flip peekElemOff n |