SlideShare uma empresa Scribd logo
1 de 73
Groovy: Efficiency Oriented Programming
Lecture 2
Master Proteomics & Bioinformatics - University of Geneva
Alexandre Masselot - summer 2010
Contents

• Eclipse IDE basics
• Assertions
• Closures
• I/O
• Functions
• Control structures
Eclipse IDE

• Eclipse is mainly known as a versatile Integrated Development
  Environment (http://eclipse.org) although it can be much more
Eclipse IDE

• Eclipse is mainly known as a versatile Integrated Development
  Environment (http://eclipse.org) although it can be much more
• Eclipse IDE itself is a naked framework, enriched by plugins
Eclipse IDE

• Eclipse is mainly known as a versatile Integrated Development
  Environment (http://eclipse.org) although it can be much more
• Eclipse IDE itself is a naked framework, enriched by plugins
• We will use the prepackaged Springsource Tool Suite (http://
  www.springsource.com/products/sts)
  • start STS
     • help > dashboard,
     • tab extensions

     • install groovy and grails
     • restart eclipse
Eclipse IDE

• Eclipse is mainly known as a versatile Integrated Development
  Environment (http://eclipse.org) although it can be much more
• Eclipse IDE itself is a naked framework, enriched by plugins
• We will use the prepackaged Springsource Tool Suite (http://
  www.springsource.com/products/sts)
  • start STS
     • help > dashboard,
     • tab extensions

     • install groovy and grails
     • restart eclipse
• Plenty of other plugins can be installed Help > Install new software
Eclipse IDE : a super simple setup

• starting eclipse => run into a workspace, i.e. a disk directory
Eclipse IDE : a super simple setup

• starting eclipse => run into a workspace, i.e. a disk directory

• one workspace hosts projects (sub directory strcture)
Eclipse IDE : a super simple setup

• starting eclipse => run into a workspace, i.e. a disk directory

• one workspace hosts projects (sub directory strcture)
• create a project New > Project > Groovy
Eclipse IDE : a super simple setup

• starting eclipse => run into a workspace, i.e. a disk directory

• one workspace hosts projects (sub directory strcture)
• create a project New > Project > Groovy
• You can change the workspace when working for totally different aspect of
  a project (e.g. one for practicals, one for a more lab internship)
Eclipse IDE : a super simple setup

• a project contains several directories
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
   • bin/ where the compiler write machine ready files
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
   • bin/ where the compiler write machine ready files

   • test/ where the test files (will) resides
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
   • bin/ where the compiler write machine ready files

   • test/ where the test files (will) resides
   • lib/ etc.
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
   • bin/ where the compiler write machine ready files

   • test/ where the test files (will) resides
   • lib/ etc.
• In the src directory, you can create packages. Package names are
  delimiteid with dot e.g. mpb.practicals.lec1
Eclipse IDE : a super simple setup

• a project contains several directories
   • src/ for the source file (the code you type)
   • bin/ where the compiler write machine ready files

   • test/ where the test files (will) resides
   • lib/ etc.
• In the src directory, you can create packages. Package names are
  delimiteid with dot e.g. mpb.practicals.lec1

• In practice, you will find a directory src/mpb/praticals/lec1/
Eclipse IDE : a super simple setup
Eclipse IDE : a super simple setup


• Then, you can create a new script : New > File > MyScript.groovy
Eclipse IDE : a super simple setup


• Then, you can create a new script : New > File > MyScript.groovy
• Run the script with Right button > Run as > groovy script
7
How do you know that your code works?




                                        8
Assertions
Assertions


To check a code validity, one solution is to call print statements
 List l=[1, 2, 7, 4]
 def x=l.max()
 println “max is $x”
Assertions


To check a code validity, one solution is to call print statements
 List l=[1, 2, 7, 4]
 def x=l.max()
 println “max is $x”

Relies on human reading...
Assertions
Assertions


Use assertions that will clearly report failure if any, and be silent if none
 assert x == 7
Assertions


Use assertions that will clearly report failure if any, and be silent if none
 assert x == 7
Any boolean value can be tested
 assert [1, 7, 4] == l-2
 assert “my funny valentine” == my_song
Assertions


Use assertions that will clearly report failure if any, and be silent if none
 assert x == 7
Any boolean value can be tested
 assert [1, 7, 4] == l-2
 assert “my funny valentine” == my_song

assert statement are heavily used in test driven programming
assert   report only on error




                                11
write code




             12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
write code   check it works




                              12
Closures



“Let’s start with a simple definition of closures[...]. A closure is a piece of code
wrapped up as an object[...]. It’s a normal object in that you can pass a
reference to it around just as you can a reference to any other object”

                        (Groovy in Action - 5.1 A gentle introduction to closures)
Closures



“Let’s start with a simple definition of closures[...]. A closure is a piece of code
wrapped up as an object[...]. It’s a normal object in that you can pass a
reference to it around just as you can a reference to any other object”

                        (Groovy in Action - 5.1 A gentle introduction to closures)

Closure is a master feature of the groovy language. Although it can be used
in complex situations, closure are also part of daily programming.
Closures


It can be seen as a method attached to an object
 List l=[1, 1, 2, 3, 5, 8, 13]
 l.each{println it}            //it is the default iterator
Closures


It can be seen as a method attached to an object
 List l=[1, 1, 2, 3, 5, 8, 13]
 l.each{println it}            //it is the default iterator
The iterator can also be named
 l.each{myVal -> println “value $myVal”}
 “my funny valentine”.each{println $it} // -> m
                                         //   y ...
Closures                                         (cont’d)

Much more closures are available on list
 l.eachWithIndex{val, i -> println “list[$i]=$val”}
Closures                                           (cont’d)

Much more closures are available on list
 l.eachWithIndex{val, i -> println “list[$i]=$val”}
Find even values
 l.findAll{ it%2 == 0}              // -> [2, 8]
Closures                                                 (cont’d)

Much more closures are available on list
 l.eachWithIndex{val, i -> println “list[$i]=$val”}
Find even values
 l.findAll{ it%2 == 0}              // -> [2, 8]
Make global boolean test
 l.every{ it < 20 }                 // -> true (all values are <20)
 l.any{ it < 0 }                    // -> false (none is negative)
Closures                                                 (cont’d)

Much more closures are available on list
 l.eachWithIndex{val, i -> println “list[$i]=$val”}
Find even values
 l.findAll{ it%2 == 0}              // -> [2, 8]
Make global boolean test
 l.every{ it < 20 }                 // -> true (all values are <20)
 l.any{ it < 0 }                    // -> false (none is negative)
Transform
 l.collect{ it*10}                  // [10, 10, 20 , 30, 50, ...]
Closures                                                 (cont’d)

Much more closures are available on list
 l.eachWithIndex{val, i -> println “list[$i]=$val”}
Find even values
 l.findAll{ it%2 == 0}              // -> [2, 8]
Make global boolean test
 l.every{ it < 20 }                 // -> true (all values are <20)
 l.any{ it < 0 }                    // -> false (none is negative)
Transform
 l.collect{ it*10}                  // [10, 10, 20 , 30, 50, ...]
Or even piped
 l.findAll{ it%2 == 0}.collect{ it*10 } // -> [20, 80]
Closures (on map)

Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’),
                          ‘simone’:new Date(‘4/2/1985’),
                          ‘birgit’:new Date(’12/6/1988’)]
Closures (on map)

 Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’),
                           ‘simone’:new Date(‘4/2/1985’),
                           ‘birgit’:new Date(’12/6/1988’)]
