SlideShare a Scribd company logo
1. Два клЕвых пацана на
сцене
2. Прикольные загадки
3. Вы голосуете за
правильный ответ
4. Мы швыряемся вещами
-3.abs()
(-3).abs()
int value = -3
value.abs()
println (-3).abs()
-3
Caught: java.lang.NullPointerException: Cannot invoke method abs() on null
object
java.lang.NullPointerException: Cannot invoke method abs() on null object
at AbsolutelyGroovy.run(AbsolutelyGroovy.groovy:7)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
println (-3).abs()
“Все проблемы в программировании
можно решить добавив пару скобок”
John McCarthy, изобретатель LISP
println ((-3).abs())
int value = -3
println value.abs()
boolean isPrime(def x) {
if (x == 2) return true
int limit = Math.sqrt(x) + 1
(2..limit).each {
if (x % it == 0) {
return false
}
}
true
}
println isPrime("4" as Double)
boolean isPrime(def x) {
if (x == 2) return true
int limit = Math.sqrt(x) + 1
(2..limit).each {
if (x % it == 0) {
return false
}
}
true
}
println isPrime("4" as Double)
boolean isPrime(def x) {
if (x == 2) return true
int limit = Math.sqrt(x) + 1
(2..limit).each {
if (x % it == 0) {
return false
}
}
true
}
println isPrime("4" as Double)
http://kousenit.wordpress.com/2014/04/18/responses-to-the-closure-of-no-return/
class Conference {def name; def year}
def gr = new Conference(name: 'Greach', year: 2014)
gr.each {println it}
List<Integer> list = [56, '9', 74]
def max = list.max { item ->
(item < 50) ? item : null
}
println max
List<Integer> list = [56, '9', 74]
def max = list.max { item ->
(item < 50) ? item : null
}
println max
>groovysh (('9' as Character) as Integer)
===> 57
List<Integer> list = [56, 57, 74]
def max = list.max { item ->
(item < 50) ? item : null
}
println max
List<Integer> list = [56, '9', 74]
def max = list.max { item ->
(item < 50) ? item : null
}
println max
def random = new Random()
def randomList = []
0..10.each {randomList << random.nextInt()}
assert randomList.max{ null } == randomList[0]
Closure ктоУбийца() {
{
'Мориарти.'
}
}
println ктоУбийца()
Closure ктоУбийца() {
{ ->
'Мориарти.'
}
}
Closure ктоУбийца() {
return {
'Мориарти.'
}
}
class CountDown { int counter = 10 }
CountDown finalCountDown() {
def countDown = new CountDown()
try {
countDown.counter = --countDown.counter
} catch (ignored) {
println "That will never happen."
countDown.counter = Integer.MIN_VALUE
} finally {
return countDown
}
}
println finalCountDown().counter
class CountDown { int counter = 10 }
CountDown finalCountDown() {
def countDown = new CountDown()
try {
countDown.counter = --countDown.counter
} catch (ignored) {
println "That will never happen."
countDown.counter = Integer.MIN_VALUE
} finally {
return countDown
}
}
println finalCountDown().counter
class CountDown { int counter = 10 }
CountDown finalCountDown() {
def countDown = new CountDown()
try {
countDown.counter = --countDown.counter
} catch (ignored) {
ignored.printStackTrace()
countDown.counter = Integer.MIN_VALUE
} finally {
return countDown
}
}
println finalCountDown().counter
org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object '9' with class 'java.lang.Integer' to class 'CountDown’
-2147483648
class CountDown { int counter = 10 }
CountDown finalCountDown() {
def countDown = new CountDown()
try {
countDown.counter = --countDown.counter
} catch (ignored) {
println "That will never happen."
countDown.counter = Integer.MIN_VALUE
} finally {
return countDown
}
42
}
println finalCountDown().counter
class CountDown { int counter = 10 }
CountDown finalCountDown() {
def countDown = new CountDown()
try {
countDown.counter = --countDown.counter
} catch (ignored) {
println "That will never happen."
countDown.counter = Integer.MIN_VALUE
} finally {
return countDown
}
42
}
println finalCountDown().counter
def key = 'x'
def map = [key: 'treasure']
def value = map.get(key)
println value
def key = 'x'
def map = [key: 'treasure']
def value = map.get(key)
println value
1.def map = [(key): 'treasure']
2.map.put(key, 'treasure')
3.map[key] = 'treasure'
4.map." " = 'treasure'
def map = [2: 'treasure']
def key = 2
def value = map."$key"
println value
def map = [2: 'treasure']
def key = 2
def value = map."$key"
println value
def map = [2: 'treasure']
println map.keySet().first().class.name
java.lang.Integer
def key = 'x'
def map = ["${key}": 'treasure']
def value = map['x']
println value
def key = 'x'
def map = ["${key}": 'treasure']
def value = map['x']
println value
def map = ["${key}": 'treasure']
println map.keySet().first().class.name
org.codehaus.groovy.runtime.GStringImpl
def range = 1.0..10.0
assert range.contains(5.0)
println range.contains(5.6)
Iterator iterator = (1.0..10.0).iterator()
while (iterator.hasNext()) {
print "${iterator.next()} "
}
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
[0..9].each { println(it - 1) }
[0..9].each { println(it - 1) }
Это неправильные
скобки!
(0..9).each { println(it - 1) }
Другое дело!
[0, 2, 3, 4, 5, 6, 7, 8, 9]
List<Long> list = [1,2,3]
def now = new Date()
list << now
println list
List<Long> list = [1,2,3]
def now = new Date()
list << now
println list
List<Long> list = [1,2,3]
def now = new Date()
list << now
list << 'foo'
println list*.class.name
[java.lang.Long, java.lang.Long,
java.lang.Long, java.util.Date,
java.lang.String]
double value = 3
println "$value.14".isDouble()
double value = 3
println "$value.14".isDouble()
Легко!
class Invite {
int attending = 1
}
def invite = new Invite()
def attendees = (invite.attending) +1
println attendees
Это Андрей
Это Джигурда
class Invite {
int attending = 1
}
def invite = new Invite()
def attendees = (invite.attending) +1
println attendees
def attendees = (new Invite().attending) + 1
println attendees
def invite = new Invite()
def attendees = invite.attending +1
Как убрать скобки?!
def invite = new Invite()
def attendees = ((invite.attending)) +1
Наоборот добавить.
Скобки никогда нельзя
убирать!
class MrHyde {
def me() {
return this
}
}
class DrJekyll {
}
DrJekyll.mixin MrHyde
def drJekyll = new DrJekyll().me() as DrJekyll
def mrHide = new DrJekyll().me()
println "$drJekyll and $mrHide, are they the
same? ${(drJekyll.class).equals(mrHide.class)}"
class MrHyde {
def me() {
return this
}
}
class DrJekyll {
}
DrJekyll.mixin MrHyde
def drJekyll = new DrJekyll().me() as DrJekyll
def mrHide = new DrJekyll().me()
println "$drJekyll and $mrHide, are they the same?
${(drJekyll.class).equals(mrHide.class)}"
def x = int
println x
if ((x = long)) {
println x
}
if (x = boolean ) {
println x
}
def x = int
println x
if ((x = long)) {
println x
}
if (x = boolean ) {
println x
}
class VanHalen {
public static jump() {
"Here are the ${lyrics()}"
}
def methodMissing(String name, def args) {
'lyrics'
}
}
println VanHalen.jump()
class VanHalen {
public static jump() {
"Here are the ${lyrics()}"
}
def methodMissing(String name, def args) {
'lyrics'
}
}
println VanHalen.jump()
class VanHalen {
public static jump() {
"Here are the ${lyrics()}"
}
static $static_methodMissing(String name, def args) {
'lyrics'
}
}
println VanHalen.jump()
class VanHalen {
public jump() {
"Here are the ${lyrics()}"
}
def methodMissing(String name, def args) {
'lyrics'
}
}
println new VanHalen().jump()
def map = [metaClass: ‘frequency']
println "What's the $map.metaClass, Барух?"
map.metaClass
map.get('metaClass')
map.getMetaClass()
1. Пишите читабельный код
2. Комментируйте все трюки
3. Иногда это баг
4. Пользуйте static code
analysis - intellij IDEA!
5. Rtfm
6. Don’t code like my brother
Groovy puzzlers jug-moscow-part 2

More Related Content

What's hot

Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)MongoSF
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기
Suyeol Jeon
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Suyeol Jeon
 
