Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant mutable list allocation in TrampolineArb #2904

Merged
merged 1 commit into from Mar 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,28 +47,26 @@ internal fun <A, B> Arb<A>.trampoline(next: (Sample<A>) -> Arb<B>): Arb<B> = whe
@Suppress("UNCHECKED_CAST")
internal class TrampolineArb<A> private constructor(
private val first: Arb<A>,
commands: List<(Sample<Any>) -> Arb<Any>>
private val commands: List<(Sample<Any>) -> Arb<Any>>
) : Arb<A>() {
constructor(first: Arb<A>) : this(first, emptyList())

private val commandList: MutableList<(Sample<Any>) -> Arb<Any>> = commands.toMutableList()

fun <A, B> thunk(fn: (Sample<A>) -> Arb<B>): TrampolineArb<B> =
TrampolineArb(
first,
commandList.toList() + (fn as (Sample<Any>) -> Arb<Any>)
commands + (fn as (Sample<Any>) -> Arb<Any>)
) as TrampolineArb<B>

override fun edgecase(rs: RandomSource): A? =
commandList
commands
.fold(first as Arb<Any>) { currentArb, next ->
val currentEdge = currentArb.edgecase(rs) ?: currentArb.sample(rs).value
next(Sample(currentEdge))
}
.edgecase(rs) as A?

override fun sample(rs: RandomSource): Sample<A> =
commandList
commands
.fold(first as Arb<Any>) { currentArb, next ->
next(currentArb.sample(rs))
}
Expand Down