• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
groovy & grails - lecture 2
 

groovy & grails - lecture 2

on

  • 379 views

Eclipse IDE basics

Eclipse IDE basics
Assertions
Closures
I/O
Functions
Control structures

Statistics

Views

Total Views
379
Views on SlideShare
379
Embed Views
0

Actions

Likes
0
Downloads
11
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • IDE basics\nhorizontal layers for learning\nmotto of the semester : keep the code concise!!\nwe’ll focus on features of less than 10 lines. (->7)\n\n
  • Try plugins (tasks etc...)\nbut exp => do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we’ll see the most useful shortcuts bit by bit\n\n
  • Try plugins (tasks etc...)\nbut exp => do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we’ll see the most useful shortcuts bit by bit\n\n
  • Try plugins (tasks etc...)\nbut exp => do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we’ll see the most useful shortcuts bit by bit\n\n
  • Try plugins (tasks etc...)\nbut exp => do not mix too much . install eclipse in different directories\nuse shortcuts!!!\ncustomize them. Little mouse. we’ll see the most useful shortcuts bit by bit\n\n
  • (+ 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’t forget to close project to limit noise\n
  • (+ 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’t forget to close project to limit noise\n
  • (+ 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’t forget to close project to limit noise\n
  • (+ 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’t forget to close project to limit noise\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • do not touch build/ explicitely\n
  • we will come back often to discover more possibilities in using eclipse...\ndon’t forget to close project to limit noise\n
  • we will come back often to discover more possibilities in using eclipse...\ndon’t forget to close project to limit noise\n
  • we will come back often to discover more possibilities in using eclipse...\ndon’t forget to close project to limit noise\n
  • \n
  • \n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • \n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • test driven is programming in 2 steps:\n * define the goal through assertions\n * fulfill the tests writing the code\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • Even the simplest def is a bit beyond understanding...\nit is like $_ in perl\nnote the curly brackets\n
  • go google!!!\nWe will be always able to see a 1% of the possibilities\n
  • go google!!!\nWe will be always able to see a 1% of the possibilities\n
  • go google!!!\nWe will be always able to see a 1% of the possibilities\n
  • go google!!!\nWe will be always able to see a 1% of the possibilities\n
  • go google!!!\nWe will be always able to see a 1% of the possibilities\n
  • see the new Date(string)\nbirthday.each -> loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  • see the new Date(string)\nbirthday.each -> loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  • see the new Date(string)\nbirthday.each -> loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  • see the new Date(string)\nbirthday.each -> loop on Map Entry (key/value)\nin fact, a linkedMap (order is kept)\nhow to sort with decreasing month (dec, nov,...january)\n
  • often have automated reading through file \nparameters are passed as command arguments or into a file\n
  • often have automated reading through file \nparameters are passed as command arguments or into a file\n
  • often have automated reading through file \nparameters are passed as command arguments or into a file\n
  • don’t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  • don’t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  • don’t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  • don’t forget that most of the commons manipulations are already programmed in the core library\nFile delete, exist, find all file names etc.. \n
  • avoid \n
  • avoid \n
  • avoid \n
  • avoid \n
  • avoid \n
  • avoid \n
  • avoid \n
  • better write \nint increment(x){ return increment(x, 1)}\n
  • better write \nint increment(x){ return increment(x, 1)}\n
  • better write \nint increment(x){ return increment(x, 1)}\n
  • better write \nint increment(x){ return increment(x, 1)}\n
  • we’ll see ?: later\n
  • we’ll see ?: later\n

groovy & grails - lecture 2 groovy & grails - lecture 2 Presentation Transcript

  • Groovy: Efficiency Oriented ProgrammingLecture 2Master Proteomics & Bioinformatics - University of GenevaAlexandre 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
  • AssertionsTo 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”
  • AssertionsTo 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
  • AssertionsUse assertions that will clearly report failure if any, and be silent if none assert x == 7
  • AssertionsUse assertions that will clearly report failure if any, and be silent if none assert x == 7Any boolean value can be tested assert [1, 7, 4] == l-2 assert “my funny valentine” == my_song
  • AssertionsUse assertions that will clearly report failure if any, and be silent if none assert x == 7Any boolean value can be tested assert [1, 7, 4] == l-2 assert “my funny valentine” == my_songassert 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 codewrapped up as an object[...]. It’s a normal object in that you can pass areference 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 codewrapped up as an object[...]. It’s a normal object in that you can pass areference 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 usedin complex situations, closure are also part of daily programming.
  • ClosuresIt 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
  • ClosuresIt 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 iteratorThe 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 inputWithout any connection to outside, a script is soon meaningless...
  • I/O : reading standard inputWithout 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 inputWithout 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 inOr interactively Scanner stdin=new Scanner(System.in) int i=stdin.nextInt() stdin.<CTRL-space>
  • I/O: reading from FilesFile 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()
  • FunctionsFunction is a piece of code that takes argument and returns a value (like asub in perl)
  • FunctionsFunction is a piece of code that takes argument and returns a value (like asub 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
  • FunctionsDepending 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
  • FunctionsDepending 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
  • Functionsdef 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) // -> 7More 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) // -> 7More 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) // -> 7More 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) // -> 7More 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) // -> 7Method described fully with map arguments will be extensively used whencalling action from url