Only walk list once in splitAt (#9275)

* Only walk list once in splitAt

No idea if that makes a significant difference anywhere but walking
the list twice is definitely not faster.

changelog_begin
changelog_end

* Avoid hardcoded locations in tests

changelog_begin
changelog_end
This commit is contained in:
Moritz Kiefer 2021-03-30 19:26:59 +02:00 committed by GitHub
parent 46c6701d8e
commit eb91dcf853
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -422,7 +422,11 @@ drop n (_ :: xs) = drop (n-1) xs
-- | Split a list at a given index.
splitAt : Int -> [a] -> ([a], [a])
splitAt i x = (take i x, drop i x)
splitAt _ [] = ([], [])
splitAt i ys@(x :: xs)
| i <= 0 = ([], ys)
| otherwise = case splitAt (i - 1) xs of
(a, b) -> (x :: a, b)
-- | Take elements from a list while the predicate holds.
takeWhile : (a -> Bool) -> [a] -> [a]

View File

@ -269,7 +269,10 @@ abstract class AbstractFuncIT
}
"traceOrder" should {
"emit trace statements in correct order" in {
def traceMsg(msg: String) = s"""[DA.Internal.Prelude:540]: "$msg""""
val msgRegex = raw"""\[DA.Internal.Prelude:\d+]: "(.*)"""".r
def stripLoc(msg: String) = msg match {
case msgRegex(msg_) => msg_
}
for {
clients <- participantClients()
_ = LogCollector.clear()
@ -281,7 +284,7 @@ abstract class AbstractFuncIT
} yield {
assert(v == SUnit)
val logMsgs = LogCollector.events.map(_.getMessage)
assert(logMsgs == Seq(traceMsg("abc"), traceMsg("def"), traceMsg("abc"), traceMsg("def")))
assert(logMsgs.map(stripLoc(_)) == Seq("abc", "def", "abc", "def"))
}
}
}