functional programming - Fixed point in Scala -
is there shortcut following code snippet?
while (true) { val newclusters = this.iterate(instances, clusters) if (newclusters == clusters) { return clusters } clusters = newclusters }
i calculate fixed point, i.e. execute function such result stable. aware of higher-order functions suit purposes?
an adaptation fixpoint calculation example scala example martin odersky (chapter 'first-class functions', section 5.3),
val instances = ... // question statement def isapproxfeasible(x: clusters, y: clusters) = some_distance_x_y < threshold def fixedpoint(f: clusters => clusters)(initapprox: clusters) = { def iterate(approx: clusters): clusters = { val newclusters = f(approx) if (iscloseenough(approx, newclusters)) newclusters else iterate(newclusters) } iterate(initapprox) }
where function f: clusters => clusters
delivers new candidate clusters, , initapprox
corresponds first, initial guess on fixpoint. function isapproxfeasible
helps ensuring termination a priori threshold.
Comments
Post a Comment