FAQ
Can someone explain why this is illegal (./cluster.go:40: invalid
continue label ATTEMPT)? Should I just be using goto here? My use here
feels sane, and continue indicates what I mean, but it seems that the
label must immediately precede the for loop - is this the case? It seems
restrictive.

thanks
Dan

func ClusterFeaturePairs(p []*pals.FeaturePair, epsilon float64, effort int) (pc [][]*pals.FeaturePair, rf []*feat.Feature) {
km := kmeans.NewKmeans(Pairs(p))

values := km.Values()
cut := make([]float64, len(values))
for i, v := range values {
l := epsilon * (v.Y() - v.X())
cut[i] = l * l
}

for k := 1; k <= len(p); k++ {
ATTEMPT:
var e int
if k == 1 {
e = 1
} else {
e = effort
}
for attempt := 0; attempt < e; attempt++ {
km.Seed(k)
km.Cluster()
centers := km.Means()
for i, v := range values {
dx, dy := centers[v.Cluster()].X()-v.X(), centers[v.Cluster()].Y()-v.Y()
ok := dx*dx+dy*dy < cut[i]
if !ok {
continue ATTEMPT
}
}

pc = make([][]*pals.FeaturePair, k)
rf = make([]*feat.Feature, k)
for ci, c := range km.Clusters() {
rf[ci] = &feat.Feature{Location: p[0].A.Location, Start: int(centers[ci].X()), End: int(centers[ci].Y())}
pc[ci] = make([]*pals.FeaturePair, centers[ci].Count())
for pi, i := range c {
pc[ci][pi] = p[i]
}
}

return
}
}

panic("cannot reach")
}


--

Search Discussions

  • David Symonds at Oct 7, 2012 at 11:12 pm
    Put the label before the for loop instead of inside it.

    --
  • Dan Kortschak at Oct 7, 2012 at 11:16 pm
    But that's not what I mean. It is before the relevant loop. If I replace
    continue with goto I get the behaviour I intend.
    On Mon, 2012-10-08 at 10:12 +1100, David Symonds wrote:
    Put the label before the for loop instead of inside it.

    --
  • David Symonds at Oct 7, 2012 at 11:19 pm
    It has to be immediately before the loop. If you want it where you have it,
    use goto instead.

    --
  • Dan Kortschak at Oct 7, 2012 at 11:28 pm
    OK. That was changed in d4f8b77723db?
    On Mon, 2012-10-08 at 10:19 +1100, David Symonds wrote:
    It has to be immediately before the loop. If you want it where you
    have it,
    use goto instead.

    --
  • Russ Cox at Oct 8, 2012 at 2:47 am

    On Sun, Oct 7, 2012 at 7:09 PM, Dan Kortschak wrote:
    Can someone explain why this is illegal (./cluster.go:40: invalid
    continue label ATTEMPT)? Should I just be using goto here? My use here
    feels sane, and continue indicates what I mean, but it seems that the
    label must immediately precede the for loop - is this the case? It seems
    restrictive.
    Yes, this is the case. I don't know that it is restrictive, it's just
    trying not to be error-prone. Continue is an operation on for loops.
    When you say continue, it refers to the innermost for loop. If you
    want to specify a different loop to continue, you have to label the
    loop, not a statement inside the loop. Requiring that the continue
    label actually label a loop avoids errors guessing what loop is meant
    when the label points elsewhere. For what it's worth, I believe this
    is the same behavior as in Java. This has not changed in many years.
    The revision d4f8b77723db that you found was fixing a corner case
    where in

    L:{}
    for x := range y {
    break L
    }

    the L label was still treated (incorrectly) as labeling the for loop.
    But a label inside a loop body has never been considered to label the
    loop holding that body, and that revision predates the public release.
    The label behavior has not changed since the public release.

    If we allowed the label to go either inside the loop body or before
    the loop, as in

    for x := range y {
    L:
    f()
    for z := range w {
    continue L
    }
    }

    then if we delete the call to f, the label becomes ambiguous.

    Russ

    --
  • Dan Kortschak at Oct 8, 2012 at 3:06 am
    Looking at my repository more carefully (it was around June the last
    time I touched this code, so there is no reliable memory of what I was
    doing) I think the situation is that I had made the change of including
    the conditional block between the label and loop and then moved on to
    something else without testing (the change was not committed).

    Thanks for taking the time to explain.

    Dan
    On Sun, 2012-10-07 at 22:46 -0400, Russ Cox wrote:
    On Sun, Oct 7, 2012 at 7:09 PM, Dan Kortschak
    wrote:
    Can someone explain why this is illegal (./cluster.go:40: invalid
    continue label ATTEMPT)? Should I just be using goto here? My use here
    feels sane, and continue indicates what I mean, but it seems that the
    label must immediately precede the for loop - is this the case? It seems
    restrictive.
    Yes, this is the case. I don't know that it is restrictive, it's just
    trying not to be error-prone. Continue is an operation on for loops.
    When you say continue, it refers to the innermost for loop. If you
    want to specify a different loop to continue, you have to label the
    loop, not a statement inside the loop. Requiring that the continue
    label actually label a loop avoids errors guessing what loop is meant
    when the label points elsewhere. For what it's worth, I believe this
    is the same behavior as in Java. This has not changed in many years.
    The revision d4f8b77723db that you found was fixing a corner case
    where in

    L:{}
    for x := range y {
    break L
    }

    the L label was still treated (incorrectly) as labeling the for loop.
    But a label inside a loop body has never been considered to label the
    loop holding that body, and that revision predates the public release.
    The label behavior has not changed since the public release.

    If we allowed the label to go either inside the loop body or before
    the loop, as in

    for x := range y {
    L:
    f()
    for z := range w {
    continue L
    }
    }

    then if we delete the call to f, the label becomes ambiguous.

    Russ

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 7, '12 at 11:12p
activeOct 8, '12 at 3:06a
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase