sum -package:rio
The
sum function computes the sum of the numbers of a
structure.
Examples
Basic usage:
>>> sum []
0
>>> sum [42]
42
>>> sum [1..10]
55
>>> sum [4.1, 2.0, 1.7]
7.8
>>> sum [1..]
* Hangs forever *
The
sum function computes the sum of a finite list of numbers.
>>> sum []
0
>>> sum [42]
42
>>> sum [1..10]
55
>>> sum [4.1, 2.0, 1.7]
7.8
>>> sum [1..]
* Hangs forever *
Get the sum of all values in the stream.
Subject to fusion
Compute the sum of the elements of a
Producer
Computes the sum of all elements
Stricter version of
sum.
>>> sum [1..10]
55
The sum of elements in the original Sample. This is the
sample's first simple power.
Fold a
Stream of numbers into their sum with the return value
mapped S.sum :: Stream (Stream (Of Int)) m r -> Stream (Of Int) m r
>>> S.sum $ each [1..10]
55 :> ()
>>> (n :> rest) <- S.sum $ S.splitAt 3 $ each [1..10]
>>> System.IO.print n
6
>>> (m :> rest') <- S.sum $ S.splitAt 3 rest
>>> System.IO.print m
15
>>> S.print rest'
7
8
9
10
sum does not need a zero for initialization
Sum up all elements of a list. An empty list yields zero.
This function is inappropriate for number types like Peano. Maybe we
should make
sum a method of Additive. This would also make
lengthLeft and
lengthRight superfluous.
Sum a collection of values.
Example:
foo = sum kbn [1,2,3]
Determine the sum of all elements of a stream of numbers. Returns
0 when the stream is empty. Note that this is not numerically
stable for floating point numbers.
sum = Stream.fold Fold.sum
Compute the sum of a finite list of numbers.
\(Array16 xs) -> Array.sum xs == sum (Array.toList xs)
Stricter version of
sum.
>>> sum [1..10]
55
>>> sum (Just 3)
...
• Do not use 'Foldable' methods on Maybe
Suggestions:
Instead of
for_ :: (Foldable t, Applicative f) => t a -> (a -> f b) -> f ()
use
whenJust :: Applicative f => Maybe a -> (a -> f ()) -> f ()
whenRight :: Applicative f => Either l r -> (r -> f ()) -> f ()
...
Instead of
fold :: (Foldable t, Monoid m) => t m -> m
use
maybeToMonoid :: Monoid m => Maybe m -> m
...
O(n) Compute the sum of the elements.
O(n) Compute the sum of the elements.