A simple loop
 birthdays.each{
      println “${it.key} born in “ + it.value.year
 }
Closures (on map)

 Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’),
                           ‘simone’:new Date(‘4/2/1985’),
                           ‘birgit’:new Date(’12/6/1988’)]
A simple loop
 birthdays.each{
      println “${it.key} born in “ + it.value.year
 }
Or with name parameters
 birthday.each{name, date -> println “$name : $date”}
Closures (on map)

 Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’),
                           ‘simone’:new Date(‘4/2/1985’),
                           ‘birgit’:new Date(’12/6/1988’)]
A simple loop
 birthdays.each{
      println “${it.key} born in “ + it.value.year
 }
Or with name parameters
 birthday.each{name, date -> println “$name : $date”}
Sort on the month order
 birthdays.sort{it.value.month}
          .each{println “${it.key} born in “ + it.value}
I/O : reading standard input

Without any connection to outside, a script is soon meaningless...
I/O : reading standard input

Without any connection to outside, a script is soon meaningless...
Reading can be done from stdin (standard input)
 System.in.eachLine{ ... }           // loop on all line piped in
I/O : reading standard input

Without any connection to outside, a script is soon meaningless...
Reading can be done from stdin (standard input)
 System.in.eachLine{ ... }           // loop on all line piped in

Or interactively
 Scanner stdin=new Scanner(System.in)
 int i=stdin.nextInt()
 stdin.<CTRL-space>