The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212
Mahmoud Samir Fayed
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
Alicia Pérez
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
Paweł Dawczak
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
David Golden
 
Code as data as code.
Code as data as code.Code as data as code.
Code as data as code.Mike Fogus
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
Lorenz Cuno Klopfenstein
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
osfameron
 
Fertile Ground: The Roots of Clojure
Fertile Ground: The Roots of ClojureFertile Ground: The Roots of Clojure
Fertile Ground: The Roots of ClojureMike Fogus
 
Ricky Bobby's World
Ricky Bobby's WorldRicky Bobby's World
Ricky Bobby's World
Brian Lonsdorf
 
Mongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is BrightMongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is Bright
aaronheckmann
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
Brian Lonsdorf
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
Eugene Zharkov
 
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
Mahmoud Samir Fayed
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures edition
osfameron
 
PHP 5.4
PHP 5.4PHP 5.4
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
Tsuyoshi Yamamoto
 

What's hot (20)

Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
 
RxSwift 시작하기
RxSwift 시작하기RxSwift 시작하기
RxSwift 시작하기
 
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
Swift - 혼자 공부하면 분명히 안할테니까 같이 공부하기
 
The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212The Ring programming language version 1.10 book - Part 54 of 212
The Ring programming language version 1.10 book - Part 54 of 212
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
 
