SlideShare a Scribd company logo
Submit Search
Upload
Login
Signup
Discovering functional treasure in idiomatic Groovy
Report
Naresha K
Follow
Developer | Technical Excellence Coach | Consultant | Trainer at Independent
Oct. 9, 2014
•
0 likes
•
1,656 views
1
of
42
Discovering functional treasure in idiomatic Groovy
Oct. 9, 2014
•
0 likes
•
1,656 views
Download Now
Download to read offline
Report
Technology
Slides of my talk at Functional Conf 2014 - Oct 9-11, Bangalore
Naresha K
Follow
Developer | Technical Excellence Coach | Consultant | Trainer at Independent
Recommended
Functional Programming with Groovy
Arturo Herrero
13.4K views
•
71 slides
Part II: LLVM Intermediate Representation
Wei-Ren Chen
868 views
•
39 slides
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Netcetera
5.2K views
•
22 slides
Hadoop Map Reduce 程式設計
Wei-Yu Chen
40.4K views
•
106 slides
Deep dive into Coroutines on JVM @ KotlinConf 2017
Roman Elizarov
5.8K views
•
107 slides
Introduction to Coroutines @ KotlinConf 2017
Roman Elizarov
8.3K views
•
145 slides
More Related Content
What's hot
Gdb cheat sheet
Piyush Mittal
5.8K views
•
2 slides
02 Arrays And Memory Mapping
Qundeel
6K views
•
24 slides
Google guava
Thomas Ferris Nicolaisen
9.8K views
•
27 slides
පැස්කල්
Tennyson
1.3K views
•
10 slides
design and analysis of algorithm Lab files
Nitesh Dubey
674 views
•
28 slides
Inside MongoDB: the Internals of an Open-Source Database
Mike Dirolf
52.3K views
•
25 slides
What's hot
(20)
Gdb cheat sheet
Piyush Mittal
•
5.8K views
02 Arrays And Memory Mapping
Qundeel
•
6K views
Google guava
Thomas Ferris Nicolaisen
•
9.8K views
පැස්කල්
Tennyson
•
1.3K views
design and analysis of algorithm Lab files
Nitesh Dubey
•
674 views
Inside MongoDB: the Internals of an Open-Source Database
Mike Dirolf
•
52.3K views
Intro to py spark (and cassandra)
Jon Haddad
•
6.7K views
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Jorge Vásquez
•
398 views
Functional Programming by Examples using Haskell
goncharenko
•
4.9K views
iOS 메모리관리
Changwon National University
•
7.7K views
Method Handles in Java
hendersk
•
11.9K views
Lazy java
Mario Fusco
•
6.3K views
Big picture of category theory in scala with deep dive into contravariant and...
Piotr Paradziński
•
829 views
Optimizing Cypher Queries in Neo4j
Neo4j
•
10.7K views
Functional Error Handling with Cats
Mark Canlas
•
478 views
Hierarchical data models in Relational Databases
navicorevn
•
12.1K views
Kotlin Collections
Halil Özcan
•
126 views
Dart ( 1 )
Simplife EG
•
170 views
Dart ( 2 )
Simplife EG
•
174 views
Kotlin Coroutines in Practice @ KotlinConf 2018
Roman Elizarov
•
4.6K views
Similar to Discovering functional treasure in idiomatic Groovy
Go(lang) for the Rubyist
Mark
6.6K views
•
63 slides
Ruby 入門 第一次就上手
Wen-Tien Chang
3.5K views
•
62 slides
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
1.3K views
•
58 slides
Ruby 程式語言入門導覽
Wen-Tien Chang
1.6K views
•
58 slides
Clojure for Java developers - Stockholm
Jan Kronquist
1.7K views
•
52 slides
RubyConf Portugal 2014 - Why ruby must go!
Gautam Rege
12.7K views
•
137 slides
Similar to Discovering functional treasure in idiomatic Groovy
(20)
Go(lang) for the Rubyist
Mark
•
6.6K views
Ruby 入門 第一次就上手
Wen-Tien Chang
•
3.5K views
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
•
1.3K views
Ruby 程式語言入門導覽
Wen-Tien Chang
•
1.6K views
Clojure for Java developers - Stockholm
Jan Kronquist
•
1.7K views
RubyConf Portugal 2014 - Why ruby must go!
Gautam Rege
•
12.7K views
Groovy!
Petr Giecek
•
470 views
NUS iOS Swift Talk
Gabriel Lim
•
2.2K views
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
•
259 views
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
•
481 views
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
•
289 views
Ruby 程式語言簡介
Wen-Tien Chang
•
2.7K views
Plugin jQuery, Design Patterns
Robert Casanova
•
19.7K views
Building DSLs with Xtext - Eclipse Modeling Day 2009
Heiko Behrens
•
2.2K views
ScotRuby - Dark side of ruby
Gautam Rege
•
1.3K views
Es.next
Ignacio Gil
•
279 views
Rails by example
Angelo van der Sijpt
•
1.1K views
Crunching data with go: Tips, tricks, use-cases
Sergii Khomenko
•
1.1K views
Ruby ile tanışma!
Uğur Özyılmazel
•
733 views
from Ruby to Objective-C
Eddie Kao
•
1.4K views
More from Naresha K
The Groovy Way of Testing with Spock
Naresha K
240 views
•
40 slides
Evolving with Java - How to Remain Effective
Naresha K
243 views
•
74 slides
Take Control of your Integration Testing with TestContainers
Naresha K
267 views
•
50 slides
Implementing Resilience with Micronaut
Naresha K
339 views
•
20 slides
Take Control of your Integration Testing with TestContainers
Naresha K
273 views
•
21 slides
Favouring Composition - The Groovy Way
Naresha K
229 views
•
48 slides
More from Naresha K
(20)
The Groovy Way of Testing with Spock
Naresha K
•
240 views
Evolving with Java - How to Remain Effective
Naresha K
•
243 views
Take Control of your Integration Testing with TestContainers
Naresha K
•
267 views
Implementing Resilience with Micronaut
Naresha K
•
339 views
Take Control of your Integration Testing with TestContainers
Naresha K
•
273 views
Favouring Composition - The Groovy Way
Naresha K
•
229 views
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Naresha K
•
293 views
What's in Groovy for Functional Programming
Naresha K
•
275 views
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Naresha K
•
208 views
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Naresha K
•
162 views
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Naresha K
•
498 views
Implementing Cloud-Native Architectural Patterns with Micronaut
Naresha K
•
398 views
Groovy - Why and Where?
Naresha K
•
78 views
Leveraging Micronaut on AWS Lambda
Naresha K
•
310 views
Groovy Refactoring Patterns
Naresha K
•
514 views
Implementing Cloud-native Architectural Patterns with Micronaut
Naresha K
•
664 views
Effective Java with Groovy
Naresha K
•
501 views
Evolving with Java - How to remain Relevant and Effective
Naresha K
•
294 views
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K
•
308 views
Beyond Lambdas & Streams - Functional Fluency in Java
Naresha K
•
186 views
Recently uploaded
Uber Clone Script - Keys to Understanding the Ride Hailing Industry
eSiteWorld TechnoLabs Pvt. Ltd.
26 views
•
8 slides
V3Cube Gojek Clone - Rebrand With Superiority
V3cube
61 views
•
10 slides
"Stateful app as an efficient way to build dispatching for riders and drivers...
Fwdays
37 views
•
46 slides
Solving today’s Traffic Problems with Sustainable Ride Hailing Solution
On Demand Clone
27 views
•
9 slides
Common WordPress APIs - Options API
Jonathan Bossenger
33 views
•
10 slides
h2 meet pdf test.pdf
JohnLee971654
64 views
•
4 slides
Recently uploaded
(20)
Uber Clone Script - Keys to Understanding the Ride Hailing Industry
eSiteWorld TechnoLabs Pvt. Ltd.
•
26 views
V3Cube Gojek Clone - Rebrand With Superiority
V3cube
•
61 views
"Stateful app as an efficient way to build dispatching for riders and drivers...
Fwdays
•
37 views
Solving today’s Traffic Problems with Sustainable Ride Hailing Solution
On Demand Clone
•
27 views
Common WordPress APIs - Options API
Jonathan Bossenger
•
33 views
h2 meet pdf test.pdf
JohnLee971654
•
64 views
Accelerating Data Science through Feature Platform, Transformers and GenAI
FeatureByte
•
95 views
Product Research Presentation-Maidy Veloso.pptx
MaidyVeloso
•
22 views
Getting your enterprise ready for Microsoft 365 Copilot
Vignesh Ganesan I Microsoft MVP
•
217 views
Mule Meetup Calgary- API Governance & Conformance.pdf
NithaJoseph4
•
53 views
Common WordPress APIs_ Settings API
Jonathan Bossenger
•
29 views
Product Listing Presentation-Maidy Veloso.pptx
MaidyVeloso
•
18 views
Future of Skills
Alison B. Lowndes
•
73 views
Safe Community Call #12.pdf
LornyPfeifer
•
29 views
Chandrayaan 3.pptx
PrasunJha12
•
31 views
Product Research Presentation
DeahJadeArellano
•
25 views
Meetup_adessoCamunda_2023-09-13_Part1&2_en.pdf
MariaAlcantara50
•
40 views
Webhook Testing Strategy
Dimpy Adhikary
•
82 views
"Architecture assessment from classics to details", Dmytro Ovcharenko
Fwdays
•
42 views
How is AI changing journalism? Strategic considerations for publishers and ne...
Damian Radcliffe
•
117 views
Discovering functional treasure in idiomatic Groovy
1.
Discovering Functional Treasure in Idiomatic
Groovy Naresha K Enteleki Solutions ! naresha.k@gmail.com @naresha_k
3.
An imperative language on
JVM A dynamic with Functional Flavour
4.
The origin http://radio-weblogs.com/0112098/2003/08/29.html initial idea
was to make a little dynamic language which compiles directly to Java classes and provides all the nice (alleged) productivity benefits - James Strachan
5.
Prerequisites
6.
Function ! def sayHello(){! ! println
'Hello'! }! ! sayHello()!
8.
Closure def wish =
{! ! println "Hello"! }! ! wish() def wishFriend = {! ! println "Hello $it"! } ! ! wishFriend 'Raj' def wishFriend = { to ->! ! println "Hello $to"! }! ! wishFriend 'Raj'
9.
Closure - No
Arg def wish = { ->! ! println "Hello"! }! ! wish()
10.
Closure - Multiple
args def wishWithMessage = { to, message ->! ! println "Hello $to, $message"! }! ! wishWithMessage "Raj", "Good Evening"
11.
Closures = Power
functions def wishWithMessage = { to, message ->! ! println "Hello $to, $message"! }! ! wishWithMessage "Raj", "Good Evening" def <var> = <closure> Functions as Values
12.
Sample Data import groovy.transform.ToString! ! @ToString(includeNames=true)! class
Geek{! String name! int age! List<String> languages! } def geeks = []! geeks << new Geek(name: 'Raj', age: 24, ! ! languages: ['Java', 'Groovy'])! geeks << new Geek(name: 'Arun', age: 35, ! ! languages: ['Java', 'Scala', 'Clojure'])! geeks << new Geek(name: 'Kumar', age: 28, ! ! languages: ['Groovy', 'Scala'])!
13.
Geeks who can
speak Groovy def findGroovyGeeksImperative(geeks){! ! def groovyGeeks = []! ! for(geek in geeks){! ! if(geek.languages.contains('Groovy')){! ! groovyGeeks << geek! ! }! ! }! ! groovyGeeks! }
14.
Geeks who can
speak Groovy def findGroovyGeeksImperative(geeks){! ! def groovyGeeks = []! ! for(geek in geeks){! ! if(geek.languages.contains('Groovy')){! ! groovyGeeks << geek! ! }! ! }! ! groovyGeeks! }
15.
Generalised def findGeeks(geeks, String
language){! ! def knowsLang = []! ! for(geek in geeks){! ! if(geek.languages.contains(language)){! ! knowsLang << geek! ! }! ! }! ! knowsLang! }!
16.
Towards Idiomatic Groovy def
findGroovyGeeksFunctional(geeks){! ! geeks.findAll({it.languages.contains('Groovy')})! } def findGroovyGeeksFunctional(geeks){! ! geeks.findAll() {it.languages.contains('Groovy')}! } def findGroovyGeeksFunctional(geeks){! ! geeks.findAll {it.languages.contains('Groovy')}! }
17.
Reusable def knowsGroovy =
{ geek -> ! ! geek.languages.contains('Groovy')! }! ! def findGeeksFunctional(geeks, criterion){! ! geeks.findAll(criterion)! }! ! println findGeeksFunctional(geeks, knowsGroovy) Higher Order Functions
18.
Strategy Pattern def knowsGroovy
= { geek -> ! ! geek.languages.contains('Groovy')! }! ! def knowsClojure = { geek ->! ! geek.languages.contains('Clojure')! }! ! def findGeeksFunctional(geeks, criterion){! ! geeks.findAll(criterion)! }! ! println findGeeksFunctional(geeks, knowsGroovy)! println findGeeksFunctional(geeks, knowsClojure)
19.
Command Pattern def sayHello
= {! ! println "Hello"! }! ! def sayHi = {! ! println "Hi"! }! ! [sayHello, sayHi].each{ command ->! ! command()! }
20.
Execute Around def sayHello
= {! ! println "Hello"! }! ! def sayHi = {! ! println "Hi"! }! ! [sayHello, sayHi].each{ command ->! ! println "Before Command"! ! command()! ! println "After Command"! }
21.
Code Smell! def knowsGroovy
= { geek -> ! ! geek.languages.contains('Groovy')! }! ! def knowsClojure = { geek ->! ! geek.languages.contains('Clojure')! }
22.
After DRYing def knowsLanguage
= { geek, language ->! ! geek.languages.contains(language)! }! ! def findGeeks(geeks, criterion, String language){! ! geeks.findAll {criterion(it, language)}! }! ! println findGeeks(geeks, knowsLanguage, 'Groovy')
23.
A Better Approach def
knowsLanguage = { geek, language ->! ! geek.languages.contains(language)! }! ! def knowsGroovy = knowsLanguage.rcurry('Groovy')! def knowsClojure = knowsLanguage.rcurry('Clojure')! ! def findGeeks(geeks, criterion){! ! geeks.findAll(criterion)! }! ! println findGeeks(geeks, knowsGroovy)! println findGeeks(geeks, knowsClojure) Curried Functions
24.
Geeks • Knows Groovy •
At least 25 years old
25.
Composing em def atleast25YearsOld
= { geek ->! ! geek.age >= 25! }! ! def findGeeks(geeks, criterion){! ! geeks.findAll(criterion)! }! ! def findGroovyGeeks = (this.&findGeeks)! ! .rcurry(knowsGroovy)! def findGeeksAtLeast25 = (this.&findGeeks)! ! .rcurry(atleast25YearsOld) def findGroovyGeeksOlderThan24 = ! ! findGeeksAtLeast25 << findGroovyGeeks! ! println findGroovyGeeksOlderThan24(geeks) Function Composition
26.
Towards Immutable Data def
geeks = []! geeks << new Geek(name: 'Raj', age: 24, ! ! languages: ['Java', 'Groovy'])! geeks << new Geek(name: 'Arun', age: 35, ! ! languages: ['Java', 'Scala', 'Clojure'])! geeks << new Geek(name: 'Kumar', age: 28, ! ! languages: ['Groovy', 'Scala']) geeks2 = geeks + new Geek(name: 'Mark', age: 40, ! ! languages: ['Lisp', 'Haskell']) geeksImmutable = geeks.asImmutable()
27.
Towards Immutable Data
… def geeksOrderedByAge = geeks.sort { it.age }! println geeksOrderedByAge! println geeks def geeksOrderedByAge = geeks.sort false, { it.age }! println geeksOrderedByAge! println geeks Pure Functions
28.
Demo
29.
import groovy.transform.*! ! @TailRecursive! def factorial(number,
fact = 1){! ! number == 0 ? fact : factorial(number - 1, fact * number)! }! ! println factorial(2500G) Tail Call Optimization
30.
Prior to Groovy
2.3 def fact! fact = { number, result ->! ! number == 0 ? result : ! ! ! fact.trampoline(number-1, result * number)! }.trampoline()
31.
Slow Functions
32.
import groovy.transform.*! ! @Memoized! def timeConsumingOperation(int
number){! ! println "Performing computation"! ! number * number! }! ! println timeConsumingOperation(2)! println timeConsumingOperation(2) Memoization
33.
Map Filter Reduce println
geeks.findAll { it.languages.contains('Groovy')}! ! ! ! .collect {it.age}! ! ! ! .sum()! ! println geeks.findAll { it.languages.contains('Groovy')}! ! ! ! .collect {it.age}! ! ! ! .with{ sum()/ size()}
34.
Defer
35.
Defer import groovy.transform.*! ! class Website{! !
String address! ! @Lazy ! ! URL url = address.toURL()! }! ! ! def fuconf = new Website(address: 'http://functionalconf.com/')! println fuconf.dump()! ! def content = fuconf.url.text! println content.grep("n").size()! println fuconf.dump()! Lazy Evaluation
36.
Recursion vs Iteration
37.
Recursion vs Iteration def
ages = geeks.collect { it.age }! ! def sum! sum = { head, tail ->! ! if(!tail){! ! ! head! ! }! ! else{! ! ! head + sum(tail.head(), tail.tail())! ! }! }! ! println(sum(0, ages))
38.
Recursion vs Iteration println
ages.inject(0) { s, item ->! ! s + item! }
39.
The Ultimate Lesson https://twitter.com/mfeathers/status/29581296216
40.
Functional Treasures Functions as
values (First class citizens) Higher order functions Curried Functions Function Composition Pure Functions (Immutability) Tail Call Optimization Memoization Lazy Evaluation
41.
Welcome to
42.
References • https://github.com/naresha/functionalconf2014