I/O: reading from Files

File myFile=new File(“path/to/my/file”)
I/O: reading from Files

 File myFile=new File(“path/to/my/file”)
Loop through the lines
 myFile.eachLine{...}

 myFile.splitEachLine(/s+/){
    // it is an array with all
    // the elements of the current line
 }
I/O: reading from Files

 File myFile=new File(“path/to/my/file”)
Loop through the lines
 myFile.eachLine{...}

 myFile.splitEachLine(/s+/){
    // it is an array with all
    // the elements of the current line
 }
Or get the total text at once
 myFile.getText()
I/O: reading from Files

 File myFile=new File(“path/to/my/file”)
Loop through the lines
 myFile.eachLine{...}

 myFile.splitEachLine(/s+/){
    // it is an array with all
    // the elements of the current line
 }
Or get the total text at once
 myFile.getText()
Temporary file are often necessary
 File myTmpFile=File.createTempFile(‘prefix’, ‘.suf’)
 myTmpFile.deleteOnExit()
Functions

Function is a piece of code that takes argument and returns a value (like a
sub in perl)
Functions

Function is a piece of code that takes argument and returns a value (like a
sub in perl)

Parameters can be statically or dynamically typed
 int increment(x, i){
   return x+i
 }
 int j=3
 println increment(j, 5)               // -> 8
Functions
Functions


Depending on argument type, the method is guessed
 def increment(int x)   {return x + 1 }
 def increment(double x){return x + 0.1}

 println increment(3)                   // -> 4
 println increment(3.0)                 // -> 3.1
Functions


Depending on argument type, the method is guessed
 def increment(int x)   {return x + 1 }
 def increment(double x){return x + 0.1}

 println increment(3)                   // -> 4
 println increment(3.0)                 // -> 3.1
Functions
Functions



def increment(List l)   {l << 1 }
def increment(String s){return s + 1}

println increment([3, 4])       // -> [3, 4, 1]
println increment(“abcd”)       // -> “abcd1”
Functions                                               (cont’d)

Number of arguments induces the function called
 int increment (x, i){ return x+i }
 int increment (x)   { return x+1 }

 println increment(3)                         // -> 4
 println increment(3, 4)                      // -> 7
More concisely, parameters can be defined by default
 int increment(x, i=1){              // if no second arg => i=1
   return x+i
 }

 println increment(3, 5)                      // -> 8
 println increment(3)                         // -> 4
Functions                                               (cont’d)

Number of arguments induces the function called
 int increment (x, i){ return x+i }
 int increment (x)   { return x+1 }

 println increment(3)                         // -> 4
 println increment(3, 4)                      // -> 7
More concisely, parameters can be defined by default
 int increment(x, i=1){              // if no second arg => i=1
   return x+i
 }

 println increment(3, 5)                      // -> 8
 println increment(3)                         // -> 4
Functions                                               (cont’d)

Number of arguments induces the function called
 int increment (x, i){ return x+i }
 int increment (x)   { return x+1 }

 println increment(3)                         // -> 4
 println increment(3, 4)                      // -> 7
More concisely, parameters can be defined by default
 int increment(x, i=1){              // if no second arg => i=1
   return x+i
 }

 println increment(3, 5)                      // -> 8
 println increment(3)                         // -> 4
Functions                                               (cont’d)

Number of arguments induces the function called
 int increment (x, i){ return x+i }
 int increment (x)   { return x+1 }

 println increment(3)                         // -> 4
 println increment(3, 4)                      // -> 7
More concisely, parameters can be defined by default
 int increment(x, i=1){              // if no second arg => i=1
   return x+i
 }

 println increment(3, 5)                      // -> 8
 println increment(3)                         // -> 4
Functions                                           (cont’d)

We can always use a Map with for named parameters

 int increment(params){
      return (params.x?:0)     + // ?:0 0 if params.x false
             (params.plus?:0) -
             (params.minus?:0)
 }
 increment(x:3, plus:4)         // -> 7
Functions                                                    (cont’d)

We can always use a Map with for named parameters

 int increment(params){
      return (params.x?:0)     + // ?:0 0 if params.x false
             (params.plus?:0) -
             (params.minus?:0)
 }
 increment(x:3, plus:4)         // -> 7
Method described fully with map arguments will be extensively used when
calling action from url

Mais conteúdo relacionado

