From 39b96dd2db1de1c8d31ef6ce1c05543e7f690659 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Mon, 2 Sep 2013 12:53:05 +0100 Subject: Highlight code as Groovy, not cpp --- groovy.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'groovy.html.markdown') diff --git a/groovy.html.markdown b/groovy.html.markdown index 1a635e59..e440ef00 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -8,7 +8,7 @@ filename: learngroovy.groovy Groovy - A dynamic language for the Java platform [Read more here.](http://groovy.codehaus.org) -```cpp +```groovy /* Set yourself up: -- cgit v1.2.3 From 68f26804c54280ad86ce639ae428fdf3b670de8b Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Mon, 2 Sep 2013 12:57:29 +0100 Subject: Added a few more examples for List manipulation --- groovy.html.markdown | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'groovy.html.markdown') diff --git a/groovy.html.markdown b/groovy.html.markdown index e440ef00..63cef76b 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -54,15 +54,26 @@ println x //Creating an empty list def technologies = [] -//Add an element to the list -technologies << "Groovy" +/*** Adding a elements to the list ***/ + +// As with Java technologies.add("Grails") + +// Left shift adds, and returns the list +technologies << "Groovy" + +// Add multiple elements technologies.addAll(["Gradle","Griffon"]) -//Remove an element from the list +/*** Removing elements from the list ***/ + +// As with Java technologies.remove("Griffon") -//Iterate over elements of a list +// Subtraction works also +technologies = technologies - 'Grails' + +// Iterate over elements of a list technologies.each { println "Technology: $it"} technologies.eachWithIndex { it, i -> println "$i: $it"} -- cgit v1.2.3 From 6f444bece417a18127782d909a518c91962823c9 Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Mon, 2 Sep 2013 12:59:18 +0100 Subject: Mention mutating with List.sort --- groovy.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'groovy.html.markdown') diff --git a/groovy.html.markdown b/groovy.html.markdown index 63cef76b..135efc0f 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -51,6 +51,7 @@ println x /* Collections and maps */ + //Creating an empty list def technologies = [] @@ -81,9 +82,12 @@ technologies.eachWithIndex { it, i -> println "$i: $it"} technologies.contains('Groovy') technologies.containsAll(['Groovy','Grails']) -//Sort a list +// Sort a list (mutates original list) technologies.sort() +// To sort without mutating original, you can do: +sortedTechnologies = technologies.sort( false ) + //Replace all elements in the list Collections.replaceAll(technologies, 'Gradle', 'gradle') -- cgit v1.2.3 From 95058aea96036fbdb4829d5245f5521541abdf0c Mon Sep 17 00:00:00 2001 From: Tim Yates Date: Mon, 2 Sep 2013 13:02:53 +0100 Subject: Another way of checking List.contains and some headers --- groovy.html.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'groovy.html.markdown') diff --git a/groovy.html.markdown b/groovy.html.markdown index 135efc0f..8fb1b346 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -74,20 +74,33 @@ technologies.remove("Griffon") // Subtraction works also technologies = technologies - 'Grails' +/*** Iterating Lists ***/ + // Iterate over elements of a list technologies.each { println "Technology: $it"} technologies.eachWithIndex { it, i -> println "$i: $it"} +/*** Checking List contents ***/ + //Evaluate if a list contains element(s) (boolean) -technologies.contains('Groovy') +contained = technologies.contains( 'Groovy' ) + +// Or +contained = 'Groovy' in technologies + +// Check for multiple contents technologies.containsAll(['Groovy','Grails']) +/*** Sorting Lists ***/ + // Sort a list (mutates original list) technologies.sort() // To sort without mutating original, you can do: sortedTechnologies = technologies.sort( false ) +/*** Manipulating Lists ***/ + //Replace all elements in the list Collections.replaceAll(technologies, 'Gradle', 'gradle') -- cgit v1.2.3