Here goes my contribution to HelloScript project by Praseed Pai.
GitHub: https://github.com/praseedpai/HelloScript_files/blob/master/Groovy/HelloScript.groovy
Source Code:
001
/**
002 * HelloScript.groovy
003 */
004
005 // A Simple Console Output
006
007 println "Hello World"
008
009 // Some more
010 print ("Hello World")
011 System.out.println("K-Mug"); // Java style also works
012
013 // Declare variables
014 def a = 100 // a number, def is about scope
015 b = 100; // another number
016 year = "Twenty Twenty" // a string
017
018 // String formatting & interpolation
019 println "This is ${a}, ${b}, and " + year;
020
021 // Reassign a variable
022 year = 2008; // dynamic typing
023
024 // if-else
025 if (year > 2008)
026 println "Welcome to the future - yes, we have flying cars!"
027 else if (year < 2008)
028 printLN "The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!"
029 else
030 println "Anything wrong with your time machine? You have not gone anywhere, kiddo."
031
032
033 // Range based for loop
034 for (i in 0..3) {
035 println "$i Hi there!"
036 }
037
038 // Copying range value to a variable
039 range_array = (0..10)
040 print range_array;
041
042 // Array demo
043 rules = ['Do no harm','Obey','Continue Living']
044 println rules;
045
046 rules << "Be Honest" // add one more item
047 println rules
048
049 // Array demo, with mixed types
050 more_rules = ['Do no harm','Obey','Continue Living', 404, 403, 500, 100f]
051 println more_rules;
052
053 // Loop through Array
054 i = 0
055 while (i < rules.size()) {
056 print "Rule " + (i + 1) + " : " + rules[i]
057 i += 1
058 }
059
060 println() // just a newline
061
062 // Associating array
063 associated = [
064 'hello' : 'world',
065 'foo' : 'bar',
066 'lorem' : 'ipsum'
067 ]
068
069 for (ele in associated) {
070 print ele.key + " : " + ele.value
071 }
072
073 println() // just a newline
074
075 // Example of a Nested Loop
076 // To calculate Pythagorean Triplets
077 n = 10
078 println "-------------------------------------"
079 for (va in (1..n)) {
080 for (vb in (va..n)) {
081 Integer c_square = va**2 + vb**2
082 Integer vc = Math.sqrt(c_square)
083 if ((c_square - vc**2) == 0) {
084 println va + " " + vb + " " + vc
085 }
086 }
087 }
088 println "----------------------------------"
089
090 // Iterating over a list using range and size
091 println "-------------------------------------"
092 fibonacci = [0,1,1,2,3,5,8,13,21]
093 for (i in (0..fibonacci.size()-1)) {
094 println i + " " + fibonacci[i]
095 }
096 println "---------------------------------------"
097
098 // Parsing a line - split and join
099 csv_values = "hello,world,how,are,you".split(",")
100 println csv_values;
101 println csv_values.join(":")
102
103 // A Single Argument Function
104 def hello(name) {
105 return "Hello ${name}!"
106 }
107 println hello("Praveen")
108
109 // A simple class
110 class Movie {
111 String name = ""
112 Integer rating = 0
113
114 def Movie(movieName) {
115 this.name = movieName
116 this.rateMovie()
117 }
118
119 def rateMovie() {
120 this.rating = (this.name.length() % 10) + 1 // IMDBs rating algorithm. True story!
121 }
122 def printMovieDetails() {
123 println "Movie : " + this.name
124 println "Rating : " + '*'.multiply(this.rating) + "(" + this.rating +")"
125 }
126 }
127
128 // Create the Object
129 ncfom = new Movie("New Country for Old Men"); // It's a sequel!
130 ncfom.printMovieDetails()
131
132 // Closures in action
133 myList = ["Hello","My","World","What's","Up"]
134 myList.each {
135 print it + "-" // it is special
136 }
137
138 // Multiplication table
139 (1..10).each {
140 println "${it} x 2 = ${it*2}"
141 }
142
143 println() // just a newline
144
145 // Tuples
146 myTuple = new Tuple(1, 'two', 3, 'four')
147 println myTuple
148
149 // File IO - Create a new file
150 myFile = new File("myfile.txt");
151 myFile.text = "Hello World! - from the file"
152 myFile.createNewFile()
153
154 // File IO - Read from file
155 println myFile.text
156
157 // File IO - With closure
158 myFile.eachLine {
159 l -> println l.toUpperCase()
160 }
161
162 // Regular Expressions
163 import java.util.regex.Pattern
164 Pattern pattern = ~/World/
165 str = "Hello World, this is Universe, not your World!"
166 println "Found " + pattern.matcher(str).size() + " mathes."
View in GitHub | Made with wp-showgithubfile plugin [Code generated by wp-ShowGithubFile plugin]
002 * HelloScript.groovy
003 */
004
005 // A Simple Console Output
006
007 println "Hello World"
008
009 // Some more
010 print ("Hello World")
011 System.out.println("K-Mug"); // Java style also works
012
013 // Declare variables
014 def a = 100 // a number, def is about scope
015 b = 100; // another number
016 year = "Twenty Twenty" // a string
017
018 // String formatting & interpolation
019 println "This is ${a}, ${b}, and " + year;
020
021 // Reassign a variable
022 year = 2008; // dynamic typing
023
024 // if-else
025 if (year > 2008)
026 println "Welcome to the future - yes, we have flying cars!"
027 else if (year < 2008)
028 printLN "The past - please don't change anything. Don't step on any butterflies. And for the sake of all thats good and holy, stay away from your parents!"
029 else
030 println "Anything wrong with your time machine? You have not gone anywhere, kiddo."
031
032
033 // Range based for loop
034 for (i in 0..3) {
035 println "$i Hi there!"
036 }
037
038 // Copying range value to a variable
039 range_array = (0..10)
040 print range_array;
041
042 // Array demo
043 rules = ['Do no harm','Obey','Continue Living']
044 println rules;
045
046 rules << "Be Honest" // add one more item
047 println rules
048
049 // Array demo, with mixed types
050 more_rules = ['Do no harm','Obey','Continue Living', 404, 403, 500, 100f]
051 println more_rules;
052
053 // Loop through Array
054 i = 0
055 while (i < rules.size()) {
056 print "Rule " + (i + 1) + " : " + rules[i]
057 i += 1
058 }
059
060 println() // just a newline
061
062 // Associating array
063 associated = [
064 'hello' : 'world',
065 'foo' : 'bar',
066 'lorem' : 'ipsum'
067 ]
068
069 for (ele in associated) {
070 print ele.key + " : " + ele.value
071 }
072
073 println() // just a newline
074
075 // Example of a Nested Loop
076 // To calculate Pythagorean Triplets
077 n = 10
078 println "-------------------------------------"
079 for (va in (1..n)) {
080 for (vb in (va..n)) {
081 Integer c_square = va**2 + vb**2
082 Integer vc = Math.sqrt(c_square)
083 if ((c_square - vc**2) == 0) {
084 println va + " " + vb + " " + vc
085 }
086 }
087 }
088 println "----------------------------------"
089
090 // Iterating over a list using range and size
091 println "-------------------------------------"
092 fibonacci = [0,1,1,2,3,5,8,13,21]
093 for (i in (0..fibonacci.size()-1)) {
094 println i + " " + fibonacci[i]
095 }
096 println "---------------------------------------"
097
098 // Parsing a line - split and join
099 csv_values = "hello,world,how,are,you".split(",")
100 println csv_values;
101 println csv_values.join(":")
102
103 // A Single Argument Function
104 def hello(name) {
105 return "Hello ${name}!"
106 }
107 println hello("Praveen")
108
109 // A simple class
110 class Movie {
111 String name = ""
112 Integer rating = 0
113
114 def Movie(movieName) {
115 this.name = movieName
116 this.rateMovie()
117 }
118
119 def rateMovie() {
120 this.rating = (this.name.length() % 10) + 1 // IMDBs rating algorithm. True story!
121 }
122 def printMovieDetails() {
123 println "Movie : " + this.name
124 println "Rating : " + '*'.multiply(this.rating) + "(" + this.rating +")"
125 }
126 }
127
128 // Create the Object
129 ncfom = new Movie("New Country for Old Men"); // It's a sequel!
130 ncfom.printMovieDetails()
131
132 // Closures in action
133 myList = ["Hello","My","World","What's","Up"]
134 myList.each {
135 print it + "-" // it is special
136 }
137
138 // Multiplication table
139 (1..10).each {
140 println "${it} x 2 = ${it*2}"
141 }
142
143 println() // just a newline
144
145 // Tuples
146 myTuple = new Tuple(1, 'two', 3, 'four')
147 println myTuple
148
149 // File IO - Create a new file
150 myFile = new File("myfile.txt");
151 myFile.text = "Hello World! - from the file"
152 myFile.createNewFile()
153
154 // File IO - Read from file
155 println myFile.text
156
157 // File IO - With closure
158 myFile.eachLine {
159 l -> println l.toUpperCase()
160 }
161
162 // Regular Expressions
163 import java.util.regex.Pattern
164 Pattern pattern = ~/World/
165 str = "Hello World, this is Universe, not your World!"
166 println "Found " + pattern.matcher(str).size() + " mathes."