Pre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to ElixirPre-Bootcamp introduction to Elixir
Pre-Bootcamp introduction to Elixir
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Code as data as code.
Code as data as code.Code as data as code.
Code as data as code.
 
Introduzione a C#
Introduzione a C#Introduzione a C#
Introduzione a C#
 
Functional Pe(a)rls version 2
Functional Pe(a)rls version 2Functional Pe(a)rls version 2
Functional Pe(a)rls version 2
 
Fertile Ground: The Roots of Clojure
Fertile Ground: The Roots of ClojureFertile Ground: The Roots of Clojure
Fertile Ground: The Roots of Clojure
 
Ricky Bobby's World
Ricky Bobby's WorldRicky Bobby's World
Ricky Bobby's World
 
Mongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is BrightMongoose v3 :: The Future is Bright
Mongoose v3 :: The Future is Bright
 
Fact, Fiction, and FP
Fact, Fiction, and FPFact, Fiction, and FP
Fact, Fiction, and FP
 
Elm: give it a try
Elm: give it a tryElm: give it a try
Elm: give it a try
 
The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184The Ring programming language version 1.5.3 book - Part 44 of 184
The Ring programming language version 1.5.3 book - Part 44 of 184
 
Functional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures editionFunctional Pe(a)rls - the Purely Functional Datastructures edition
Functional Pe(a)rls - the Purely Functional Datastructures edition
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Intro
IntroIntro
Intro
 
はじめてのGroovy
はじめてのGroovyはじめてのGroovy
はじめてのGroovy
 

Similar to Groovy puzzlers jug-moscow-part 2

The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 Seasons
Baruch Sadogursky
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
Arturo Herrero
 
The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)
GroovyPuzzlers
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
Husain Dalal
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
Scott Leberknight
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
Aleksandar Prokopec
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
ikdysfm
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
kesav24
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
niklal
 
Lecture on Rubinius for Compiler Construction at University of Twente
Lecture on Rubinius for Compiler Construction at University of TwenteLecture on Rubinius for Compiler Construction at University of Twente
Lecture on Rubinius for Compiler Construction at University of Twente
Dirkjan Bussink
 
Clojure入門
Clojure入門Clojure入門
Clojure入門
Naoyuki Kakuda
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
JkPoppy
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
潤一 加藤
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
Ashal aka JOKER
 
Exhibition of Atrocity
Exhibition of AtrocityExhibition of Atrocity
Exhibition of Atrocity
Michael Pirnat
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...Revolution Analytics
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
garux
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
Brian Lonsdorf
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
진성 오
 

Similar to Groovy puzzlers jug-moscow-part 2 (20)

The Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 SeasonsThe Groovy Puzzlers – The Complete 01 and 02 Seasons
The Groovy Puzzlers – The Complete 01 and 02 Seasons
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)The groovy puzzlers (as Presented at Gr8Conf US 2014)
The groovy puzzlers (as Presented at Gr8Conf US 2014)
 
Monadologie
MonadologieMonadologie
Monadologie
 
Groovy grails types, operators, objects
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
Lecture on Rubinius for Compiler Construction at University of Twente
Lecture on Rubinius for Compiler Construction at University of TwenteLecture on Rubinius for Compiler Construction at University of Twente
Lecture on Rubinius for Compiler Construction at University of Twente
 
