Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

recursion - Groovy :: Map Find Recursive

Edit See @tim's solution below for the "correct" Groovy-esque approach to map recursion. Since Map findRecursive does not yet exist in Groovy, if you find yourself needing this functionality in various parts of your app, just add it to Map metaClass:

Map.metaClass.findRecursive = {String key->
    if(delegate.containsKey(key)) return delegate."$key"
    else
        for(m in delegate) {
            if(m.value in Map) return m.value.findRecursive(key)
        }
}
// then anywhere in your app
someMap.findRecursive('foo')

Original Was hoping something like findResult{it.key=='foo'} would recurse through map elements beyond 1-d deep, but appears not to be the case.

Rolled my own recursive map finder, but am wondering if there's a better way to do this. Maybe there's a built-in function I'm missing, or an even Groovier (concise) way to pull off the below:

Map map = [school:[id:'schoolID', table:'_school',
    children:[team:[id:'teamID',table:'_team',
        children:[player:[id:'playerID',table:'_roster']]
    ]]
]]

class Foo {
    static finder = {Map map, String key->
        if(map.containsKey(key)) return map[key]
        else
            for(m in map) {
                if(m.value in Map) return this.finder(m.value,key)
            }
    }
}
println Foo.finder(map,'team') 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With Groovy 1.8 (reqd for the findResult method), you could do something like this:

class DeepFinder {
  static Object findDeep( Map map, Object key ) {
    map.get( key ) ?: map.findResult { k, v -> if( v in Map ) v.findDeep( key ) }
  }
}

use( DeepFinder ) {
  println map.findDeep( 'team' )
}

There's no recursing default Groovy method that I know of...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...