Mais procurados

Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Kenji Tanaka
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickHermann Hueck
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous phpWim Godden
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHPWim Godden
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Workhorse Computing
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)Nikita Popov
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHPThomas Weinert
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challengervanphp
 
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webclkao
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Susan Potter
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Phil Calçado
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11Combell NV
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016Manoj Kumar
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2osfameron
 

Mais procurados (20)

Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -Stubる - Mockingjayを使ったHTTPクライアントのテスト -
Stubる - Mockingjayを使ったHTTPクライアントのテスト -
 
Implementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with SlickImplementing a many-to-many Relationship with Slick
Implementing a many-to-many Relationship with Slick
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
 
groovy & grails - lecture 6
groovy & grails - lecture 6groovy & grails - lecture 6
groovy & grails - lecture 6
 
The promise of asynchronous php
The promise of asynchronous phpThe promise of asynchronous php
The promise of asynchronous php
 
The promise of asynchronous PHP
The promise of asynchronous PHPThe promise of asynchronous PHP
The promise of asynchronous PHP
 
Little Big Ruby
Little Big RubyLittle Big Ruby
Little Big Ruby
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHP
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
 
AnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time webAnyMQ, Hippie, and the real-time web
AnyMQ, Hippie, and the real-time web
 
Ggug spock
Ggug spockGgug spock
Ggug spock
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
 
Cli the other SAPI confoo11
Cli the other SAPI confoo11Cli the other SAPI confoo11
Cli the other SAPI confoo11
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 

Semelhante a groovy & grails - lecture 2

Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Codemotion
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentationManav Prasad
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scriptsMichael Boelen
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developersStoyan Stefanov
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Coxlachie
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl TechniquesDave Cross
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Davide Cerbo
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovyIsuru Samaraweera
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala MakeoverGarth Gilmour
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?osfameron
 

Semelhante a groovy & grails - lecture 2 (20)

Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
 
Ruby
RubyRuby
Ruby
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Lets make better scripts
Lets make better scriptsLets make better scripts
Lets make better scripts
 
JavaScript for PHP developers
JavaScript for PHP developersJavaScript for PHP developers
JavaScript for PHP developers
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Blocks by Lachs Cox
Blocks by Lachs CoxBlocks by Lachs Cox
Blocks by Lachs Cox
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Shell Script
Shell ScriptShell Script
Shell Script
 
Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)Kotlin: forse è la volta buona (Trento)
Kotlin: forse è la volta buona (Trento)
 
Groovy unleashed
Groovy unleashed Groovy unleashed
Groovy unleashed
 
Introductionto fp with groovy
Introductionto fp with groovyIntroductionto fp with groovy
Introductionto fp with groovy
 
Getting testy with Perl
Getting testy with PerlGetting testy with Perl
Getting testy with Perl
 
The Great Scala Makeover
The Great Scala MakeoverThe Great Scala Makeover
The Great Scala Makeover
 
Metadata-driven Testing
Metadata-driven TestingMetadata-driven Testing
Metadata-driven Testing
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 

Mais de Alexandre Masselot

Offshoring software development in Switzerland: You can do it
Offshoring software development in Switzerland: You can do itOffshoring software development in Switzerland: You can do it
Offshoring software development in Switzerland: You can do itAlexandre Masselot
 
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data Stack
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data StackDev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data Stack
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data StackAlexandre Masselot
 
Swiss Transport in Real Time: Tribulations in the Big Data Stack
Swiss Transport in Real Time: Tribulations in the Big Data StackSwiss Transport in Real Time: Tribulations in the Big Data Stack
Swiss Transport in Real Time: Tribulations in the Big Data StackAlexandre Masselot
 

Mais de Alexandre Masselot (9)

Offshoring software development in Switzerland: You can do it
Offshoring software development in Switzerland: You can do itOffshoring software development in Switzerland: You can do it
Offshoring software development in Switzerland: You can do it
 
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data Stack
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data StackDev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data Stack
Dev Wednesday - Swiss Transport in Real Time: Tribulations in the Big Data Stack
 
Swiss Transport in Real Time: Tribulations in the Big Data Stack
Swiss Transport in Real Time: Tribulations in the Big Data StackSwiss Transport in Real Time: Tribulations in the Big Data Stack
Swiss Transport in Real Time: Tribulations in the Big Data Stack
 
groovy & grails - lecture 8
groovy & grails - lecture 8groovy & grails - lecture 8
groovy & grails - lecture 8
 
groovy & grails - lecture 10
groovy & grails - lecture 10groovy & grails - lecture 10
groovy & grails - lecture 10
 
