inits -package:base-compat-batteries

The inits function returns all initial segments of the argument, shortest first. inits is semantically equivalent to map reverse . scanl (flip (:)) [], but under the hood uses a queue to amortize costs of reverse.

Laziness

Note that inits has the following strictness property: inits (xs ++ _|_) = inits xs ++ _|_ In particular, inits _|_ = [] : _|_

Examples

>>> inits "abc"
["","a","ab","abc"]
>>> inits []
[[]]
inits is productive on infinite lists:
>>> take 5 $ inits [1..]
[[],[1],[1,2],[1,2,3],[1,2,3,4]]
The inits function takes a stream xs and returns all the finite prefixes of xs, starting with the shortest. The result is NonEmpty because the result always contains the empty list as the first element.
inits [1,2,3] == [] :| [[1], [1,2], [1,2,3]]
inits [1] == [] :| [[1]]
inits [] == [] :| []
O(n) Returns all initial segments of the given ByteString, shortest first.
Returns all initial segments of the given ByteString, shortest first.
O(n) Return all initial segments of the given Text, shortest first.
O(n²) Return all initial segments of the given Text, shortest first.
Returns a sequence of all prefixes of this sequence, shortest first. For example,
inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
Evaluating the <math>th prefix takes <math>, but evaluating every prefix in the sequence takes <math> due to sharing.
This function is lazier than the one suggested in the Haskell 98 report. It is inits undefined = [] : undefined, in contrast to Data.List.inits undefined = undefined.
O(n) Return all initial segments of the given ByteString, shortest first.
The inits function returns all initial segments of the argument, shortest first. For example,
>>> inits "abc"
["","a","ab","abc"]
Note that inits has the following strictness property: inits (xs ++ _|_) = inits xs ++ _|_ In particular, inits _|_ = [] : _|_
The inits function takes a stream xs and returns all the finite prefixes of xs.
Return all the initial segments of seq with the shortest first.
> inits [1,2]
[[],[1],[1,2]]
> inits []
[[]]
Only advised for structures with efficient appending of single elements like Sequence. Alternatively you may consider initsRev.
All initial segments of the list, shortest first
O(n^2) Return all initial segments of the given JSString, shortest first.
Returns the list of all prefixes of the argument, mempty first.
O(n) Return all initial segments of the given Vector, shortest first.
The inits function returns all initial segments of the argument, shortest first. For example,
>>> inits "abc"
["","a","ab","abc"]
Note that inits has the following strictness property: inits (xs ++ _|_) = inits xs ++ _|_ In particular, inits _|_ = [] : _|_ inits is semantically equivalent to map reverse . scanl (flip (:)) [], but under the hood uses a queue to amortize costs of reverse.
The inits function takes a stream xs and returns all the finite prefixes of xs. Note that this inits is lazier then Data.List.inits:
inits _|_ = [] ::: _|_
while for Data.List.inits:
inits _|_ = _|_
Construct the fully balanced initial tree, where the leaves are simply the numbers 0 through 255.
Generate all prefixes of an infinite list.
>>> :set -XPostfixOperators

>>> Data.List.Infinite.take 5 $ Data.List.Infinite.inits (0...)
[[],[0],[0,1],[0,1,2],[0,1,2,3]]
If you need reversed prefixes, they can be generated cheaper using scanl':
>>> :set -XPostfixOperators

>>> Data.List.Infinite.take 5 $ Data.List.Infinite.scanl' (flip (:)) [] (0...)
[[],[0],[1,0],[2,1,0],[3,2,1,0]]
Returns a sequence of all non-empty prefixes of this sequence, shortest first. For example,
tails (fromList (1:|[2,3])) = fromList (fromList (1:|[]) :| [fromList (1:|[2]), fromList (1:|[2,3]))
Evaluating the <math>th prefix takes <math>, but evaluating every prefix in the sequence takes <math> due to sharing.
Produce a list of (<= priority) language tags
>>> inits enGBTJP
[en,en-GB,en-GB-t-jp]