intercalate package:streamly-core

Deprecated: Please use unfoldEachSepBySeq instead.
Deprecated: Please use unfoldEachEndBySeq instead.
Deprecated: Please use concatEndBySeq instead.
See intercalateSepBy for detailed documentation. You can think of this as interleaveEndBy on the stream of streams followed by concat. Same as the following but more efficient:
>>> intercalateEndBy u1 s1 u2 s2 = Stream.concat $ Stream.interleaveEndBy (fmap (Stream.unfold u1) s1) (fmap (Stream.unfold u2) s2)
Pre-release
The first stream Stream m b is turned into a stream of streams by unfolding each element using the first unfold, similarly Stream m a is also turned into a stream of streams. The second stream of streams is interspersed with the streams from the first stream in an infix manner and then the resulting stream is flattened. You can think of this as interleaveSepBy on the stream of streams followed by concat. Same as the following but more efficient:
>>> intercalateSepBy u1 s1 u2 s2 = Stream.concat $ Stream.interleaveSepBy (fmap (Stream.unfold u1) s1) (fmap (Stream.unfold u2) s2)
If the separator stream consists of nil streams then it becomes equivalent to unfoldEach:
>>> unfoldEach = Stream.intercalateSepBy (Unfold.nilM (const (return ()))) (Stream.repeat ())
Pre-release
Apply two parsers alternately to an input stream. The input stream is considered an interleaving of two patterns. The two parsers represent the two patterns. Parsing starts at the first parser and stops at the first parser. It can be used to parse a infix style pattern e.g. p1 p2 p1 . Empty input or single parse of the first parser is accepted.
>>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList

>>> p2 = Parser.satisfy (== '+')

>>> p = Parser.deintercalate p1 p2 Fold.toList

>>> Stream.parse p $ Stream.fromList ""
Right []

>>> Stream.parse p $ Stream.fromList "1"
Right [Left "1"]

>>> Stream.parse p $ Stream.fromList "1+"
Right [Left "1"]

>>> Stream.parse p $ Stream.fromList "1+2+3"
Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
See also chainl1.
Apply two parsers alternately to an input stream. The input stream is considered an interleaving of two patterns. The two parsers represent the two patterns. Parsing starts at the first parser and stops at the first parser. It can be used to parse a infix style pattern e.g. p1 p2 p1 . Empty input or single parse of the first parser is accepted.
>>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList

>>> p2 = Parser.satisfy (== '+')

>>> p = Parser.deintercalate1 p1 p2 Fold.toList

>>> Stream.parsePos p $ Stream.fromList ""
Left (ParseErrorPos 0 "takeWhile1: end of input")

>>> Stream.parse p $ Stream.fromList "1"
Right [Left "1"]

>>> Stream.parse p $ Stream.fromList "1+"
Right [Left "1"]

>>> Stream.parse p $ Stream.fromList "1+2+3"
Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
Like deintercalate but the entire input must satisfy the pattern otherwise the parser fails. This is many times faster than deintercalate.
>>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList

>>> p2 = Parser.satisfy (== '+')

>>> p = Parser.deintercalateAll p1 p2 Fold.toList

>>> Stream.parse p $ Stream.fromList ""
Right []

>>> Stream.parse p $ Stream.fromList "1"
Right [Left "1"]

>>> Stream.parsePos p $ Stream.fromList "1+"
Left (ParseErrorPos 2 "takeWhile1: end of input")

>>> Stream.parse p $ Stream.fromList "1+2+3"
Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
See also chainl1.
Deprecated: Please use intercalateSepBy instead.
Deprecated: Please use intercalateEndBy instead. Note the change in argument order.