groovy & grails - lecture 1

  • 640 views
Uploaded on

Why Groovy & Grails (and not Perl anymore) …

Why Groovy & Grails (and not Perl anymore)
Simple types
String
Map & List

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
640
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
22
Comments
1
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • perl 5 was getting stucked by legacy weight %,@,$, $/, $!, @_, $$ and incompressible (back compatibility choices)\n
  • perl 5 was getting stucked by legacy weight %,@,$, $/, $!, @_, $$ and incompressible (back compatibility choices)\n
  • \n
  • \n
  • \n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • rakudo is a total rewriting of perl, as far from perl perl than groovy (almost)\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • At GroovyOne 2004—a gathering of Groovy developers in London—James Strachan gave a keynote address telling the story of how he arrived at the idea of inventing Groovy.\nSome time ago, he and his wife were waiting for a late plane. While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.\n\nit is important to note that developing in a language is totally portable to another (major)\ngap between perl -> groovy small\nyou will use other languages during your life\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • put a upperacase L (not l/one)\nBigDecimal && BigInteger\nmix from java and groovy\n
  • put a upperacase L (not l/one)\nBigDecimal && BigInteger\nmix from java and groovy\n
  • same kind of direction with perl 6\n
  • same kind of direction with perl 6\n
  • same kind of direction with perl 6\n
  • same kind of direction with perl 6\n
  • same kind of direction with perl 6\n
  • \n
  • \n
  • use def my_array\nmy_str=my_str.kkkkk\n
  • i>>1\n
  • \n
  • \n
  • \n
  • \n
  • no quote compulsory\nend ‘,’\nplenty of similtudes between linear array and associative ones\n
  • no quote compulsory\nend ‘,’\nplenty of similtudes between linear array and associative ones\n
  • no quote compulsory\nend ‘,’\nplenty of similtudes between linear array and associative ones\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Perl: Efficiency Oriented ProgrammingLecture 1Master Proteomics & Bioinformatics - University of GenevaAlexandre Masselot - summer 2011
  • 2. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)
  • 3. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)• Excel/mouse data handling is the best path to psychiatric hospital
  • 4. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)• Excel/mouse data handling is the best path to psychiatric hospital• Powerful computers (or even small clusters) are becoming more affordable
  • 5. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)• Excel/mouse data handling is the best path to psychiatric hospital• Powerful computers (or even small clusters) are becoming more affordable• Programming languages and frameworks more popular (scripts, web applications, relational databases)
  • 6. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)• Excel/mouse data handling is the best path to psychiatric hospital• Powerful computers (or even small clusters) are becoming more affordable• Programming languages and frameworks more popular (scripts, web applications, relational databases)• Code is shared (open source culture)
  • 7. ?EOP : goal of the course• Biological experiments produce huge amount of data (proteomics => TB)• Excel/mouse data handling is the best path to psychiatric hospital• Powerful computers (or even small clusters) are becoming more affordable• Programming languages and frameworks more popular (scripts, web applications, relational databases)• Code is shared (open source culture) Goal of the this course: make you efficient in the lab, with a computer at you fingertips
  • 8. ?EOP: topics of this course• Fall: Programming for Bioinformatics I for programming fundamentals
  • 9. ?EOP: topics of this course• Fall: Programming for Bioinformatics I for programming fundamentals• Spring: get some independence and meet real life challenges
  • 10. ?EOP: topics of this course• Fall: Programming for Bioinformatics I for programming fundamentals• Spring: get some independence and meet real life challenges• By the end, you should be able to • deploy your own development environment, • write scripts to analyze data and give the scientist an answer, • interact with other libraries, • build a web application, • build a relational DB model, • have basic skills in software development methodologies.
  • 11. ?EOP: topics of this course• Fall: Programming for Bioinformatics I for programming fundamentals• Spring: get some independence and meet real life challenges• By the end, you should be able to • deploy your own development environment, • write scripts to analyze data and give the scientist an answer, • interact with other libraries, • build a web application, • build a relational DB model, • have basic skills in software development methodologies. Just another tool in the box (yet damned powerful!)
  • 12. Efficiency Oriented Programming• Efficiency = Result/Time
  • 13. Efficiency Oriented Programming• Efficiency = Result/Time• Time: • time spent in programming the tool • time invested in learning the technology • not only computing time (although it is important)
  • 14. Efficiency Oriented Programming• Efficiency = Result/Time• Time: • time spent in programming the tool • time invested in learning the technology • not only computing time (although it is important)• Result: • complexity of the produced code • quality (functionalities, evolution etc.) • versatility
  • 15. EOP: why not Perl (this year)?• “Perl is a language to get your job done” Larry Wall
  • 16. EOP: why not Perl (this year)?• “Perl is a language to get your job done” Larry Wall• To maximize efficiency, Perl has been perfect candidate: • from eighties until now, a constant evolution, • rich and direct syntax, • dynamic community: >7’000 of public modules, often maintained, hight quality (http://search.cpan.org), • free (speech & beer), • almost 100 OS supported (http://www.cpan.org/ports/index.html), • perl is anarchist (and even poetic).
  • 17. EOP: why not Perl (this year)?• Language are species following evolution patterns (no creationism)
  • 18. EOP: why not Perl (this year)?• Language are species following evolution patterns (no creationism)• Thousands of languages exist (http://en.wikipedia.org/wiki/ List_of_programming_languages)
  • 19. EOP: why not Perl (this year)?• Language are species following evolution patterns (no creationism)• Thousands of languages exist (http://en.wikipedia.org/wiki/ List_of_programming_languages)• Recent trends for high level languages: • syntax become more compact (and efficient), • scripting is not incompatible with classic “structured” code (writing and running), • features, culture: cross-over between languages, • language cooperation becomes a standard (parrot, .net)
  • 20. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:
  • 21. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:• perl 5 : a historical choice, anchored in the community (bioperl etc.),
  • 22. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:• perl 5 : a historical choice, anchored in the community (bioperl etc.),• java : structured, safe, heavy,
  • 23. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:• perl 5 : a historical choice, anchored in the community (bioperl etc.),• java : structured, safe, heavy,• python : a versatile language, allying strength from scripts up to object- oriented models
  • 24. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:• perl 5 : a historical choice, anchored in the community (bioperl etc.),• java : structured, safe, heavy,• python : a versatile language, allying strength from scripts up to object- oriented models• perl 6 (rakudo) : a total re-designing of perl, by its original authors, a very appealing option, but not yet a standard
  • 25. EOP: why not Perl (this year)?For general purpose bioinformatics, several nominees:• perl 5 : a historical choice, anchored in the community (bioperl etc.),• java : structured, safe, heavy,• python : a versatile language, allying strength from scripts up to object- oriented models• perl 6 (rakudo) : a total re-designing of perl, by its original authors, a very appealing option, but not yet a standard• groovy (+grails) : a recent (2003) initiative, with powerful scripting manners & tight links with java; grails offer a very light (although powerful) framework for web development, RDBMS and Spring integration
  • 26. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is:•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications•data naturally stored in relational database•good software development practices are encouraged (agile)•powerful IDE available
  • 27. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications•data naturally stored in relational database•good software development practices are encouraged (agile)•powerful IDE available
  • 28. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,
  • 29. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications
  • 30. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications•data naturally stored in relational database
  • 31. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications•data naturally stored in relational database•good software development practices are encouraged (agile)
  • 32. EOP: why not Perl (this year)?There is no “one best language” for everyone and all situations,but our winner (this year) is: Groovy and Grails•compact syntax, either “free” or “controlled”, structured or object-oriented,•modern web applications•data naturally stored in relational database•good software development practices are encouraged (agile)•powerful IDE available
  • 33. Groovy+grails : setup• integrated solution: • install java 6 (sun) sdk • install SpringSource Tool Suite (eclipse) http://www.springsource.com/ products/sts • start STS • help>dashboard, • tab extensions • install groovy and grails• Launch trials environments, with either: • groovysh • groovyConsole
  • 34. Groovy: a first script + string evaluation
  • 35. Groovy: a first script + string evaluationSubroutines calls, parenthesis and ; println (“hello world”); //java-like println “hello world”; //end line with ; println “hello world” //slim fast
  • 36. Groovy: a first script + string evaluationSubroutines calls, parenthesis and ; println (“hello world”); //java-like println “hello world”; //end line with ; println “hello world” //slim fastString evaluation /* double quotes allow string evaluation single quote no evaluation looks perlish, huh? */ def name=‘Mickey Mouse’ println “hello $name” //=> hello Mickey Mouse println ‘hello ‘+name //=> hello Mickey Mouse println ‘hello $name‘ //=> hello $name
  • 37. Defining variable: to type or not to type?It is possible to define variable, without specifying their type def x= 42.4 //dynamic type variable x, //initialized with a float x= ‘ola chico‘ //a string is assigned to the same x x=[‘aa’, ‘bb’, ‘c’] //now an array...
  • 38. Defining variable: to type or not to type?It is possible to define variable, without specifying their type def x= 42.4 //dynamic type variable x, //initialized with a float x= ‘ola chico‘ //a string is assigned to the same x x=[‘aa’, ‘bb’, ‘c’] //now an array...It is generally better to assign a type to a variable (security & readability) int x=42 //static type set to integer x=12.34 // => x=12 (implicit conversion) x=[1, 1, 2, 3, 5] // => error: cannot convert // (GroovyCastException)
  • 39. More on implicit castingWhenever possible groovy compiler tries to cast variable.
  • 40. More on implicit castingWhenever possible groovy compiler tries to cast variable.Why is the following correct (from the compiler point of view)? String x=‘ola chico’ x=23.14 x=[‘a’, ‘b’, ‘c’]
  • 41. More on implicit castingWhenever possible groovy compiler tries to cast variable.Why is the following correct (from the compiler point of view)? String x=‘ola chico’ x=23.14 x=[‘a’, ‘b’, ‘c’]To know the class (the type) of a variable println x.class
  • 42. More on implicit castingWhenever possible groovy compiler tries to cast variable.Why is the following correct (from the compiler point of view)? String x=‘ola chico’ x=23.14 x=[‘a’, ‘b’, ‘c’]To know the class (the type) of a variable println x.classWhat will produce the following statements? int x=1 println x+2 String y=1 println y+2
  • 43. And even more on type conversionCasting can be called explicitly (TIMTOWTDI) def my_str=‘123’ int x=my_str.toInteger()+10000 x=Integer.parseInt(my_str) x=my_str as Integer x=my_str //error!!!
  • 44. And even more on type conversionCasting can be called explicitly (TIMTOWTDI) def my_str=‘123’ int x=my_str.toInteger()+10000 x=Integer.parseInt(my_str) x=my_str as Integer x=my_str //error!!!Or back to String String other_str=x.toString() other_str=x as String other_str=’’ + x //’’ force the casting other_str=”$x”
  • 45. Test the parameters typeImagine we have not statically defined a variable type (argument to function) x=23 if(x in Integer){ // true ... } 23.45 in Double // false 23.45D in Double // true 23.45 in BigDecimal // true ‘pipo’ in String // true
  • 46. Test the parameters typeImagine we have not statically defined a variable type (argument to function) x=23 if(x in Integer){ // true ... } 23.45 in Double // false 23.45D in Double // true 23.45 in BigDecimal // true ‘pipo’ in String // trueMore generally, we can detect numerical values x in Number
  • 47. Test the parameters typeImagine we have not statically defined a variable type (argument to function) x=23 if(x in Integer){ // true ... } 23.45 in Double // false 23.45D in Double // true 23.45 in BigDecimal // true ‘pipo’ in String // trueMore generally, we can detect numerical values x in Numberand later x in Map; x in List
  • 48. Some available scalar types
  • 49. Some available scalar typesMost commons types int //or Integer double //or Double String boolean //boolean b=true
  • 50. Some available scalar typesMost commons types int //or Integer double //or Double String boolean //boolean b=trueAnd more char //single character (‘a’) byte //8 bit signed integer 0..255 short //16 bit signed integer long //64 bit signed integer (123L) float //32 bit floating point (1.23F) double //64 bit floating point (1.23D) BigInteger //large integer (123G) BigDecimal //large float (1.23G)
  • 51. Primitive type or Object?In java, there is a distinction between int and Integer
  • 52. Primitive type or Object?In java, there is a distinction between int and IntegerIn groovy, everything is Object
  • 53. Primitive type or Object?In java, there is a distinction between int and IntegerIn groovy, everything is ObjectSo we can call methods String name=‘Mickey Mouse’ println name.length() // => 12 println name.substring(0,4) // => ‘Mick’ println name.endsWith(‘use’) // => true
  • 54. Primitive type or Object?In java, there is a distinction between int and IntegerIn groovy, everything is ObjectSo we can call methods String name=‘Mickey Mouse’ println name.length() // => 12 println name.substring(0,4) // => ‘Mick’ println name.endsWith(‘use’) // => true“Hidden” methods println name[5] // name.getAt(5) println name[3..5] // name.substring(3,6)
  • 55. Primitive type or Object?In java, there is a distinction between int and IntegerIn groovy, everything is ObjectSo we can call methods String name=‘Mickey Mouse’ println name.length() // => 12 println name.substring(0,4) // => ‘Mick’ println name.endsWith(‘use’) // => true“Hidden” methods println name[5] // name.getAt(5) println name[3..5] // name.substring(3,6)The total list: in groovyConsole, launch Inspector on last evaluatedvariable with ctrl-I -> methods tab (or http://groovy.codehaus.org/groovy-jdk/, or google)
  • 56. OperatorsSome common operations can be written via operators a+b a.plus(b) a/b a.div(b) // 16/3 -> 5.33333333 a%b a.mod(b) // 16%3 -> 1 a++ a.next() // difference with ++a ? a**b a.power(b) // 15**2 -> 225 a>>b a.rightShift(b) a|b a.or(b) // 5|6 -> 7 (101|110 -> 111)
  • 57. OperatorsSome common operations can be written via operators a+b a.plus(b) a/b a.div(b) // 16/3 -> 5.33333333 a%b a.mod(b) // 16%3 -> 1 a++ a.next() // difference with ++a ? a**b a.power(b) // 15**2 -> 225 a>>b a.rightShift(b) a|b a.or(b) // 5|6 -> 7 (101|110 -> 111)Later, we’ll see how these operators can be overridden
  • 58. Exercise 1 : string for fun• Initialize a String with ‘my funny valentine’• what is the length of this string?• assign a boolean to store if ‘fun’ is a substring• replace ‘funny’ by ‘happy’• convert this string to upper case• split this string into a list, with one word per element ([‘MY’, ‘HAPPY’, ‘VALENTINE’])• reverse this list ([‘VALENTINE’, ‘HAPPY’, ‘MY’])
  • 59. Exercise 2 : integer and bit representation• Touching the integer limit • initialize an integer with 123 • what is the maximum value for Integer? the minimum value? • replace it by itself to the power 3 • do the same thing once more. What do you observe?• bit representation • initialize an integer with 103 (binary 1100111) • shift the binary representation by two steps on the right. What operation is it equivalent two? • shift it back to the left. What do you observe?
  • 60. static methodMethod can be bound to a object instance, e.g. : String name=‘pipo’; name.length()They can also be bound to the class itself: String str=‘23.45’ double b=Double.parseDouble(str) // 23.45D Integer.bitCount(103) // 5
  • 61. Other classes?Beside intrinsic scalar value, there are millions of classes defined, andthousands of them are available more or less by default.Example : Random Random my_rnd // -> null my_rnd=new Random() // not null anymore println my_rnd.nextDouble() // pseudo random in [0,1[ 10.times{println my_rnd.nextBoolean()}
  • 62. Exercise 3 : going random• More with Random class • is it possible for your computer to draw random numbers? • what happen if your print my_rnd? • draw 6 number with a gaussian distribution (µ=0, σ2=1) • draw 6 number with a gaussian distribution (µ=170, σ2=25) • draw a classic dice 10 times • draw the sum of two dice 10 times • what happen is you reproduce the same experiment many times? is the results the same? how could have reproducible results?
  • 63. Exercise 4 : have a date• Using the Date class • assign to a variable the current date • print the just the second (60 seconds per minute) value of the current date • assign your variable with one week ago • get the month of one week ago as an integer • is now after yesterday? (this is a boolean question, not an existentialist one) • is now equals to now? always? (how date is coded?)
  • 64. Collective types : List & MapDynamic typing is still possible
  • 65. Collective types : List & MapDynamic typing is still possibleFor List (or array) def fibonacci=[1,1,2,3,5,8,13] fibonacci=[] //empty list
  • 66. Collective types : List & MapDynamic typing is still possibleFor List (or array) def fibonacci=[1,1,2,3,5,8,13] fibonacci=[] //empty listOr Map (or HashMap, or associative array) def medals = [us:36, germany:30, canada:26, norway:23] medals = [:] //empty map def medalsDetails=[ us:[gold:9, silver:15, bronze:13], germany:[gold:10, silver:13, bronze:7], canada:[gold:14, silver:7, bronze:5], norway:[gold:9, silver:8, bronze:6], ]
  • 67. Collective types (cont’d)Or static type List<Integer> fibonacci Map<String, Integer> medals Map<String, Map<String, Integer>> medal_details
  • 68. Collective types (cont’d)Or static type List<Integer> fibonacci Map<String, Integer> medals Map<String, Map<String, Integer>> medal_detailsList access fibonacci[0] // first element fibonacci[-1] // fibonacci[fibonacci.size()-1] fibonacci[2..4] // [2, 3, 5]
  • 69. Collective types (cont’d)Or static type List<Integer> fibonacci Map<String, Integer> medals Map<String, Map<String, Integer>> medal_detailsList access fibonacci[0] // first element fibonacci[-1] // fibonacci[fibonacci.size()-1] fibonacci[2..4] // [2, 3, 5]Map access medals.canada == medals[‘canada’] medal_details.norway.gold == medals_details[‘norway’][‘gold’]
  • 70. Collective types (cont’d)
  • 71. Collective types (cont’d)Add or modify fibonacci[7]=21 //add a new element medals.canada.gold=97 //modify medals.us.gold-- //anti doping control...
  • 72. Collective types (cont’d)Add or modify fibonacci[7]=21 //add a new element medals.canada.gold=97 //modify medals.us.gold-- //anti doping control...Other methods fibonnacci.size() medal_details.germany.size() medal_details.remove(‘germany’)
  • 73. Collective types (cont’d)Add or modify fibonacci[7]=21 //add a new element medals.canada.gold=97 //modify medals.us.gold-- //anti doping control...Other methods fibonnacci.size() medal_details.germany.size() medal_details.remove(‘germany’)Operators are also available fibonacci << 34 //push 34 at the end fibonacci - 1 //remove all values 1
  • 74. Exercise 5: arrays• fills an array with a few values • what is the difference between array+12, array<<12 and array+=12? • print the sum, the maximum value • store the reversed array into another one • sort the array with increasing values. With decreasing values.• A last little one • remember the 10.times{...} syntax we saw previously • put [1, 1] in a fibo array, then fill this array with the 20 next value os a Fibonacci suite, where fibo[n+1]=fibo[n]+fibo[n-1]