/usr/share/doc/libghc-foldl-doc/html/foldl.txt is in libghc-foldl-doc 1.1.2-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | -- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Composable, streaming, and efficient left folds
--
-- This library provides strict left folds that stream in constant
-- memory, and you can combine folds using <tt>Applicative</tt> style to
-- derive new folds. Derived folds still traverse the container just once
-- and are often as efficient as hand-written folds.
@package foldl
@version 1.1.2
-- | This module provides efficient and streaming left folds that you can
-- combine using <a>Applicative</a> style.
--
-- Import this module qualified to avoid clashing with the Prelude:
--
-- <pre>
-- >>> import qualified Control.Foldl as L
-- </pre>
--
-- Use <a>fold</a> to apply a <a>Fold</a> to a list:
--
-- <pre>
-- >>> L.fold L.sum [1..100]
-- 5050
-- </pre>
--
-- <a>Fold</a>s are <a>Applicative</a>s, so you can combine them using
-- <a>Applicative</a> combinators:
--
-- <pre>
-- >>> import Control.Applicative
--
-- >>> let average = (/) <$> L.sum <*> L.genericLength
-- </pre>
--
-- Taking the sum, the sum of squares, ..., upto the sum of x^5
--
-- <pre>
-- >>> import Data.Traversable
--
-- >>> let powerSums = sequenceA [premap (^n) L.sum | n <- [1..5]]
--
-- >>> L.fold powerSums [1..10]
-- [55,385,3025,25333,220825]
-- </pre>
--
-- These combined folds will still traverse the list only once, streaming
-- efficiently over the list in constant space without space leaks:
--
-- <pre>
-- >>> L.fold average [1..10000000]
-- 5000000.5
--
-- >>> L.fold ((,) <$> L.minimum <*> L.maximum) [1..10000000]
-- (Just 1,Just 10000000)
-- </pre>
module Control.Foldl
-- | Efficient representation of a left fold that preserves the fold's step
-- function, initial accumulator, and extraction function
--
-- This allows the <a>Applicative</a> instance to assemble derived folds
-- that traverse the container only once
--
-- A '<a>Fold</a> a b' processes elements of type <b>a</b> and results in
-- a value of type <b>b</b>.
data Fold a b
-- | <tt>Fold </tt> <tt> step </tt> <tt> initial </tt> <tt> extract</tt>
Fold :: (x -> a -> x) -> x -> (x -> b) -> Fold a b
-- | Like <a>Fold</a>, but monadic.
--
-- A '<a>FoldM</a> m a b' processes elements of type <b>a</b> and results
-- in a monadic value of type <b>m b</b>.
data FoldM m a b
-- | <tt>FoldM </tt> <tt> step </tt> <tt> initial </tt> <tt> extract</tt>
FoldM :: (x -> a -> m x) -> (m x) -> (x -> m b) -> FoldM m a b
-- | Apply a strict left <a>Fold</a> to a <a>Foldable</a> container
fold :: Foldable f => Fold a b -> f a -> b
-- | Like <a>fold</a>, but monadic
foldM :: (Foldable f, Monad m) => FoldM m a b -> f a -> m b
-- | Convert a strict left <a>Fold</a> into a scan
scan :: Fold a b -> [a] -> [b]
-- | Fold all values within a container using <a>mappend</a> and
-- <a>mempty</a>
mconcat :: Monoid a => Fold a a
-- | Convert a "<tt>foldMap</tt>" to a <a>Fold</a>
foldMap :: Monoid w => (a -> w) -> (w -> b) -> Fold a b
-- | Get the first element of a container or return <a>Nothing</a> if the
-- container is empty
head :: Fold a (Maybe a)
-- | Get the last element of a container or return <a>Nothing</a> if the
-- container is empty
last :: Fold a (Maybe a)
-- | Get the last element of a container or return a default value if the
-- container is empty
lastDef :: a -> Fold a a
-- | Return the last N elements
lastN :: Int -> Fold a [a]
-- | Returns <a>True</a> if the container is empty, <a>False</a> otherwise
null :: Fold a Bool
-- | Return the length of the container
length :: Fold a Int
-- | Returns <a>True</a> if all elements are <a>True</a>, <a>False</a>
-- otherwise
and :: Fold Bool Bool
-- | Returns <a>True</a> if any element is <a>True</a>, <a>False</a>
-- otherwise
or :: Fold Bool Bool
-- | <tt>(all predicate)</tt> returns <a>True</a> if all elements satisfy
-- the predicate, <a>False</a> otherwise
all :: (a -> Bool) -> Fold a Bool
-- | <tt>(any predicate)</tt> returns <a>True</a> if any element satisfies
-- the predicate, <a>False</a> otherwise
any :: (a -> Bool) -> Fold a Bool
-- | Computes the sum of all elements
sum :: Num a => Fold a a
-- | Computes the product all elements
product :: Num a => Fold a a
-- | Computes the maximum element
maximum :: Ord a => Fold a (Maybe a)
-- | Computes the minimum element
minimum :: Ord a => Fold a (Maybe a)
-- | <tt>(elem a)</tt> returns <a>True</a> if the container has an element
-- equal to <tt>a</tt>, <a>False</a> otherwise
elem :: Eq a => a -> Fold a Bool
-- | <tt>(notElem a)</tt> returns <a>False</a> if the container has an
-- element equal to <tt>a</tt>, <a>True</a> otherwise
notElem :: Eq a => a -> Fold a Bool
-- | <tt>(find predicate)</tt> returns the first element that satisfies the
-- predicate or <a>Nothing</a> if no element satisfies the predicate
find :: (a -> Bool) -> Fold a (Maybe a)
-- | <tt>(index n)</tt> returns the <tt>n</tt>th element of the container,
-- or <a>Nothing</a> if the container has an insufficient number of
-- elements
index :: Int -> Fold a (Maybe a)
-- | <tt>(elemIndex a)</tt> returns the index of the first element that
-- equals <tt>a</tt>, or <a>Nothing</a> if no element matches
elemIndex :: Eq a => a -> Fold a (Maybe Int)
-- | <tt>(findIndex predicate)</tt> returns the index of the first element
-- that satisfies the predicate, or <a>Nothing</a> if no element
-- satisfies the predicate
findIndex :: (a -> Bool) -> Fold a (Maybe Int)
-- | Pick a random element, using reservoir sampling
random :: FoldM IO a (Maybe a)
-- | Pick several random elements, using reservoir sampling
randomN :: Vector v a => Int -> FoldM IO a (Maybe (v a))
-- | Converts an effectful function to a fold
--
-- <pre>
-- sink (f <> g) = sink f <> sink g -- if `(<>)` is commutative
-- sink mempty = mempty
-- </pre>
sink :: (Monoid w, Monad m) => (a -> m w) -> FoldM m a w
-- | Like <a>length</a>, except with a more general <a>Num</a> return value
genericLength :: Num b => Fold a b
-- | Like <a>index</a>, except with a more general <a>Integral</a> argument
genericIndex :: Integral i => i -> Fold a (Maybe a)
-- | Fold all values into a list
list :: Fold a [a]
-- | Fold all values into a list, in reverse order
revList :: Fold a [a]
-- | <i>O(n log n)</i>. Fold values into a list with duplicates removed,
-- while preserving their first occurrences
nub :: Ord a => Fold a [a]
-- | <i>O(n^2)</i>. Fold values into a list with duplicates removed, while
-- preserving their first occurrences
eqNub :: Eq a => Fold a [a]
-- | Fold values into a set
set :: Ord a => Fold a (Set a)
-- | Fold all values into a vector
vector :: (PrimMonad m, Vector v a) => FoldM m a (v a)
-- | Upgrade a fold to accept the <a>Fold</a> type
purely :: (forall x. (x -> a -> x) -> x -> (x -> b) -> r) -> Fold a b -> r
-- | Upgrade a monadic fold to accept the <a>FoldM</a> type
impurely :: Monad m => (forall x. (x -> a -> m x) -> m x -> (x -> m b) -> r) -> FoldM m a b -> r
-- | Generalize a <a>Fold</a> to a <a>FoldM</a>
--
-- <pre>
-- generalize (pure r) = pure r
--
-- generalize (f <*> x) = generalize f <*> generalize x
-- </pre>
generalize :: Monad m => Fold a b -> FoldM m a b
-- | Simplify a pure <a>FoldM</a> to a <a>Fold</a>
--
-- <pre>
-- simplify (pure r) = pure r
--
-- simplify (f <*> x) = simplify f <*> simplify x
-- </pre>
simplify :: FoldM Identity a b -> Fold a b
-- | Allows to continue feeding a <a>FoldM</a> even after passing it to a
-- function that closes it.
--
-- For pure <a>Fold</a>s, this is provided by the <a>Comonad</a>
-- instance.
duplicateM :: Applicative m => FoldM m a b -> FoldM m a (FoldM m a b)
-- | <tt>_Fold1 step</tt> returns a new <a>Fold</a> using just a step
-- function that has the same type for the accumulator and the element.
-- The result type is the accumulator type wrapped in <a>Maybe</a>. The
-- initial accumulator is retrieved from the <a>Foldable</a>, the result
-- is <tt>None</tt> for empty containers.
_Fold1 :: (a -> a -> a) -> Fold a (Maybe a)
-- | <tt>(premap f folder)</tt> returns a new <a>Fold</a> where f is
-- applied at each step
--
-- <pre>
-- fold (premap f folder) list = fold folder (map f list)
-- </pre>
--
-- <pre>
-- >>> fold (premap Sum mconcat) [1..10]
-- Sum {getSum = 55}
-- </pre>
--
-- <pre>
-- >>> fold mconcat (map Sum [1..10])
-- Sum {getSum = 55}
-- </pre>
--
-- <pre>
-- premap id = id
--
-- premap (f . g) = premap g . premap f
-- </pre>
--
-- <pre>
-- premap k (pure r) = pure r
--
-- premap k (f <*> x) = premap k f <*> premap k x
-- </pre>
premap :: (a -> b) -> Fold b r -> Fold a r
-- | <tt>(premapM f folder)</tt> returns a new <a>FoldM</a> where f is
-- applied to each input element
--
-- <pre>
-- foldM (premapM f folder) list = foldM folder (map f list)
-- </pre>
--
-- <pre>
-- premapM id = id
--
-- premapM (f . g) = premap g . premap f
-- </pre>
--
-- <pre>
-- premapM k (pure r) = pure r
--
-- premapM k (f <*> x) = premapM k f <*> premapM k x
-- </pre>
premapM :: (a -> b) -> FoldM m b r -> FoldM m a r
-- | A handler for the upstream input of a <a>Fold</a>
--
-- Any lens, traversal, or prism will type-check as a <a>Handler</a>
type Handler a b = forall x. (b -> Constant (Endo x) b) -> a -> Constant (Endo x) a
-- | <tt>(handles t folder)</tt> transforms the input of a <a>Fold</a>
-- using a lens, traversal, or prism:
--
-- <pre>
-- handles _1 :: Fold a r -> Fold (a, b) r
-- handles _Left :: Fold a r -> Fold (Either a b) r
-- handles traverse :: Traversable t => Fold a r -> Fold (t a) r
-- </pre>
--
-- <pre>
-- >>> fold (handles traverse sum) [[1..5],[6..10]]
-- 55
-- </pre>
--
-- <pre>
-- >>> fold (handles (traverse.traverse) sum) [[Nothing, Just 2, Just 7],[Just 13, Nothing, Just 20]]
-- 42
-- </pre>
--
-- <pre>
-- >>> fold (handles (filtered even) sum) [1,3,5,7,21,21]
-- 42
-- </pre>
--
-- <pre>
-- >>> fold (handles _2 mconcat) [(1,"Hello "),(2,"World"),(3,"!")]
-- "Hello World!"
-- </pre>
--
-- <pre>
-- handles id = id
--
-- handles (f . g) = handles f . handles g
-- </pre>
--
-- <pre>
-- handles t (pure r) = pure r
--
-- handles t (f <*> x) = handles t f <*> handles t x
-- </pre>
handles :: Handler a b -> Fold b r -> Fold a r
-- | <pre>
-- instance Monad m => Monoid (EndoM m a) where
-- mempty = EndoM return
-- mappend (EndoM f) (EndoM g) = EndoM (f >=> g)
-- </pre>
newtype EndoM m a
EndoM :: (a -> m a) -> EndoM m a
[appEndoM] :: EndoM m a -> a -> m a
-- | A Handler for the upstream input of <a>FoldM</a>
--
-- Any lens, traversal, or prism will type-check as a <a>HandlerM</a>
type HandlerM m a b = forall x. (b -> Constant (EndoM m x) b) -> a -> Constant (EndoM m x) a
-- | <tt>(handlesM t folder)</tt> transforms the input of a <a>FoldM</a>
-- using a lens, traversal, or prism:
--
-- <pre>
-- handlesM _1 :: FoldM m a r -> FoldM (a, b) r
-- handlesM _Left :: FoldM m a r -> FoldM (Either a b) r
-- handlesM traverse :: Traversable t => FoldM m a r -> FoldM m (t a) r
-- </pre>
--
-- <a>handlesM</a> obeys these laws:
--
-- <pre>
-- handlesM id = id
--
-- handlesM (f . g) = handlesM f . handlesM g
-- </pre>
--
-- <pre>
-- handlesM t (pure r) = pure r
--
-- handlesM t (f <*> x) = handlesM t f <*> handlesM t x
-- </pre>
handlesM :: Monad m => HandlerM m a b -> FoldM m b r -> FoldM m a r
instance GHC.Base.Functor (Control.Foldl.Fold a)
instance Data.Profunctor.Unsafe.Profunctor Control.Foldl.Fold
instance Control.Comonad.Comonad (Control.Foldl.Fold a)
instance GHC.Base.Applicative (Control.Foldl.Fold a)
instance GHC.Base.Monoid b => GHC.Base.Monoid (Control.Foldl.Fold a b)
instance GHC.Num.Num b => GHC.Num.Num (Control.Foldl.Fold a b)
instance GHC.Real.Fractional b => GHC.Real.Fractional (Control.Foldl.Fold a b)
instance GHC.Float.Floating b => GHC.Float.Floating (Control.Foldl.Fold a b)
instance GHC.Base.Monad m => GHC.Base.Functor (Control.Foldl.FoldM m a)
instance GHC.Base.Monad m => GHC.Base.Applicative (Control.Foldl.FoldM m a)
instance GHC.Base.Monad m => Data.Profunctor.Unsafe.Profunctor (Control.Foldl.FoldM m)
instance (GHC.Base.Monoid b, GHC.Base.Monad m) => GHC.Base.Monoid (Control.Foldl.FoldM m a b)
instance (GHC.Base.Monad m, GHC.Num.Num b) => GHC.Num.Num (Control.Foldl.FoldM m a b)
instance (GHC.Base.Monad m, GHC.Real.Fractional b) => GHC.Real.Fractional (Control.Foldl.FoldM m a b)
instance (GHC.Base.Monad m, GHC.Float.Floating b) => GHC.Float.Floating (Control.Foldl.FoldM m a b)
instance GHC.Base.Monad m => GHC.Base.Monoid (Control.Foldl.EndoM m a)
-- | Folds for byte streams
module Control.Foldl.ByteString
-- | Apply a strict left <a>Fold</a> to a lazy bytestring
fold :: Fold ByteString a -> ByteString -> a
-- | Get the first byte of a byte stream or return <a>Nothing</a> if the
-- stream is empty
head :: Fold ByteString (Maybe Word8)
-- | Get the last byte of a byte stream or return <a>Nothing</a> if the
-- byte stream is empty
last :: Fold ByteString (Maybe Word8)
-- | Returns <a>True</a> if the byte stream is empty, <a>False</a>
-- otherwise
null :: Fold ByteString Bool
-- | Return the length of the byte stream in bytes
length :: Num n => Fold ByteString n
-- | <tt>(any predicate)</tt> returns <a>True</a> if any byte satisfies the
-- predicate, <a>False</a> otherwise
any :: (Word8 -> Bool) -> Fold ByteString Bool
-- | <tt>(all predicate)</tt> returns <a>True</a> if all bytes satisfy the
-- predicate, <a>False</a> otherwise
all :: (Word8 -> Bool) -> Fold ByteString Bool
-- | Computes the maximum byte
maximum :: Fold ByteString (Maybe Word8)
-- | Computes the minimum byte
minimum :: Fold ByteString (Maybe Word8)
-- | <tt>(elem w8)</tt> returns <a>True</a> if the byte stream has a byte
-- equal to <tt>w8</tt>, <a>False</a> otherwise
elem :: Word8 -> Fold ByteString Bool
-- | <tt>(notElem w8)</tt> returns <a>False</a> if the byte stream has a
-- byte equal to <tt>w8</tt>, <a>True</a> otherwise
notElem :: Word8 -> Fold ByteString Bool
-- | <tt>(find predicate)</tt> returns the first byte that satisfies the
-- predicate or <a>Nothing</a> if no byte satisfies the predicate
find :: (Word8 -> Bool) -> Fold ByteString (Maybe Word8)
-- | <tt>(index n)</tt> returns the <tt>n</tt>th byte of the byte stream,
-- or <a>Nothing</a> if the stream has an insufficient number of bytes
index :: Integral n => n -> Fold ByteString (Maybe Word8)
-- | <tt>(elemIndex w8)</tt> returns the index of the first byte that
-- equals <tt>w8</tt>, or <a>Nothing</a> if no byte matches
elemIndex :: Num n => Word8 -> Fold ByteString (Maybe n)
-- | <tt>(findIndex predicate)</tt> returns the index of the first byte
-- that satisfies the predicate, or <a>Nothing</a> if no byte satisfies
-- the predicate
findIndex :: Num n => (Word8 -> Bool) -> Fold ByteString (Maybe n)
-- | <tt>count w8</tt> returns the number of times <tt>w8</tt> appears
count :: Num n => Word8 -> Fold ByteString n
-- | Folds for text streams
module Control.Foldl.Text
-- | Apply a strict left <a>Fold</a> to lazy text
fold :: Fold Text a -> Text -> a
-- | Get the first character of a text stream or return <a>Nothing</a> if
-- the stream is empty
head :: Fold Text (Maybe Char)
-- | Get the last character of a text stream or return <a>Nothing</a> if
-- the text stream is empty
last :: Fold Text (Maybe Char)
-- | Returns <a>True</a> if the text stream is empty, <a>False</a>
-- otherwise
null :: Fold Text Bool
-- | Return the length of the text stream in characters
length :: Num n => Fold Text n
-- | <tt>(any predicate)</tt> returns <a>True</a> if any character
-- satisfies the predicate, <a>False</a> otherwise
any :: (Char -> Bool) -> Fold Text Bool
-- | <tt>(all predicate)</tt> returns <a>True</a> if all characters satisfy
-- the predicate, <a>False</a> otherwise
all :: (Char -> Bool) -> Fold Text Bool
-- | Computes the maximum character
maximum :: Fold Text (Maybe Char)
-- | Computes the minimum character
minimum :: Fold Text (Maybe Char)
-- | <tt>(elem c)</tt> returns <a>True</a> if the text stream has a
-- character equal to <tt>c</tt>, <a>False</a> otherwise
elem :: Char -> Fold Text Bool
-- | <tt>(notElem c)</tt> returns <a>False</a> if the text stream has a
-- character equal to <tt>c</tt>, <a>True</a> otherwise
notElem :: Char -> Fold Text Bool
-- | <tt>(find predicate)</tt> returns the first character that satisfies
-- the predicate or <a>Nothing</a> if no character satisfies the
-- predicate
find :: (Char -> Bool) -> Fold Text (Maybe Char)
-- | <tt>(index n)</tt> returns the <tt>n</tt>th character of the text
-- stream, or <a>Nothing</a> if the stream has an insufficient number of
-- characters
index :: Integral n => n -> Fold Text (Maybe Char)
-- | <tt>(elemIndex c)</tt> returns the index of the first character that
-- equals <tt>c</tt>, or <a>Nothing</a> if no character matches
elemIndex :: Num n => Char -> Fold Text (Maybe n)
-- | <tt>(findIndex predicate)</tt> returns the index of the first
-- character that satisfies the predicate, or <a>Nothing</a> if no
-- character satisfies the predicate
findIndex :: Num n => (Char -> Bool) -> Fold Text (Maybe n)
-- | <tt>(count c)</tt> returns the number of times <tt>c</tt> appears
count :: Num n => Char -> Fold Text n
|