closures - Traversing/modifying a Groovy data structure -


i'm developing project read data in proprietary format, gather unified groovy data structure, , write out in neat xml (or, json, haven't decided yet) format. i'm total newbie groovy, but, project being ant build project, groovy seems best way go.

so far good, i'm able read data, create atomic structures, concatenate them using >> operator, , seamlessly dump them xml markupbuilder (ways easier if doing in java). however, i'm stuck @ point when need modify gathered structures, or traverse through them compose aggregated data.

to illustrate, supposing collected our data it's equivalent to:

def inventory = {     car (make: "subaru", model: "impreza wrx", year: 2010, color: "blue") {         feature ("premium sound")         feature ("brembo brakes")         bug ("leaks oil")         bug ("needs new transmission")     }     car (make: "jeep", model: "wrangler", year: 13, awd: true) {         feature ("soft top")         bug ("doesn't start")         bug ("flooded")     }     // blahblahblah } 

and we're trying achieve following, example:

  1. remove "bug" items (supposing, we're composing list publishing on our dealership's website). or, "feature"'s (if it's our pre-sale repairs squad).
  2. go through list , make sure "year" attributes 4-digit
  3. obsolete "awd" attributes, moving them "feature" list

so end structure this:

def inventory = {     car (make: "subaru", model: "impreza wrx", year: 2010, color: "blue") {         feature ("premium sound")         feature ("brembo brakes")     }     car (make: "jeep", model: "wrangler", year: 2013) {         feature ("awd")         feature ("soft top")     }     // blahblahblah } 

actually, i'm ok going through original structure composing new list (my data isn't huge require in-place editing), how traverse through structure, in first place?

oh, , question of terminology. maybe, googling around wrong keyword... entity defined in code: called "closure" too, or there's different term it?

looking @ inventory, being represented like

@immutable(copywith = true) class car {     string make     string model     int year     string color     boolean awd      list<string> features     list<string> bugs } 

as fine composing new list (and there speaking few reasons not so), can add @immutable annotation. "side effect" can add copywith = true copy method.

the definition of inventory this:

    def inventory = [         new car(             make: "subaru",             model: "impreza wrx",             year: 2010,             color: "blue",             features: ["premium sound", "brembo brakes"],             bugs: ["leaks oil", "needs new transmission"]),         new car(             make: "jeep",             model: "wrangler",             year: 13,             awd: true,             features: ["soft top"],             bugs: ["doesn't start", "flooded"])         ] 

which gives immutable represenation of data. can traverse , change data using collection api. in case:

    def result = inventory         .collect { it.copywith(bugs: []) }         .collect { it.copywith(year:                       it.year < 2000 ? it.year + 2000 : it.year) }         .collect {             if (it.awd) {                  it.copywith(features: it.features + "awd")             } else {                             }  

each transformation applied individually (and there immutable instances flowing through pipeline).


if want remove the empty bugs list and/or awd property, define target class:

class fixedcar {     string make     string model     int year     string color      list<string> features      static fixedcar apply(car car) {         new fixedcar(             make: car.make,             model: car.model,             year: car.year,             color: car.color,             features: car.features)     } } 

and add call collect:

    def result = inventory         .collect { it.copywith(bugs: []) }         .collect { it.copywith(year:                       it.year < 2000 ? it.year + 2000 : it.year) }         .collect {             if (it.awd) {                  it.copywith(features: it.features + "awd")             } else {                             }          }.collect { fixedcar.apply(it) } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -