-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: performance improvements in core/matchers module (Matchers,…
… Matching, CollectionUtils)
- Loading branch information
Tomasz Linkowski
committed
Dec 12, 2022
1 parent
21af9f5
commit c9a62cc
Showing
5 changed files
with
37 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 5 additions & 26 deletions
31
core/matchers/src/main/kotlin/au/com/dius/pact/core/matchers/util/CollectionUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,11 @@ | ||
package au.com.dius.pact.core.matchers.util | ||
|
||
fun tails(col: List<String>): List<List<String>> { | ||
val result = mutableListOf<List<String>>() | ||
var acc = col | ||
while (acc.isNotEmpty()) { | ||
result.add(acc) | ||
acc = acc.drop(1) | ||
} | ||
result.add(acc) | ||
return result | ||
} | ||
|
||
fun <A, B> corresponds(l1: List<A>, l2: List<B>, fn: (a: A, b: B) -> Boolean): Boolean { | ||
return if (l1.size == l2.size) { | ||
l1.zip(l2).all { fn(it.first, it.second) } | ||
} else { | ||
false | ||
} | ||
} | ||
import java.util.Collections.nCopies | ||
|
||
fun <E> List<E>.padTo(size: Int, item: E): List<E> { | ||
return if (size < this.size) { | ||
this.dropLast(this.size - size) | ||
} else { | ||
val list = this.toMutableList() | ||
for (i in this.size.until(size)) { | ||
list.add(item) | ||
} | ||
return list | ||
return when { | ||
size < this.size -> subList(fromIndex = 0, toIndex = size) | ||
size > this.size -> this + nCopies(size - this.size, item) | ||
else -> this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters