jimn
07/22/2021, 9:11 PMfold
, or reduce
to this, finally, i just did it as a while loop. is there a one-liner for this ?
var acc = sim.outputRange.indices
val tree = sortedSetOf<Int>()
while (acc.isNotEmpty()) {
tree += acc;
acc = acc.map { this.usefulScope(it) }.flatten().filter {
it in sim.hiddenRange } - tree
}
ephemient
07/22/2021, 9:46 PMval tree = sortedSetOf<Int>()
generateSequence(sim.outputRange.indices) { acc ->
acc.flatMap { usefulScope(it) }.filter { it in sim.hiddenRange && it !in tree }.ifEmpty { null }
}.flatMapTo(tree) { it }
Peter Ertl
07/22/2021, 10:28 PMsim.outputRange.indices
.mapNotNull(this::usefulScope)
.intersect(sim.hiddenRange)
.minus(tree)
.toSortedSet()
ephemient
07/22/2021, 10:34 PMsim.outputRange.indices + sim.outputRange.indices.flatMap(::usefulScope) + sim.outputRange.indices.flatMap(::usefulScope).flatMap(::usefulScope) + ...
jimn
07/23/2021, 12:35 AMPeter Ertl
07/23/2021, 5:44 AMephemient
07/23/2021, 6:07 AMval queue = ArrayDeque(sim.outputRange.indices)
val tree = sortedSetOf<Int>()
generateSequence(queue::removeFirstOrNull).filter(tree::add).flatMapTo(queue) { usefulScope(it) intersect sim.hiddenRange }
Roukanken
07/23/2021, 7:36 AMusefulScope
does
Imho I wouldn't try to do it as one liner, I would just do a generic search
function, that takes a starting point(s) and a neighborhoods
function (takes a point, and returns all nearby)
this way you can separate the concepts of what exactly it does: both simple search & neighborhoods will be easy to understand, in fact the last @ephemient's example is pretty close to this
Something like this:
fun <T> breadthFirstSearch(start: Collection<T>, neighborhoods: (T) -> Collection<T>): Sequence<T> {
val queue = ArrayDeque(start)
return generateSequence(queue::removeFirstOrNull)
.distinct()
.onEach { queue.addAll(neighborhoods(it)) }
}
breadthFirstSearch(sim.outputRange.indices) { current ->
usefulScope(current) intersect sim.hiddenRange
}
good thing about it is, that you separate what you search, from how you search itephemient
07/23/2021, 7:57 AMephemient
07/23/2021, 8:05 AMval tree = sortedSetOf<Int>()
val search = DeepRecursiveFunction<Int, Unit> { if (tree.add(it)) (usefulScope(it) intersect sim.hiddenRange).forEach(::callRecursive) }
sim.outputRange.indices.forEach(search::invoke)
jimn
07/23/2021, 8:22 AMjimn
07/23/2021, 8:36 AMjimn
07/23/2021, 9:31 AMjimn
07/23/2021, 9:36 AMGnome(points=0.9999116613481498, jeans=[(2, Jean(gated=false, af=ExponentialLinearFunction, impulse=1.0, inputs=[0, 1], weights=[1.0, 0.76])), (3, Jean(gated=false, af=EvSailSigmoid, impulse=0.0, inputs=[0, 1], weights=[-1.0, 0.46])), (4, Jean(gated=false, af=SqrtAndLinear, impulse=1.0, inputs=[0, 1], weights=[1.0, 0.99])), (5, Jean(gated=false, af=SignedClampedLinear, impulse=2.0, inputs=[0, 1, 2], weights=[1.0, 1.0, 1.0])), (6, Jean(gated=false, af=ExponentialLinearFunction, impulse=2.5, inputs=[0, 1, 3, 4], weights=[1.0, 0.76, 1.0, 1.0])), (7, Jean(gated=false, af=Linear, impulse=1.0, inputs=[0, 1, 4], weights=[1.0, 1.0, 1.0])), (8, Jean(gated=false, af=Absolute, impulse=2.820130884406553, inputs=[0, 1, 2, 7], weights=[0.82, 1.0, 1.0, 1.0])), (9, Jean(gated=false, af=ConvertToSigned, impulse=4.820130884406553, inputs=[0, 1, 5, 8], weights=[1.0, 0.36, 1.0, 1.0])), (10, Jean(gated=false, af=BipolarSigmoid, impulse=7.200927962384435, inputs=[0, 1, 3, 4, 5, 7, 8, 9], weights=[0.36, 1.0, 0.76, 1.0, 1.0, 1.0, 1.0, 1.0])), (11, Jean(gated=false, af=Sine, impulse=2.48750260912872, inputs=[0, 1, 2, 7, 8], weights=[1.0, 1.0, 0.76, 0.68, 0.36])), (12, Jean(gated=false, af=Recipriocal, impulse=9.652278437785709, inputs=[0, 1, 3, 4, 6, 7, 8, 9, 10, 11], weights=[1.0, 1.0, 1.0, 0.76, 1.0, 1.0, 1.0, 0.46, 1.0, 1.0])), (13, Jean(gated=false, af=SteepSigmoid, impulse=2.6267907935893584, inputs=[0, 1, 2, 5, 7, 8, 12], weights=[0.0, 0.97, 0.76, 0.76, 1.0, 0.0, 1.0])), (19, Jean(gated=false, af=Linear, impulse=1.0002208466296254, inputs=[0, 1, 8, 11, 13], weights=[0.22, 0.21, 0.05, 0.69, 0.21]))]}