Skip to content

Commit

Permalink
traverse => traverse_ to avoid building list of units!
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jun 8, 2023
1 parent d8f8a48 commit 860839e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions parsley/shared/src/main/scala/parsley/combinator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ object combinator {
def traverse[A, B](f: A => Parsley[B], xs: A*): Parsley[List[B]] = sequence(xs.map(f): _*)
// this will be used in future!
private [parsley] def traverse5[A, B](xs: A*)(f: A => Parsley[B]): Parsley[List[B]] = traverse(f, xs: _*)
private [parsley] def traverse_[A](xs: A*)(f: A => Parsley[_]): Parsley[Unit] = skip(unit, xs.map(f): _*)

/** This combinator will parse each of `ps` in order, discarding the results.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import parsley.Success
import parsley.XAssert.assert
import parsley.XCompat.unused
import parsley.character.{item, stringOfSome}
import parsley.combinator.{option, traverse5}
import parsley.combinator.{option, traverse5, traverse_}
import parsley.errors.{ErrorBuilder, Token, TokenSpan}
import parsley.position

Expand Down Expand Up @@ -58,7 +58,7 @@ trait LexToken { this: ErrorBuilder[_] =>
private lazy val makeParser: Parsley[Either[::[(String, (Int, Int))], String]] = {
val toks = traverse5(tokens: _*)(p => option(lookAhead(attempt(p) <~> position.pos))).map(_.flatten).collect { case toks@(_::_) => toks }
// this can only fail if either there is no input (which there must be), or there is a token at the front, in which case `rawTok` is not parsed anyway
val rawTok = stringOfSome(traverse5(tokens: _*)(notFollowedBy) *> item)
val rawTok = stringOfSome(traverse_(tokens: _*)(notFollowedBy) *> item)
toks <+> rawTok
}

Expand Down

0 comments on commit 860839e

Please sign in to comment.