Search

Ted Vinke's Blog

Java, technology and stuff

Interaction with Slackbot

First interaction I have with slackbot, the interactive tutorial bot, after signing up with Slack – the messaging app for teams. Seems it’s to early in the morning for me to proper read instructions :-)

first-slack

Groovy Weekend – Collections: Grouping And Summing Items

I’ve been working with Groovy for some time now and I think it’s one of the best languages for the JVM.

To show my love for this language I dedicate this weekend to showcase some of the enhancements Groovy has made to working with Lists, Maps and Collections. Some small tidbits – just for fun.

This is the 7th and last installment of the Groovy Weekend Collections Showcase Reel – at least for this weekend :-) which we’ll conclude with grouping, collecting and summing.


sum

sum() is a method on Iterable which ehm,…sums up all items – using the plus() method under the hood.

class Animal {
 String name
 BigDecimal price
 String farmer
 String toString() { name }
}

def animals = []
animals << new Animal(name: "Buttercup", price: 2, farmer: "john")
animals << new Animal(name: "Carmella", price: 5, farmer: "dick")
animals << new Animal(name: "Cinnamon", price: 2, farmer: "dick")

// gather the total price of all animals
assert 9 == animals.sum { it.price }
assert 9 == animals.price.sum()

Putting it all together

// grouping: animals by farmer
def animalsByFarmer = animals.groupBy { it.farmer }
assert "[john:[Buttercup], dick:[Carmella, Cinnamon]]" 
               == animalsByFarmer.toString()

// grouping and summing: total price per farmer
def totalPriceByFarmer = animals
                            .groupBy { it.farmer }
                            .collectEntries { k, v -> 
                                [k, v.price.sum()]
                            }
assert ['john':2, 'dick':7] == totalPriceByFarmer

That’s it – the last edition this weekend! Check out more of Groovy’s methods on Iterable and Collection in the GDK documentation.

No animals were hurt while making these examples. For now, I’ll hope you will enjoy Groovy as much as I do. :-)

Groovy Weekend – Collections: Injecting or Map Reduce

I’ve been working with Groovy for some time now and I think it’s one of the best languages for the JVM.

To show my love for this language I dedicate this weekend to showcase some of the enhancements Groovy has made to working with Lists, Maps and Collections. Some small tidbits – just for fun.

In this 7th installment of the Groovy Weekend Collections Showcase Reel…


inject

inject() is a method on Collection which iterates all elements, passing 1st item to specified closure, injecting results back into closure with 2nd item, etc

First an abstract example:

assert 1 * 2 * 3 * 4 == 
    [1, 2, 3, 4].inject { current, val -> current * val }

And – as you would expect by now – some animals:

class Animal {
    String name
    BigDecimal price
    String farmer
}

def animals = []
animals << new Animal(name: "Buttercup", price: 2, farmer: "john")
animals << new Animal(name: "Carmella", price: 5, farmer: "dick")
animals << new Animal(name: "Cinnamon", price: 2, farmer: "dick")

// gather the total price of all animals, passing initial price of 0
assert 9 == animals.inject(0) { totalPrice, animal -> 
    totalPrice + animal.price 
}
// or...
assert 9 == animals.price.inject(0) { totalPrice, price -> 
    totalPrice + price
}

There are some variants of inject, one which takes an initial value and one which takes the head of the collection as initial value. Check out Groovy’s GDK documentation.

Groovy Weekend – Collections: Removing Matching Items

I’ve been working with Groovy for some time now and I think it’s one of the best languages for the JVM.

To show my love for this language I dedicate this weekend to showcase some of the enhancements Groovy has made to working with Lists, Maps and Collections. Some small tidbits – just for fun.

In this 6th installment of the Groovy Weekend Collections Showcase Reel…


removeAll

removeAll() is a method on Collection which normally gets passed an array of objects to remove. There’s also a variant which accepts a closure – which specifies the condition if an element should be removed.

 
class Animal {
    String name
    int age
    String toString() { properties }
}

def animals = []
animals << new Animal(name: "Buttercup", age: 2)
animals << new Animal(name: "Carmella", age: 5)
animals << new Animal(name: "Cinnamon", age: 2)

// remove all animals younger than 3
animals.removeAll { it.age < 3 }
assert animals.size() == 1
assert animals.first().name == "Carmella"

Next to removing there’s also a retainAll. Check out Groovy’s GDK documentation.

Blog at WordPress.com. | The Baskerville Theme.

Up ↑

Follow

Get every new post delivered to your Inbox.

Join 132 other followers