Clojure入門
Clojure入門Clojure入門
Clojure入門
 
Swift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdfSwift 5.1 Language Guide Notes.pdf
Swift 5.1 Language Guide Notes.pdf
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
Exhibition of Atrocity
Exhibition of AtrocityExhibition of Atrocity
Exhibition of Atrocity
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
 
Introdução ao Perl 6
Introdução ao Perl 6Introdução ao Perl 6
Introdução ao Perl 6
 
Oh Composable World!
Oh Composable World!Oh Composable World!
Oh Composable World!
 
Swift 함수 커링 사용하기
Swift 함수 커링 사용하기Swift 함수 커링 사용하기
Swift 함수 커링 사용하기
 

More from Evgeny Borisov

Java 8 puzzlers
Java 8 puzzlersJava 8 puzzlers
Java 8 puzzlers
Evgeny Borisov
 
Spring puzzlers 2
Spring puzzlers 2Spring puzzlers 2
Spring puzzlers 2
Evgeny Borisov
 
мифы о спарке
мифы о спарке мифы о спарке
мифы о спарке
Evgeny Borisov
 
Spring data jee conf
Spring data jee confSpring data jee conf
Spring data jee conf
Evgeny Borisov
 
Groovy jug-moscow-part 1
Groovy jug-moscow-part 1Groovy jug-moscow-part 1
Groovy jug-moscow-part 1
Evgeny Borisov
 
Spock
SpockSpock
Spring the ripper
Spring the ripperSpring the ripper
Spring the ripper
Evgeny Borisov
 
Javaone 2013 moscow gradle english
Javaone 2013 moscow gradle   englishJavaone 2013 moscow gradle   english
Javaone 2013 moscow gradle english
Evgeny Borisov
 
Javaone 2013 moscow gradle
Javaone 2013 moscow gradleJavaone 2013 moscow gradle
Javaone 2013 moscow gradleEvgeny Borisov
 

More from Evgeny Borisov (10)

Java 8 puzzlers
Java 8 puzzlersJava 8 puzzlers
Java 8 puzzlers
 
Spring puzzlers 2
Spring puzzlers 2Spring puzzlers 2
Spring puzzlers 2
 
мифы о спарке
мифы о спарке мифы о спарке
мифы о спарке
 
Spring puzzlers
Spring puzzlersSpring puzzlers
Spring puzzlers
 
Spring data jee conf
Spring data jee confSpring data jee conf
Spring data jee conf
 
Groovy jug-moscow-part 1
Groovy jug-moscow-part 1Groovy jug-moscow-part 1
Groovy jug-moscow-part 1
 
Spock
SpockSpock
Spock
 
Spring the ripper
Spring the ripperSpring the ripper
Spring the ripper
 
Javaone 2013 moscow gradle english
Javaone 2013 moscow gradle   englishJavaone 2013 moscow gradle   english
Javaone 2013 moscow gradle english
 
Javaone 2013 moscow gradle
Javaone 2013 moscow gradleJavaone 2013 moscow gradle
Javaone 2013 moscow gradle
 

Recently uploaded

Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
Sebastiano Panichella
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Matjaž Lipuš
 
Eureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 PresentationEureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 Presentation
Access Innovations, Inc.
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
OWASP Beja
 
Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
IP ServerOne
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Sebastiano Panichella
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
khadija278284
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
Faculty of Medicine And Health Sciences
 
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Orkestra
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
Sebastiano Panichella
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
OECD Directorate for Financial and Enterprise Affairs
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
Vladimir Samoylov
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
Howard Spence
 

Recently uploaded (13)

Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...Announcement of 18th IEEE International Conference on Software Testing, Verif...
Announcement of 18th IEEE International Conference on Software Testing, Verif...
 
Bitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXOBitcoin Lightning wallet and tic-tac-toe game XOXO
Bitcoin Lightning wallet and tic-tac-toe game XOXO
 
Eureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 PresentationEureka, I found it! - Special Libraries Association 2021 Presentation
Eureka, I found it! - Special Libraries Association 2021 Presentation
 
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
0x01 - Newton's Third Law:  Static vs. Dynamic Abusers0x01 - Newton's Third Law:  Static vs. Dynamic Abusers
0x01 - Newton's Third Law: Static vs. Dynamic Abusers
 
Acorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutesAcorn Recovery: Restore IT infra within minutes
Acorn Recovery: Restore IT infra within minutes
 
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...Doctoral Symposium at the 17th IEEE International Conference on Software Test...
Doctoral Symposium at the 17th IEEE International Conference on Software Test...
 
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdfBonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
Bonzo subscription_hjjjjjjjj5hhhhhhh_2024.pdf
 
Obesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditionsObesity causes and management and associated medical conditions
Obesity causes and management and associated medical conditions
 
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
Sharpen existing tools or get a new toolbox? Contemporary cluster initiatives...
 
International Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software TestingInternational Workshop on Artificial Intelligence in Software Testing
International Workshop on Artificial Intelligence in Software Testing
 
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
Competition and Regulation in Professional Services – KLEINER – June 2024 OEC...
 
Getting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control TowerGetting started with Amazon Bedrock Studio and Control Tower
Getting started with Amazon Bedrock Studio and Control Tower
 
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptxsomanykidsbutsofewfathers-140705000023-phpapp02.pptx
somanykidsbutsofewfathers-140705000023-phpapp02.pptx
 

Groovy puzzlers jug-moscow-part 2

  • 1.
  • 2.
  • 3. 1. Два клЕвых пацана на сцене 2. Прикольные загадки 3. Вы голосуете за правильный ответ 4. Мы швыряемся вещами
  • 4.
  • 5.
  • 6.
  • 7.
  • 9.
  • 10. (-3).abs() int value = -3 value.abs()
  • 11.
  • 13.
  • 14.
  • 15. -3 Caught: java.lang.NullPointerException: Cannot invoke method abs() on null object java.lang.NullPointerException: Cannot invoke method abs() on null object at AbsolutelyGroovy.run(AbsolutelyGroovy.groovy:7) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) println (-3).abs()
  • 16. “Все проблемы в программировании можно решить добавив пару скобок” John McCarthy, изобретатель LISP
  • 17. println ((-3).abs()) int value = -3 println value.abs()
  • 18.
  • 19. boolean isPrime(def x) { if (x == 2) return true int limit = Math.sqrt(x) + 1 (2..limit).each { if (x % it == 0) { return false } } true } println isPrime("4" as Double)
  • 20.
  • 21. boolean isPrime(def x) { if (x == 2) return true int limit = Math.sqrt(x) + 1 (2..limit).each { if (x % it == 0) { return false } } true } println isPrime("4" as Double)
  • 22. boolean isPrime(def x) { if (x == 2) return true int limit = Math.sqrt(x) + 1 (2..limit).each { if (x % it == 0) { return false } } true } println isPrime("4" as Double)
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 30.
  • 31. class Conference {def name; def year} def gr = new Conference(name: 'Greach', year: 2014) gr.each {println it}
  • 32.
  • 33.
  • 34.
  • 35. List<Integer> list = [56, '9', 74] def max = list.max { item -> (item < 50) ? item : null } println max
  • 36. List<Integer> list = [56, '9', 74] def max = list.max { item -> (item < 50) ? item : null } println max
  • 37. >groovysh (('9' as Character) as Integer) ===> 57
  • 38. List<Integer> list = [56, 57, 74] def max = list.max { item -> (item < 50) ? item : null } println max
  • 39.
  • 40. List<Integer> list = [56, '9', 74] def max = list.max { item -> (item < 50) ? item : null } println max
  • 41.
  • 42. def random = new Random() def randomList = [] 0..10.each {randomList << random.nextInt()} assert randomList.max{ null } == randomList[0]
  • 43.
  • 44.
  • 46.
  • 47. Closure ктоУбийца() { { -> 'Мориарти.' } } Closure ктоУбийца() { return { 'Мориарти.' } }
  • 48.
  • 49. class CountDown { int counter = 10 } CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown } } println finalCountDown().counter
  • 50. class CountDown { int counter = 10 } CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown } } println finalCountDown().counter
  • 51.
  • 52. class CountDown { int counter = 10 } CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { ignored.printStackTrace() countDown.counter = Integer.MIN_VALUE } finally { return countDown } } println finalCountDown().counter org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '9' with class 'java.lang.Integer' to class 'CountDown’ -2147483648
  • 53.
  • 54.
  • 55. class CountDown { int counter = 10 } CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown } 42 } println finalCountDown().counter
  • 56. class CountDown { int counter = 10 } CountDown finalCountDown() { def countDown = new CountDown() try { countDown.counter = --countDown.counter } catch (ignored) { println "That will never happen." countDown.counter = Integer.MIN_VALUE } finally { return countDown } 42 } println finalCountDown().counter
  • 57.
  • 58.
  • 59.
  • 60.
  • 61. def key = 'x' def map = [key: 'treasure'] def value = map.get(key) println value
  • 62. def key = 'x' def map = [key: 'treasure'] def value = map.get(key) println value
  • 63.
  • 64.
  • 65. 1.def map = [(key): 'treasure'] 2.map.put(key, 'treasure') 3.map[key] = 'treasure' 4.map." " = 'treasure'
  • 66.
  • 67. def map = [2: 'treasure'] def key = 2 def value = map."$key" println value
  • 68. def map = [2: 'treasure'] def key = 2 def value = map."$key" println value
  • 69.
  • 70. def map = [2: 'treasure'] println map.keySet().first().class.name java.lang.Integer
  • 71.
  • 72. def key = 'x' def map = ["${key}": 'treasure'] def value = map['x'] println value
  • 73. def key = 'x' def map = ["${key}": 'treasure'] def value = map['x'] println value
  • 74.
  • 75. def map = ["${key}": 'treasure'] println map.keySet().first().class.name org.codehaus.groovy.runtime.GStringImpl
  • 76.
  • 77. def range = 1.0..10.0 assert range.contains(5.0) println range.contains(5.6)
  • 78.
  • 79.
  • 80.
  • 81.
  • 82. Iterator iterator = (1.0..10.0).iterator() while (iterator.hasNext()) { print "${iterator.next()} " } 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
  • 83.
  • 84.
  • 85.
  • 86.
  • 88.
  • 89. [0..9].each { println(it - 1) } Это неправильные скобки!
  • 90. (0..9).each { println(it - 1) } Другое дело!
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96. [0, 2, 3, 4, 5, 6, 7, 8, 9]
  • 97.
  • 98. List<Long> list = [1,2,3] def now = new Date() list << now println list
  • 99. List<Long> list = [1,2,3] def now = new Date() list << now println list
  • 100.
  • 101.
  • 102.
  • 103.
  • 104. List<Long> list = [1,2,3] def now = new Date() list << now list << 'foo' println list*.class.name [java.lang.Long, java.lang.Long, java.lang.Long, java.util.Date, java.lang.String]
  • 105.
  • 106.
  • 107. double value = 3 println "$value.14".isDouble()
  • 108.
  • 109.
  • 110.
  • 111. double value = 3 println "$value.14".isDouble()
  • 112.
  • 113.
  • 114.
  • 116. class Invite { int attending = 1 } def invite = new Invite() def attendees = (invite.attending) +1 println attendees Это Андрей Это Джигурда
  • 117. class Invite { int attending = 1 } def invite = new Invite() def attendees = (invite.attending) +1 println attendees
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123. def attendees = (new Invite().attending) + 1 println attendees
  • 124. def invite = new Invite() def attendees = invite.attending +1 Как убрать скобки?!
  • 125. def invite = new Invite() def attendees = ((invite.attending)) +1 Наоборот добавить. Скобки никогда нельзя убирать!
  • 126.
  • 127. class MrHyde { def me() { return this } } class DrJekyll { } DrJekyll.mixin MrHyde def drJekyll = new DrJekyll().me() as DrJekyll def mrHide = new DrJekyll().me() println "$drJekyll and $mrHide, are they the same? ${(drJekyll.class).equals(mrHide.class)}"
  • 128. class MrHyde { def me() { return this } } class DrJekyll { } DrJekyll.mixin MrHyde def drJekyll = new DrJekyll().me() as DrJekyll def mrHide = new DrJekyll().me() println "$drJekyll and $mrHide, are they the same? ${(drJekyll.class).equals(mrHide.class)}"
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134. def x = int println x if ((x = long)) { println x } if (x = boolean ) { println x }
  • 135. def x = int println x if ((x = long)) { println x } if (x = boolean ) { println x }
  • 136.
  • 137.
  • 138.
  • 139.
  • 140. class VanHalen { public static jump() { "Here are the ${lyrics()}" } def methodMissing(String name, def args) { 'lyrics' } } println VanHalen.jump()
  • 141. class VanHalen { public static jump() { "Here are the ${lyrics()}" } def methodMissing(String name, def args) { 'lyrics' } } println VanHalen.jump()
  • 142.
  • 143.
  • 144.
  • 145.
  • 146. class VanHalen { public static jump() { "Here are the ${lyrics()}" } static $static_methodMissing(String name, def args) { 'lyrics' } } println VanHalen.jump()
  • 147. class VanHalen { public jump() { "Here are the ${lyrics()}" } def methodMissing(String name, def args) { 'lyrics' } } println new VanHalen().jump()
  • 148.
  • 149. def map = [metaClass: ‘frequency'] println "What's the $map.metaClass, Барух?"
  • 150.
  • 151.
  • 153.
  • 154.
  • 155. 1. Пишите читабельный код 2. Комментируйте все трюки 3. Иногда это баг 4. Пользуйте static code analysis - intellij IDEA! 5. Rtfm 6. Don’t code like my brother

Editor's Notes

  1. CURRENT – FRED NEXT - FRED
  2. List<Integer> list = [56, '9', 74,23,25,20] def max = list.max { item -> (item < 50) ? item : null } println max Это ищет максимальный элемент в листе из тех, что меньше 50
  3. CURRENT – JB NEXT - FRED
  4. 1985
  5. CURRENT – FRED NEXT - JB
  6. CURRENT – FRED NEXT - JB
  7. CURRENT – FRED NEXT - JB
  8. CURRENT – FRED NEXT - JB
  9. CURRENT – FRED NEXT - JB
  10. CURRENT – FRED NEXT - JB
  11. CURRENT – FRED NEXT - JB
  12. CURRENT – FRED NEXT - JB
  13. CURRENT – FRED NEXT - JB
  14. CURRENT – FRED NEXT - JB
  15. CURRENT – FRED NEXT - JB
  16. CURRENT – FRED NEXT - JB
  17. CURRENT – FRED NEXT - JB
  18. CURRENT – FRED NEXT - JB
  19. CURRENT – FRED NEXT - JB
  20. CURRENT – FRED NEXT - JB
  21. CURRENT – FRED NEXT - JB
  22. CURRENT – FRED NEXT - JB
  23. CURRENT – FRED NEXT - JB
  24. CURRENT – FRED NEXT - JB
  25. CURRENT – FRED NEXT - JB
  26. CURRENT – JB NEXT - FRED
  27. CURRENT – JB NEXT - FRED
  28. CURRENT – JB NEXT - FRED
  29. CURRENT – JB NEXT - FRED
  30. CURRENT – JB NEXT - FRED
  31. CURRENT – JB NEXT - FRED
  32. CURRENT – JB NEXT - FRED
  33. CURRENT – JB NEXT - FRED
  34. CURRENT – JB NEXT - FRED
  35. CURRENT – FRED
  36. CURRENT – FRED
  37. CURRENT – FRED
  38. CURRENT – FRED
  39. CURRENT – FRED
  40. CURRENT – FRED
  41. CURRENT – FRED
  42. CURRENT – FRED
  43. CURRENT – FRED
  44. CURRENT – FRED
  45. CURRENT – FRED
  46. CURRENT – JB NEXT - FRED
  47. CURRENT – JB NEXT - FRED
  48. CURRENT – JB NEXT - FRED
  49. CURRENT – JB NEXT - FRED
  50. CURRENT – JB NEXT - FRED
  51. CURRENT – JB NEXT - FRED
  52. CURRENT – FRED NEXT - JB
  53. CURRENT – FRED NEXT - JB
  54. CURRENT – FRED NEXT - JB
  55. CURRENT – FRED NEXT - JB
  56. CURRENT – FRED NEXT - JB
  57. CURRENT – FRED NEXT - JB
  58. CURRENT – FRED NEXT - JB (1983)
  59. CURRENT – FRED NEXT - JB
  60. CURRENT – FRED NEXT - JB
  61. CURRENT – FRED NEXT - JB
  62. CURRENT – FRED NEXT - JB
  63. CURRENT – FRED NEXT - JB
  64. CURRENT – FRED NEXT - JB
  65. CURRENT – FRED NEXT - JB
  66. CURRENT – FRED NEXT - JB
  67. CURRENT – JB NEXT - FRED 1993
  68. CURRENT – JB NEXT - FRED
  69. CURRENT – JB NEXT - FRED
  70. CURRENT – JB NEXT - FRED
  71. CURRENT – JB NEXT - FRED
  72. CURRENT – JB NEXT - FRED