:: List a -> List a package:rio
O(n) Yield the argument but force it not to retain any extra
memory, possibly by copying it.
This is especially useful when dealing with slices. For example:
force (slice 0 2 <huge vector>)
Here, the slice retains a reference to the huge vector. Forcing it
creates a copy of just the elements that belong to the slice and
allows the huge vector to be garbage collected.
O(1) Yield all but the last element without copying. The vector
may not be empty.
O(1) Yield all but the first element without copying. The
vector may not be empty.
O(1) Yield all but the last element without copying. The vector
may not be empty but this is not checked.
O(1) Yield all but the first element without copying. The
vector may not be empty but this is not checked.
Repeat an action indefinitely.
Using
ApplicativeDo: '
forever as' can be
understood as the pseudo-
do expression
do as
as
..
with
as repeating.
Examples
A common use of
forever is to process input from network
sockets,
Handles, and channels (e.g.
MVar and
Chan).
For example, here is how we might implement an
echo server,
using
forever both to listen for client connections on a
network socket and to echo client input on client connection handles:
echoServer :: Socket -> IO ()
echoServer socket = forever $ do
client <- accept socket
forkFinally (echo client) (\_ -> hClose client)
where
echo :: Handle -> IO ()
echo client = forever $
hGetLine client >>= hPutStrLn client
O(n) Convert different vector types
O(1) First element of a vector in a monad. See
indexM
for an explanation of why this is useful.
O(1) Last element of a vector in a monad. See
indexM for
an explanation of why this is useful.
O(1) First element in a monad without checking for empty
vectors. See
indexM for an explanation of why this is useful.
O(1) Last element in a monad without checking for empty
vectors. See
indexM for an explanation of why this is useful.
O(n) Drop repeated adjacent elements.