groovy & grails - lecture 1
groovy & grails - lecture 1groovy & grails - lecture 1
groovy & grails - lecture 1
 
groovy & grails - lecture 13
groovy & grails - lecture 13groovy & grails - lecture 13
groovy & grails - lecture 13
 
groovy & grails - lecture 9
groovy & grails - lecture 9groovy & grails - lecture 9
groovy & grails - lecture 9
 
groovy & grails - lecture 7
groovy & grails - lecture 7groovy & grails - lecture 7
groovy & grails - lecture 7
 

Último

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

groovy & grails - lecture 2

  • 1. Groovy: Efficiency Oriented Programming Lecture 2 Master Proteomics & Bioinformatics - University of Geneva Alexandre Masselot - summer 2010
  • 2. Contents • Eclipse IDE basics • Assertions • Closures • I/O • Functions • Control structures
  • 3. Eclipse IDE • Eclipse is mainly known as a versatile Integrated Development Environment (http://eclipse.org) although it can be much more
  • 4. Eclipse IDE • Eclipse is mainly known as a versatile Integrated Development Environment (http://eclipse.org) although it can be much more • Eclipse IDE itself is a naked framework, enriched by plugins
  • 5. Eclipse IDE • Eclipse is mainly known as a versatile Integrated Development Environment (http://eclipse.org) although it can be much more • Eclipse IDE itself is a naked framework, enriched by plugins • We will use the prepackaged Springsource Tool Suite (http:// www.springsource.com/products/sts) • start STS • help > dashboard, • tab extensions • install groovy and grails • restart eclipse
  • 6. Eclipse IDE • Eclipse is mainly known as a versatile Integrated Development Environment (http://eclipse.org) although it can be much more • Eclipse IDE itself is a naked framework, enriched by plugins • We will use the prepackaged Springsource Tool Suite (http:// www.springsource.com/products/sts) • start STS • help > dashboard, • tab extensions • install groovy and grails • restart eclipse • Plenty of other plugins can be installed Help > Install new software
  • 7. Eclipse IDE : a super simple setup • starting eclipse => run into a workspace, i.e. a disk directory
  • 8. Eclipse IDE : a super simple setup • starting eclipse => run into a workspace, i.e. a disk directory • one workspace hosts projects (sub directory strcture)
  • 9. Eclipse IDE : a super simple setup • starting eclipse => run into a workspace, i.e. a disk directory • one workspace hosts projects (sub directory strcture) • create a project New > Project > Groovy
  • 10. Eclipse IDE : a super simple setup • starting eclipse => run into a workspace, i.e. a disk directory • one workspace hosts projects (sub directory strcture) • create a project New > Project > Groovy • You can change the workspace when working for totally different aspect of a project (e.g. one for practicals, one for a more lab internship)
  • 11. Eclipse IDE : a super simple setup • a project contains several directories
  • 12. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type)
  • 13. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type) • bin/ where the compiler write machine ready files
  • 14. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type) • bin/ where the compiler write machine ready files • test/ where the test files (will) resides
  • 15. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type) • bin/ where the compiler write machine ready files • test/ where the test files (will) resides • lib/ etc.
  • 16. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type) • bin/ where the compiler write machine ready files • test/ where the test files (will) resides • lib/ etc. • In the src directory, you can create packages. Package names are delimiteid with dot e.g. mpb.practicals.lec1
  • 17. Eclipse IDE : a super simple setup • a project contains several directories • src/ for the source file (the code you type) • bin/ where the compiler write machine ready files • test/ where the test files (will) resides • lib/ etc. • In the src directory, you can create packages. Package names are delimiteid with dot e.g. mpb.practicals.lec1 • In practice, you will find a directory src/mpb/praticals/lec1/
  • 18. Eclipse IDE : a super simple setup
  • 19. Eclipse IDE : a super simple setup • Then, you can create a new script : New > File > MyScript.groovy
  • 20. Eclipse IDE : a super simple setup • Then, you can create a new script : New > File > MyScript.groovy • Run the script with Right button > Run as > groovy script
  • 21. 7
  • 22. How do you know that your code works? 8
  • 24. Assertions To check a code validity, one solution is to call print statements List l=[1, 2, 7, 4] def x=l.max() println “max is $x”
  • 25. Assertions To check a code validity, one solution is to call print statements List l=[1, 2, 7, 4] def x=l.max() println “max is $x” Relies on human reading...
  • 27. Assertions Use assertions that will clearly report failure if any, and be silent if none assert x == 7
  • 28. Assertions Use assertions that will clearly report failure if any, and be silent if none assert x == 7 Any boolean value can be tested assert [1, 7, 4] == l-2 assert “my funny valentine” == my_song
  • 29. Assertions Use assertions that will clearly report failure if any, and be silent if none assert x == 7 Any boolean value can be tested assert [1, 7, 4] == l-2 assert “my funny valentine” == my_song assert statement are heavily used in test driven programming
  • 30. assert report only on error 11
  • 32. write code check it works 12
  • 33. write code check it works 12
  • 34. write code check it works 12
  • 35. write code check it works 12
  • 36. write code check it works 12
  • 37. write code check it works 12
  • 38. write code check it works 12
  • 39. write code check it works 12
  • 40. write code check it works 12
  • 41. Closures “Let’s start with a simple definition of closures[...]. A closure is a piece of code wrapped up as an object[...]. It’s a normal object in that you can pass a reference to it around just as you can a reference to any other object” (Groovy in Action - 5.1 A gentle introduction to closures)
  • 42. Closures “Let’s start with a simple definition of closures[...]. A closure is a piece of code wrapped up as an object[...]. It’s a normal object in that you can pass a reference to it around just as you can a reference to any other object” (Groovy in Action - 5.1 A gentle introduction to closures) Closure is a master feature of the groovy language. Although it can be used in complex situations, closure are also part of daily programming.
  • 43. Closures It can be seen as a method attached to an object List l=[1, 1, 2, 3, 5, 8, 13] l.each{println it} //it is the default iterator
  • 44. Closures It can be seen as a method attached to an object List l=[1, 1, 2, 3, 5, 8, 13] l.each{println it} //it is the default iterator The iterator can also be named l.each{myVal -> println “value $myVal”} “my funny valentine”.each{println $it} // -> m // y ...
  • 45. Closures (cont’d) Much more closures are available on list l.eachWithIndex{val, i -> println “list[$i]=$val”}
  • 46. Closures (cont’d) Much more closures are available on list l.eachWithIndex{val, i -> println “list[$i]=$val”} Find even values l.findAll{ it%2 == 0} // -> [2, 8]
  • 47. Closures (cont’d) Much more closures are available on list l.eachWithIndex{val, i -> println “list[$i]=$val”} Find even values l.findAll{ it%2 == 0} // -> [2, 8] Make global boolean test l.every{ it < 20 } // -> true (all values are <20) l.any{ it < 0 } // -> false (none is negative)
  • 48. Closures (cont’d) Much more closures are available on list l.eachWithIndex{val, i -> println “list[$i]=$val”} Find even values l.findAll{ it%2 == 0} // -> [2, 8] Make global boolean test l.every{ it < 20 } // -> true (all values are <20) l.any{ it < 0 } // -> false (none is negative) Transform l.collect{ it*10} // [10, 10, 20 , 30, 50, ...]
  • 49. Closures (cont’d) Much more closures are available on list l.eachWithIndex{val, i -> println “list[$i]=$val”} Find even values l.findAll{ it%2 == 0} // -> [2, 8] Make global boolean test l.every{ it < 20 } // -> true (all values are <20) l.any{ it < 0 } // -> false (none is negative) Transform l.collect{ it*10} // [10, 10, 20 , 30, 50, ...] Or even piped l.findAll{ it%2 == 0}.collect{ it*10 } // -> [20, 80]
  • 50. Closures (on map) Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’), ‘simone’:new Date(‘4/2/1985’), ‘birgit’:new Date(’12/6/1988’)]
  • 51. Closures (on map) Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’), ‘simone’:new Date(‘4/2/1985’), ‘birgit’:new Date(’12/6/1988’)] A simple loop birthdays.each{ println “${it.key} born in “ + it.value.year }
  • 52. Closures (on map) Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’), ‘simone’:new Date(‘4/2/1985’), ‘birgit’:new Date(’12/6/1988’)] A simple loop birthdays.each{ println “${it.key} born in “ + it.value.year } Or with name parameters birthday.each{name, date -> println “$name : $date”}
  • 53. Closures (on map) Map<String, Date> birthdays=[‘paul’:new Date(‘5/4/1983’), ‘simone’:new Date(‘4/2/1985’), ‘birgit’:new Date(’12/6/1988’)] A simple loop birthdays.each{ println “${it.key} born in “ + it.value.year } Or with name parameters birthday.each{name, date -> println “$name : $date”} Sort on the month order birthdays.sort{it.value.month} .each{println “${it.key} born in “ + it.value}
  • 54. I/O : reading standard input Without any connection to outside, a script is soon meaningless...
  • 55. I/O : reading standard input Without any connection to outside, a script is soon meaningless... Reading can be done from stdin (standard input) System.in.eachLine{ ... } // loop on all line piped in
  • 56. I/O : reading standard input Without any connection to outside, a script is soon meaningless... Reading can be done from stdin (standard input) System.in.eachLine{ ... } // loop on all line piped in Or interactively Scanner stdin=new Scanner(System.in) int i=stdin.nextInt() stdin.<CTRL-space>
  • 57. I/O: reading from Files File myFile=new File(“path/to/my/file”)
  • 58. I/O: reading from Files File myFile=new File(“path/to/my/file”) Loop through the lines myFile.eachLine{...} myFile.splitEachLine(/s+/){ // it is an array with all // the elements of the current line }
  • 59. I/O: reading from Files File myFile=new File(“path/to/my/file”) Loop through the lines myFile.eachLine{...} myFile.splitEachLine(/s+/){ // it is an array with all // the elements of the current line } Or get the total text at once myFile.getText()
  • 60. I/O: reading from Files File myFile=new File(“path/to/my/file”) Loop through the lines myFile.eachLine{...} myFile.splitEachLine(/s+/){ // it is an array with all // the elements of the current line } Or get the total text at once myFile.getText() Temporary file are often necessary File myTmpFile=File.createTempFile(‘prefix’, ‘.suf’) myTmpFile.deleteOnExit()
  • 61. Functions Function is a piece of code that takes argument and returns a value (like a sub in perl)
  • 62. Functions Function is a piece of code that takes argument and returns a value (like a sub in perl) Parameters can be statically or dynamically typed int increment(x, i){ return x+i } int j=3 println increment(j, 5) // -> 8
  • 64. Functions Depending on argument type, the method is guessed def increment(int x) {return x + 1 } def increment(double x){return x + 0.1} println increment(3) // -> 4 println increment(3.0) // -> 3.1
  • 65. Functions Depending on argument type, the method is guessed def increment(int x) {return x + 1 } def increment(double x){return x + 0.1} println increment(3) // -> 4 println increment(3.0) // -> 3.1
  • 67. Functions def increment(List l) {l << 1 } def increment(String s){return s + 1} println increment([3, 4]) // -> [3, 4, 1] println increment(“abcd”) // -> “abcd1”
  • 68. Functions (cont’d) Number of arguments induces the function called int increment (x, i){ return x+i } int increment (x) { return x+1 } println increment(3) // -> 4 println increment(3, 4) // -> 7 More concisely, parameters can be defined by default int increment(x, i=1){ // if no second arg => i=1 return x+i } println increment(3, 5) // -> 8 println increment(3) // -> 4
  • 69. Functions (cont’d) Number of arguments induces the function called int increment (x, i){ return x+i } int increment (x) { return x+1 } println increment(3) // -> 4 println increment(3, 4) // -> 7 More concisely, parameters can be defined by default int increment(x, i=1){ // if no second arg => i=1 return x+i } println increment(3, 5) // -> 8 println increment(3) // -> 4
  • 70. Functions (cont’d) Number of arguments induces the function called int increment (x, i){ return x+i } int increment (x) { return x+1 } println increment(3) // -> 4 println increment(3, 4) // -> 7 More concisely, parameters can be defined by default int increment(x, i=1){ // if no second arg => i=1 return x+i } println increment(3, 5) // -> 8 println increment(3) // -> 4
  • 71. Functions (cont’d) Number of arguments induces the function called int increment (x, i){ return x+i } int increment (x) { return x+1 } println increment(3) // -> 4 println increment(3, 4) // -> 7 More concisely, parameters can be defined by default int increment(x, i=1){ // if no second arg => i=1 return x+i } println increment(3, 5) // -> 8 println increment(3) // -> 4
  • 72. Functions (cont’d) We can always use a Map with for named parameters int increment(params){ return (params.x?:0) + // ?:0 0 if params.x false (params.plus?:0) - (params.minus?:0) } increment(x:3, plus:4) // -> 7
  • 73. Functions (cont’d) We can always use a Map with for named parameters int increment(params){ return (params.x?:0) + // ?:0 0 if params.x false (params.plus?:0) - (params.minus?:0) } increment(x:3, plus:4) // -> 7 Method described fully with map arguments will be extensively used when calling action from url

Notas do Editor

  1. \n
  2. IDE basics\nhorizontal layers for learning\nmotto of the semester : keep the code concise!!\nwe&amp;#x2019;ll focus on features of less than 10 lines. (-&gt;7)\n\n
  3. Try plugins (tasks etc...)\nbut exp =&gt; do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we&amp;#x2019;ll see the most useful shortcuts bit by bit\n\n
  4. Try plugins (tasks etc...)\nbut exp =&gt; do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we&amp;#x2019;ll see the most useful shortcuts bit by bit\n\n
  5. Try plugins (tasks etc...)\nbut exp =&gt; do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we&amp;#x2019;ll see the most useful shortcuts bit by bit\n\n
  6. Try plugins (tasks etc...)\nbut exp =&gt; do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we&amp;#x2019;ll see the most useful shortcuts bit by bit\n\n
  7. (+ some meta-information) \nsetup will greatly be enhanced when we will build more than just scripts\nCTRL-Shift-F11 (or something close, depending on your setup) to relaunch the script\nCtrl-space demo\ndon&amp;#x2019;t forget to close project to limit noise\n
  8. (+ some meta-information) \nsetup will greatly be enhanced when we will build more than just scripts\nCTRL-Shift-F11 (or something close, depending on your setup) to relaunch the script\nCtrl-space demo\ndon&amp;#x2019;t forget to close project to limit noise\n
  9. (+ some meta-information) \nsetup will greatly be enhanced when we will build more than just scripts\nCTRL-Shift-F11 (or something close, depending on your setup) to relaunch the script\nCtrl-space demo\ndon&amp;#x2019;t forget to close project to limit noise\n
  10. (+ some meta-information) \nsetup will greatly be enhanced when we will build more than just scripts\nCTRL-Shift-F11 (or something close, depending on your setup) to relaunch the script\nCtrl-space demo\ndon&amp;#x2019;t forget to close project to limit noise\n
  11. do not touch build/ explicitely\n
  12. do not touch build/ explicitely\n
  13. do not touch build/ explicitely\n
  14. do not touch build/ explicitely\n
  15. do not touch build/ explicitely\n
  16. do not touch build/ explicitely\n
  17. do not touch build/ explicitely\n
  18. we will come back often to discover more possibilities in using eclipse...\ndon&amp;#x2019;t forget to close project to limit noise\n
  19. we will come back often to discover more possibilities in using eclipse...\ndon&amp;#x2019;t forget to close project to limit noise\n
  20. we will come back often to discover more possibilities in using eclipse...\ndon&amp;#x2019;t forget to close project to limit noise\n
  21. \n
  22. \n
  23. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  24. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  25. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  26. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  27. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  28. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  29. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  30. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  31. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  32. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  33. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  34. \n
  35. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  36. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  37. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  38. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  39. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  40. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  41. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  42. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  43. test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  44. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  45. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  46. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  47. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  48. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  49. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  50. Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  51. go google!!!\nWe will be always able to see a 1% of the possibilities\n
  52. go google!!!\nWe will be always able to see a 1% of the possibilities\n
  53. go google!!!\nWe will be always able to see a 1% of the possibilities\n
  54. go google!!!\nWe will be always able to see a 1% of the possibilities\n
  55. go google!!!\nWe will be always able to see a 1% of the possibilities\n
  56. see the new Date(string)\nbirthday.each -&gt; loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  57. see the new Date(string)\nbirthday.each -&gt; loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  58. see the new Date(string)\nbirthday.each -&gt; loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  59. see the new Date(string)\nbirthday.each -&gt; loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  60. often have automated reading through file \nparameters are passed as command arguments or into a file\n
  61. often have automated reading through file \nparameters are passed as command arguments or into a file\n
  62. often have automated reading through file \nparameters are passed as command arguments or into a file\n
  63. don&amp;#x2019;t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  64. don&amp;#x2019;t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  65. don&amp;#x2019;t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  66. don&amp;#x2019;t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  67. avoid \n
  68. avoid \n
  69. avoid \n
  70. avoid \n
  71. avoid \n
  72. avoid \n
  73. avoid \n
  74. better write \nint increment(x){ return increment(x, 1)}\n
  75. better write \nint increment(x){ return increment(x, 1)}\n
  76. better write \nint increment(x){ return increment(x, 1)}\n
  77. better write \nint increment(x){ return increment(x, 1)}\n
  78. we&amp;#x2019;ll see ?: later\n
  79. we&amp;#x2019;ll see ?: later\n