produced › design-patterns › › topic01-context › talk-c... · java? 8 1.conditionals 2.a...
TRANSCRIPT
![Page 1: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/1.jpg)
Produced by
Department of Computing, Maths & PhysicsWaterford Institute of Technologyhttp://www.wit.ie
http://elearning.wit.ie
MSc in Computer Science
Agile Software Development
Eamonn de [email protected]
![Page 2: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/2.jpg)
Programming Languages - Typing Wish Lists
Agile Software Development
![Page 3: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/3.jpg)
Family Tree (3)
3
![Page 4: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/4.jpg)
Smalltalk Cluster
4
![Page 5: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/5.jpg)
5
Ruby, Groovy, Java, Scala Cluster
![Page 6: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/6.jpg)
Groovy
GoXtendSwift
Objective-C
Scala
![Page 7: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/7.jpg)
Paul Grahams Wish List for a Programming Language http://www.paulgraham.com/diff.html
1.Conditionals
2.A function type
3.Recursion
4.Dynamic typing
5.Garbage collection
6.Programs composed of expressions
7.A symbol type
8.A notation for code using symbols and trees
9.The whole language there all the time
Lisp programming Language has all of these features (since mid 1960’s)
7
![Page 8: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/8.jpg)
Java?
8
1.Conditionals
2.A function type (Java 8 only)
3.Recursion
4.Dynamic typing
5.Garbage collection
6.Programs composed of expressions
7.A symbol type
8.A notation for code using symbols and trees
9.The whole language there all the time
![Page 9: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/9.jpg)
Groovy/Ruby/Python/Scala/Xtend (from Neal Ford)
9
1.Conditionals
2.A function type
3.Recursion
4.Dynamic typing (+ Type Inference)
5.Garbage collection
6.Programs composed of expressions
7.A symbol type
8.A notation for code using symbols and trees
9.The whole language there all the time
+ Metaprogramming
![Page 10: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/10.jpg)
Groovy/Ruby/Python/Scala (from Neal Ford)
10
1.Conditionals
2.A function type
3.Recursion
4.Dynamic typing (+Type Inference)
5.Garbage collection
6.Programs composed of expressions
7.A symbol type
8.A notation for code using symbols and trees
9.The whole language there all the time
+ Metaprogramming
![Page 11: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/11.jpg)
Typing
11
![Page 12: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/12.jpg)
![Page 13: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/13.jpg)
http://blogs.agilefaqs.com/2011/07/11/dynamic-typing-is-not-weak-typing/
![Page 14: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/14.jpg)
Another Approach to Types?
• Type Inference : the compiler draws conclusions about the types of variables based on how programmers use those variables.
• Yields programs that have some of the conciseness of Dynamically Typed Languages
• But - decision made at compile time, not at run time
• More information for static analysis - refactoring tools, complexity analysis. bug checking etc...
• Haskell, Scala, Swift, Xtend
14
object InferenceTest1 extends Application { val x = 1 + 2 * 3 // the type of x is Int val y = x.toString() // the type of y is String def succ(x: Int) = x + 1 // method succ returns Int values}
![Page 15: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/15.jpg)
‘Pragmatic’ Languages
15
•Python
• Java
•Swift•C
•Ruby
•C#
•Go
•C++•Objective-C
•Scala
•PHP• Javascript
•Groovy•Smalltalk
![Page 16: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/16.jpg)
Typing Spectrum
16
•Python
• Java
•Swift•C
•Ruby
•C#
•Go
•C++•Objective-C
•Scala
•PHP• Javascript
•Groovy
Strong WeakStatic
Dynamic•Smalltalk
Inferred
![Page 17: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/17.jpg)
Java Example (from Jim Weirich)
• Java algorithm to filter a list of strings
• Only printing those shorter than 3 (in this test case).
17
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 18: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/18.jpg)
Groovy 1
18
• Also a valid Groovy program...
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 19: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/19.jpg)
Groovy 1
19
• Do we need generics?
• What about semicolons...
• Should standard libraries be imported?
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 20: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/20.jpg)
Groovy 2
20
class Erase{ public static void main(String[] args) { List names = new ArrayList() names.add("Ted") names.add("Fred") names.add("Jed") names.add("Ned") System.out.println(names) Erase e = new Erase() List short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) for (String s : short_names) { System.out.println(s) } }
public List filterLongerThan(Liststrings, length) { List result = new ArrayList(); for (String s : strings) { if (s.length() < length + 1) { result.add(s) } } return result }}
![Page 21: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/21.jpg)
Groovy 2
21
class Erase{ public static void main(String[] args) { List names = new ArrayList() names.add("Ted") names.add("Fred") names.add("Jed") names.add("Ned") System.out.println(names) Erase e = new Erase() List short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) for (String s : short_names) { System.out.println(s) } }
public List filterLongerThan(Liststrings, length) { List result = new ArrayList(); for (String s : strings) { if (s.length() < length + 1) { result.add(s) } } return result }}
• Do we need the static types?
• Must we always have a main method and class definition?
• Consistency (size or length)?
![Page 22: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/22.jpg)
Groovy 3
22
def filterLongerThan(strings, length){ List result = new ArrayList(); for (String s : strings) { if (s.length() < length + 1) { result.add(s) } } return result}
List names = new ArrayList()names.add("Ted")names.add("Fred")names.add("Jed")names.add("Ned")System.out.println(names)List short_names = filterLongerThan(names, 3)System.out.println(short_names.size())for (String s : short_names){ System.out.println(s)}
![Page 23: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/23.jpg)
Groovy 3
• Should we have a special notation for lists?
• And special facilities for list processing?
23
def filterLongerThan(strings, length){ List result = new ArrayList(); for (String s : strings) { if (s.length() < length + 1) { result.add(s) } } return result}
List names = new ArrayList()names.add("Ted")names.add("Fred")names.add("Jed")names.add("Ned")System.out.println(names)List short_names = filterLongerThan(names, 3)System.out.println(short_names.size())for (String s : short_names){ System.out.println(s)}
![Page 24: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/24.jpg)
Groovy 4
24
def filterLongerThan(strings, length){ return strings.findAll {it.size() <= length}}
names = ["Ted", "Fred", "Jed", "Ned"]System.out.println(names)List short_names = filterLongerThan(names, 3)System.out.println(short_names.size())short_names.each {System.out.println(it)}
![Page 25: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/25.jpg)
Groovy 4
• Method needed any longer?
• Is there an easier way to use common methods (e.g. println)?
• Are brackets always needed?
25
def filterLongerThan(strings, length){ return strings.findAll {it.size() <= length}}
names = ["Ted", "Fred", "Jed", "Ned"]System.out.println(names)List short_names = filterLongerThan(names, 3)System.out.println(short_names.size())short_names.each {System.out.println(it)}
![Page 26: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/26.jpg)
Groovy 5
26
names = ["Ted", "Fred", "Jed", "Ned"]println namesshort_names = names.findAll{it.size() <= 3}println short_names.size()short_names.each {println it}
![Page 27: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/27.jpg)
Java vs Groovy?
27
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
names = ["Ted", "Fred", "Jed", "Ned"]println namesshort_names = names.findAll{it.size() <= 3}println short_names.size()short_names.each {println it}
![Page 28: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/28.jpg)
Java Example -> XTend
• Unlike Groovy - this is NOT an XTend Program
28
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 29: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/29.jpg)
def & var
29
import java.util.ArrayList;import java.util.List;
class Erase{ def static void main(String[] args) { var List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); var Erase e = new Erase(); var List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
def List<String> filterLongerThan(List<String> strings, int length) { var List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
Are semicolons necessary?
![Page 30: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/30.jpg)
No semicolons
30
import java.util.ArrayList;import java.util.List;
class Erase{ def static void main(String[] args) { var List<String> names = new ArrayList<String>() names.add("Ted") names.add("Fred") names.add("Jed") names.add("Ned") System.out.println(names) var Erase e = new Erase() var List<String> short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) for (String s : short_names) { System.out.println(s) } }
def List<String> filterLongerThan(List<String> strings, int length) { var List<String> result = new ArrayList<String>() for (String s : strings) { if (s.length() < length + 1) { result.add(s) } } return result }}
Can some types be inferred?
![Page 31: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/31.jpg)
Type inference
31
import java.util.ArrayList;import java.util.List;
class Erase{ def static void main(String[] args) { var names = new ArrayList<String>() names.add("Ted") names.add("Fred") names.add("Jed") names.add("Ned") System.out.println(names) var e = new Erase() var short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) for (s : short_names) { System.out.println(s) } }
def filterLongerThan(List<String> strings, int length) { var result = new ArrayList<String>() for (s : strings) { if (s.length() < length + 1) { result.add(s) } } return result }}
What about Collection Literals?
![Page 32: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/32.jpg)
Collection Literals
32
import java.util.ArrayList;import java.util.List;
class Erase{ def static void main(String[] args) { var names = #["Ted", "Fred", "Jed", "Ned"] System.out.println(names) var e = new Erase() var short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) for (s : short_names) { System.out.println(s) } }
def filterLongerThan(List<String> strings, int length) { var result = new ArrayList<String>() for (s : strings) { if (s.length() < length + 1) { result.add(s) } } return result }}
Can Lambas simplify code?
![Page 33: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/33.jpg)
Lambdas
33
import java.util.ArrayList;import java.util.List;
class Erase{ def static void main(String[] args) { var names = #["Ted", "Fred", "Jed", "Ned"] System.out.println(names) var e = new Erase() var short_names = e.filterLongerThan(names, 3) System.out.println(short_names.size()) short_names.forEach[System.out.println(it)] }
def filterLongerThan(List<String> strings, int length) { val result = new ArrayList<String>() strings.forEach[ if (it.length() < length + 1) { result.add(it) }] result }}
What are List Comprehensions?
![Page 34: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/34.jpg)
Filters/List Comprehensions
34
import java.util.List;
class Erase{ def static void main(String[] args) { var names = #["Ted", "Fred", "Jed", "Ned"] System.out.println(names) var e = new Erase() var short_names = e.filterLongerThan(names, 3)
System.out.println(short_names.size()) short_names.forEach[System.out.println(it)] }
def filterLongerThan(List<String> strings, int length) { val list = strings.filter[it.length() <= 3] list }}
Do we need the class Erase at all?
![Page 35: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/35.jpg)
Final Version
35
class Erase{ def static void main(String[] args) { var names = #["Ted", "Fred", "Jed", "Ned"] println(names) var short_names = names.filter[it.length() <= 3] println(short_names.size()) short_names.forEach[println(it)] }}
![Page 36: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/36.jpg)
Java
36
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
class Erase{ def static void main(String[] args) { var names = #["Ted", "Fred", "Jed", "Ned"] println(names) var short_names = names.filter[it.length() <= 3] println(short_names.size()) short_names.forEach[println(it)] }}
names = ["Ted", "Fred", "Jed", "Ned"]println namesshort_names = names.findAll{it.size() <= 3}println short_names.size()short_names.each {println it}
java
groovy
xtend
![Page 37: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/37.jpg)
Java Example (again)
37
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 38: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/38.jpg)
Swift
38
import Foundation
class Erase { func main() { var names:String[] = String[]() names.append ("ted") names.append ("fred") names.append ("jed") names.append ("ned") println(names) var short_names:String[] = filterLongerThan(names, length:3) for name:String in short_names { println (name) } } func filterLongerThan (strings : String[], length : Int) -> String[] { var result:String[] = String[]() for s:String in strings { if countElements(s) < length + 1 { result.append(s) } } return result } }
var erase:Erase = Erase() erase.main()
![Page 39: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/39.jpg)
Swift
• Type Inference
39
import Foundation
class Erase { func main() { var names = String[]() names.append ("ted") names.append ("fred") names.append ("jed") names.append ("ned") println(names) var short_names = filterLongerThan(names, length:3) for name in short_names { println (name) } } func filterLongerThan (strings : String[], length : Int) -> String[] { var result = String[]() for s in strings { if countElements(s) < length + 1 { result.append(s) } } return result } }
var erase = Erase() erase.main()
![Page 40: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/40.jpg)
Swift
• Literals
40
import Foundation
class Erase { func main() { var names = ["ted", "fred", "jed", "ned"] var short_names = filterLongerThan(names, length:3) for name in short_names { println (name) } } func filterLongerThan (strings : String[], length : Int) -> String[] { var result = String[]() for s in strings { if countElements(s) < length + 1 { result.append(s) } } return result } }
var erase = Erase() erase.main()
![Page 41: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/41.jpg)
Swift
• Closures
41
import Foundation
class Erase { func main() { var names = ["ted", "fred", "jed", "ned"] var short_names = names.filter { countElements($0) < 4 } for name in short_names { println (name) } } }
var erase = Erase() erase.main()
![Page 42: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/42.jpg)
Swift
• Final version
42
import Foundation
var names = ["ted", "fred", "jed", "ned"] println(names) var short_names = names.filter { countElements($0) < 4 } println(short_names)
![Page 43: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/43.jpg)
43
var names = ["ted", "fred", "jed", "ned"] println(names) var short_names = names.filter { countElements($0) < 4 } println(short_names)
var names = #["Ted", "Fred", "Jed", "Ned"]println(names)var short_names = names.filter[it.length() <= 3]short_names.forEach[println(it)]
names = ["Ted", "Fred", "Jed", "Ned"]println namesshort_names = names.findAll{it.size() <= 3}short_names.each {println it}
import java.util.ArrayList;import java.util.List;
class Erase{ public static void main(String[] args) { List<String> names = new ArrayList<String>(); names.add("Ted"); names.add("Fred"); names.add("Jed"); names.add("Ned"); System.out.println(names); Erase e = new Erase(); List<String> short_names = e.filterLongerThan(names, 3); System.out.println(short_names.size()); for (String s : short_names) { System.out.println(s); } }
public List<String> filterLongerThan(List<String> strings, int length) { List<String> result = new ArrayList<String>(); for (String s : strings) { if (s.length() < length + 1) { result.add(s); } } return result; }}
![Page 44: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/44.jpg)
Another ‘Shopping List’
44
Object-literal syntax for arrays and hashesArray slicing and other intelligent collection operatorsPerl 5 compatible regular expression literalsDestructuring bind (e.g. x, y = returnTwoValues())Function literals and first-class, non-broken closuresStandard OOP with classes, instances, interfaces, polymorphism, etc.Visibility quantifiers (public/private/protected)Iterators and generatorsList comprehensionsNamespaces and packagesCross-platform GUIOperator overloadingKeyword and rest parametersFirst-class parser and AST supportType expressions and statically checkable semanticsSolid string and collection librariesStrings and streams act like collections
http://steve-yegge.blogspot.com.au/2007/02/next-big-language.html
![Page 45: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/45.jpg)
Java
45
Object-literal syntax for arrays and hashesArray slicing and other intelligent collection operatorsPerl 5 compatible regular expression literalsDestructuring bind (e.g. x, y = returnTwoValues())Function literals and first-class, non-broken closuresStandard OOP with classes, instances, interfaces, polymorphism, etc.
yVisibility quantifiers (public/private/protected) yIterators and generators yList comprehensionsNamespaces and packages yCross-platform GUI yOperator overloadingKeyword and rest parametersFirst-class parser and AST supportType expressions and statically checkable semantics ySolid string and collection libraries yStrings and streams act like collections y
![Page 46: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/46.jpg)
Google GO
46
Object-literal syntax for arrays and hashes yArray slicing and other intelligent collection operators yPerl 5 compatible regular expression literalsDestructuring bind (e.g. x, y = returnTwoValues()) yFunction literals and first-class, non-broken closures yStandard OOP with classes, instances, interfaces, polymorphism, etc.Visibility quantifiers (public/private/protected) yIterators and generatorsList comprehensionsNamespaces and packages yCross-platform GUIOperator overloadingKeyword and rest parameters yFirst-class parser and AST support yType expressions and statically checkable semantics ySolid string and collection libraries yStrings and streams act like collections
![Page 47: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/47.jpg)
Python
47
Object-literal syntax for arrays and hashes yArray slicing and other intelligent collection operators yPerl 5 compatible regular expression literals yDestructuring bind (e.g. x, y = returnTwoValues()) yFunction literals and first-class, non-broken closures yStandard OOP with classes, instances, interfaces, polymorphism, etc.
yVisibility quantifiers (public/private/protected)Iterators and generators y
yList comprehensions yNamespaces and packages yCross-platform GUIOperator overloadingKeyword and rest parameters yFirst-class parser and AST support yType expressions and statically checkable semanticsSolid string and collection libraries yStrings and streams act like collections y
![Page 48: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/48.jpg)
Xtend
48
Object-literal syntax for arrays and hashes yArray slicing and other intelligent collection operators yPerl 5 compatible regular expression literalsDestructuring bind (e.g. x, y = returnTwoValues())Function literals and first-class, non-broken closures yStandard OOP with classes, instances, interfaces, polymorphism, etc.
yVisibility quantifiers (public/private/protected) yIterators and generators yList comprehensions yNamespaces and packages yCross-platform GUI yOperator overloading yKeyword and rest parametersFirst-class parser and AST supportType expressions and statically checkable semantics ySolid string and collection libraries yStrings and streams act like collections y
Active Annotations ?
![Page 49: Produced › design-patterns › › topic01-context › talk-c... · Java? 8 1.Conditionals 2.A function type (Java 8 only) 3.Recursion 4.Dynamic typing 5.Garbage collection 6.Programs](https://reader034.vdocument.in/reader034/viewer/2022042408/5f24a16b6c9d4c52af7a6b9d/html5/thumbnails/49.jpg)
Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License.
For more information, please see http://creativecommons.org/licenses/by-nc/3.0/