Slide 1

Slide 1 text

Golo, the Tiny Language that gives super powers

Slide 2

Slide 2 text

Philippe Charrière > Technical “evangelist” & CSO @Clever_Cloud > Core committer for the eclipse/golo-lang project U@k33g_org / O @k33g

Slide 3

Slide 3 text

What is Golo?

Slide 4

Slide 4 text

A dynamic language for the JVM Built from the first day with invokedynamic Light (~700 kb) and fast (in a dynamic context) A research project by Julien Ponge http://golo-lang.org @jponge

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Why such a title? “The Tiny Language that gives super powers”

Slide 7

Slide 7 text

Thanks to Golo… I learned to read the Java doc I progressed in Java (I wasn’t a Java Ninja) I learned how to participate in an opensource project I do things I didn’t do before

Slide 8

Slide 8 text

Agenda We will not have enough time to “do the whole” of Golo

Slide 9

Slide 9 text

we are going to talk about: Golo’s simplicity: hello world, … some Golo’s specificities: DynamicObjects, Structures,… Functional Programming: Optional, Result,… Golo ❤ Java: Vert-x, … Augmenting Golo: add features to Golo

Slide 10

Slide 10 text

Golo’s Simplicity Quick landscape

Slide 11

Slide 11 text

Golo install Download Golo: http://golo-lang.org/download/ Unzip it export GOLO_HOME=~/golo-lang export PATH=$GOLO_HOME/bin:$PATH export PATH=~/:$PATH

Slide 12

Slide 12 text

Golo install You can build Golo git clone [email protected]:eclipse/golo-lang.git ./gradlew installDist

Slide 13

Slide 13 text

My first “hello world!”

Slide 14

Slide 14 text

Hello world module hello.world function main = |args| { println("Hello ") }

Slide 15

Slide 15 text

Hello world module hello.world function main = |args| { println("Hello ") }

Slide 16

Slide 16 text

Hello world module hello.world function main = |args| { println("Hello ") }

Slide 17

Slide 17 text

Hello world module hello.world function main = |args| { println("Hello ") }

Slide 18

Slide 18 text

How to run Golo code?

Slide 19

Slide 19 text

Run some Golo Code You can compile and run Or create Jar files applications (or Libraries) but…

Slide 20

Slide 20 text

Run Golo code as a script golo golo --classpath libs/*.jar --files imports/*.golo main.golo

Slide 21

Slide 21 text

Run Golo code as a script golo golo --classpath libs/*.jar --files imports/*.golo main.golo

Slide 22

Slide 22 text

Run Golo code as a script golo golo --classpath libs/*.jar --files imports/*.golo main.golo

Slide 23

Slide 23 text

Run Golo code as a script golo golo --classpath libs/*.jar --files imports/*.golo main.golo

Slide 24

Slide 24 text

Modules & Functions

Slide 25

Slide 25 text

Modules & Functions # my.lib.golo module my.lib function hello = |name| { return "Hello " + name } # short version function hi = |name| -> "Hi " + name # main.golo module myApp import my.lib function main = |args| { let name = "Baby Groot" println(hello(name)) println(hi(name)) }

Slide 26

Slide 26 text

Modules & Functions # my.lib.golo module my.lib function hello = |name| { return "Hello " + name } # short version function hi = |name| -> "Hi " + name # main.golo module myApp import my.lib function main = |args| { let name = "Baby Groot" println(hello(name)) println(hi(name)) }

Slide 27

Slide 27 text

Modules & Functions # my.lib.golo module my.lib function hello = |name| { return "Hello " + name } # short version function hi = |name| -> "Hi " + name # main.golo module myApp import my.lib function main = |args| { let name = "Baby Groot" println(hello(name)) println(hi(name)) }

Slide 28

Slide 28 text

Modules & Functions # my.lib.golo module my.lib function hello = |name| { return "Hello " + name } # short version function hi = |name| -> "Hi " + name # main.golo module myApp import my.lib function main = |args| { let name = "Baby Groot" println(hello(name)) println(hi(name)) }

Slide 29

Slide 29 text

Lambdas

Slide 30

Slide 30 text

Lambdas module lambdas function main = |args| { let add = |a, b| { return a + b } # compact version let addOne = |value| -> value + 1 println( add(40, 2) ) # 42 println( addOne(41) ) # 42 }

Slide 31

Slide 31 text

Lambdas module lambdas function main = |args| { let add = |a, b| { return a + b } # compact version let addOne = |value| -> value + 1 println( add(40, 2) ) # 42 println( addOne(41) ) # 42 }

Slide 32

Slide 32 text

Lambdas module lambdas function main = |args| { let add = |a, b| { return a + b } # compact version let addOne = |value| -> value + 1 println( add(40, 2) ) # 42 println( addOne(41) ) # 42 }

Slide 33

Slide 33 text

Some control & loop constructions

Slide 34

Slide 34 text

Control Structures

Slide 35

Slide 35 text

Control structures module control function main = |args| { let fourtyTwo = 42 if fourtyTwo is 42 { println("") } else { println("") } }

Slide 36

Slide 36 text

Control structures module control function main = |args| { let isEqualTo42 = |value| { case { when value is 42 { return " good" } when value > 42 { return " too big" } otherwise { return " too little" } } } println(isEqualTo42(42)) # good println(isEqualTo42(24)) # too little println(isEqualTo42(84)) # too big }

Slide 37

Slide 37 text

Loop Structures

Slide 38

Slide 38 text

Loop structures module loops function main = |args| { var counter = 0 while (counter < 4) { counter = counter + 1 } println(counter) # 4 }

Slide 39

Slide 39 text

Loop structures module loops function main = |args| { foreach item in [1, 2, 3, 4, 5] { if item < 3 { println(item) } } foreach item in [1, 2, 3, 4, 5] when item < 3 { println(item) } }

Slide 40

Slide 40 text

Loop structures with a guard module loops function main = |args| { foreach item in [1, 2, 3, 4, 5] { if item < 3 { println(item) } } foreach item in [1, 2, 3, 4, 5] when item < 3 { println(item) } }

Slide 41

Slide 41 text

Java types

Slide 42

Slide 42 text

Java types module javatypes function main = |args| { let vegetables = java.util.LinkedList() # no new keyword vegetables: add("") # colon notation vegetables: add("") vegetables: add("") println(vegetables) # [, , ] let mixedSalad = vegetables : stream() : map( |ingredient| -> java.lang.String.join(ingredient, ingredient, ingredient) ) : reduce("", |acc, next| -> acc + next) println(mixedSalad) # } # dot notation

Slide 43

Slide 43 text

Java types module javatypes function main = |args| { let vegetables = java.util.LinkedList() # no new keyword vegetables: add("") # colon notation vegetables: add("") vegetables: add("") println(vegetables) # [, , ] let mixedSalad = vegetables : stream() : map( |ingredient| -> java.lang.String.join(ingredient, ingredient, ingredient) ) : reduce("", |acc, next| -> acc + next) println(mixedSalad) # } # dot notation

Slide 44

Slide 44 text

Java types module javatypes function main = |args| { let vegetables = java.util.LinkedList() # no new keyword vegetables: add("") # colon notation vegetables: add("") vegetables: add("") println(vegetables) # [, , ] let mixedSalad = vegetables : stream() : map( |ingredient| -> java.lang.String.join(ingredient, ingredient, ingredient) ) : reduce("", |acc, next| -> acc + next) println(mixedSalad) # } # dot notation

Slide 45

Slide 45 text

Simpler way: Collection literals module javatypes function main = |args| { let vegetables = list["", "", ""] let mixedSalad = vegetables : map(|ingredient| -> "": join(ingredient,ingredient,ingredient)) : reduce("", |acc, next| -> acc + next) println(mixedSalad) # } java.util.LinkedList()

Slide 46

Slide 46 text

Simpler way: Collection literals module javatypes function main = |args| { let vegetables = list["", "", ""] let mixedSalad = vegetables : map(|ingredient| -> ingredient: join(ingredient,ingredient)) : reduce("", |acc, next| -> acc + next) println(mixedSalad) # }

Slide 47

Slide 47 text

Golo’s Specificities

Slide 48

Slide 48 text

no class with Golo❗

Slide 49

Slide 49 text

DynamicObjects

Slide 50

Slide 50 text

DynamicObject module clark function main = |args| { let clark = DynamicObject() : firstName("Clark"): lastName("Kent") : define("hello", |this| { println("Hello, I'm " + this: firstName()) return this }) : define("say", |this, message| { println(message) return this }) clark: hello(): say("hi!") # Hello, I'm Clark\n hi! clark: firstName("CLARK") clark: hello() # Hello, I'm CLARK clark: define("yo", |this| -> println("yo!")) }

Slide 51

Slide 51 text

DynamicObject module clark function main = |args| { let clark = DynamicObject() : firstName("Clark"): lastName("Kent") : define("hello", |this| { println("Hello, I'm " + this: firstName()) return this }) : define("say", |this, message| { println(message) return this }) clark: hello(): say("hi!") # Hello, I'm Clark\n hi! clark: firstName("CLARK") clark: hello() # Hello, I'm CLARK clark: define("yo", |this| -> println("yo!")) }

Slide 52

Slide 52 text

DynamicObject module clark function main = |args| { let clark = DynamicObject() : firstName("Clark"): lastName("Kent") : define("hello", |this| { println("Hello, I'm " + this: firstName()) return this }) : define("say", |this, message| { println(message) return this }) clark: hello(): say("hi!") # Hello, I'm Clark\n hi! clark: firstName("CLARK") clark: hello() # Hello, I'm CLARK clark: define("yo", |this| -> println("yo!")) }

Slide 53

Slide 53 text

Less dynamic but powerful: Structures

Slide 54

Slide 54 text

Structures module doeFamiliy struct human = { firstName, lastName } function main = |args| { # simple constructor let jane = human("Jane", "Doe") # constructor, then setters let john = human(): firstName("John"): lastName("Doe") println("hello we are " + jane: firstName() + " " + jane: lastName() + " and " + john: firstName() + " " + john: lastName() ) println(jane oftype doeFamiliy.types.human.class) # true }

Slide 55

Slide 55 text

Structures module doeFamiliy struct human = { firstName, lastName } function main = |args| { # simple constructor let jane = human("Jane", "Doe") # constructor, then setters let john = human(): firstName("John"): lastName("Doe") println("hello we are " + jane: firstName() + " " + jane: lastName() + " and " + john: firstName() + " " + john: lastName() ) println(jane oftype doeFamiliy.types.human.class) # true }

Slide 56

Slide 56 text

Augmenting Structure

Slide 57

Slide 57 text

Structures & Augmentations module doeFamiliy struct human = { firstName, lastName } augment human { function hello = |this| { println(this: firstName() + ": Hello!") } function hello = |this, message| { println(this: firstName() + ": Hello! " + message) } } function main = |args| { let jane = human("Jane", "Doe") jane: hello() # Jane: Hello! jane: hello("How are you doing?") # Jane: Hello! How are you doing? }

Slide 58

Slide 58 text

Structures & Augmentations module doeFamiliy struct human = { firstName, lastName } augment human { function hello = |this| { println(this: firstName() + ": Hello!") } function hello = |this, message| { println(this: firstName() + ": Hello! " + message) } } function main = |args| { let jane = human("Jane", "Doe") jane: hello() # Jane: Hello! jane: hello("How are you doing?") # Jane: Hello! How are you doing? }

Slide 59

Slide 59 text

Structures & named Augmentations module doeFamiliy struct human = { firstName, lastName } struct dog = { name } augmentation eat = { function eat = |this, something| -> println("I'm eating " + something) } augment human with eat augment dog with eat function main = |args| { let jane = human("Jane", "Doe") let cookie = dog("Cookie") jane: eat("") # I'm eating cookie: eat("") # I'm eating }

Slide 60

Slide 60 text

Golo & Functional Programming

Slide 61

Slide 61 text

NPE Java -> Optional Haskell -> Maybe Golo -> Option

Slide 62

Slide 62 text

NPE module nomore_npe struct buddy = { id, avatar } function main = |args| { let buddies = list[buddy(1, ""), buddy(2, ""), buddy(3, "")] # println(buddies: find(|buddy| -> buddy: id(): equals(3)): avatar()) # java.lang.NullPointerException println(buddies: find(|buddy| -> buddy: id(): equals(4)): avatar()) }

Slide 63

Slide 63 text

Option() / Some / None function main = |args| { let buddies = list[buddy(1, ""), buddy(2, ""), buddy(3, "")] let getBuddy = |id| -> Option( buddies: find(|buddy| -> buddy: id(): equals(id)) ) let maybePanda = getBuddy(2) let maybeNobody = getBuddy(4) println( maybePanda ) # Optional[struct buddy{id=2, avatar=}] println( maybeNobody ) # Optional.empty println( maybePanda: isSome() ) # true println( maybeNobody: isNone() ) # true println( maybePanda: orElse(buddy(0, "")) ) # struct buddy{id=2, avatar=} println( maybeNobody: orElse(buddy(0, "")) ) # struct buddy{id=0, avatar=} }

Slide 64

Slide 64 text

Option() / Some / None function main = |args| { let buddies = list[buddy(1, ""), buddy(2, ""), buddy(3, "")] let getBuddy = |id| -> Option( buddies: find(|buddy| -> buddy: id(): equals(id)) ) let maybePanda = getBuddy(2) let maybeNobody = getBuddy(4) println( maybePanda ) # Optional[struct buddy{id=2, avatar=}] println( maybeNobody ) # Optional.empty println( maybePanda: isSome() ) # true println( maybeNobody: isNone() ) # true println( maybePanda: orElse(buddy(0, "")) ) # struct buddy{id=2, avatar=} println( maybeNobody: orElse(buddy(0, "")) ) # struct buddy{id=0, avatar=} }

Slide 65

Slide 65 text

Option() / Some / None function main = |args| { let buddies = list[buddy(1, ""), buddy(2, ""), buddy(3, "")] let getBuddy = |id| -> Option( buddies: find(|buddy| -> buddy: id(): equals(id)) ) let maybePanda = getBuddy(2) let maybeNobody = getBuddy(4) println( maybePanda ) # Optional[struct buddy{id=2, avatar=}] println( maybeNobody ) # Optional.empty println( maybePanda: isSome() ) # true println( maybeNobody: isNone() ) # true println( maybePanda: orElse(buddy(0, "")) ) # struct buddy{id=2, avatar=} println( maybeNobody: orElse(buddy(0, "")) ) # struct buddy{id=0, avatar=} }

Slide 66

Slide 66 text

Option() / either() function main = |args| { let buddies = list[buddy(1, ""), buddy(2, ""), buddy(3, "")] let getBuddy = |id| -> Option( buddies: find(|buddy| -> buddy: id(): equals(id)) ) getBuddy(2) : either( |buddy| { println("Hello "+ buddy: avatar()) }, # Hello { println(" ouch❗") } ) }

Slide 67

Slide 67 text

Result and Error

Slide 68

Slide 68 text

Result / Error module nomore_error import gololang.Errors function main = |args| { let divide = |a, b| { try { return Result(a/b) } catch(e) { return Error(e: message()) } } println( divide(84, 2) ) # Result.value[42] println( divide(84, 0) ) # Result.error[ java.lang.RuntimeException: / by zero] }

Slide 69

Slide 69 text

Result / Error / trying() / either() module nomore_error import gololang.Errors function main = |args| { let divide = |a, b| -> trying({ return a/b }) divide(84, 0): either( |result| -> println(": " + result), |error| -> println(": " + error: message()) # : / by zero ) }

Slide 70

Slide 70 text

Result / Error / trying() / either() module nomore_error import gololang.Errors function main = |args| { let divide = |a, b| -> trying({ return a/b }) divide(84, 0): either( |result| -> println(": " + result), |error| -> println(": " + error: message()) # : / by zero ) }

Slide 71

Slide 71 text

Golo ❤ Java

Slide 72

Slide 72 text

Golo ❤ Java package acme; import java.lang.String; import java.lang.System; public class Toon { private String name; public String getName() { return name; } public void setName(String value) { name = value; } public Toon(String name) { this.name = name; } public static Toon getInstance(String name) { return new Toon(name); } public void hug(Toon toon) { System.out.println("I ❤ " + toon.name); } } module toons import acme function main = |args| { let buster = Toon("Buster") # no new buster: name("Buster Bunny") println(buster: name()) let elmira = Toon.getInstance("elmira") elmira: hug(buster) }

Slide 73

Slide 73 text

Golo ❤ Java package acme; import java.lang.String; import java.lang.System; public class Toon { private String name; public String getName() { return name; } public void setName(String value) { name = value; } public Toon(String name) { this.name = name; } public static Toon getInstance(String name) { return new Toon(name); } public void hug(Toon toon) { System.out.println("I ❤ " + toon.name); } } module toons import acme function main = |args| { let buster = Toon("Buster") # no new buster: name("Buster Bunny") println(buster: name()) let elmira = Toon.getInstance("elmira") elmira: hug(buster) }

Slide 74

Slide 74 text

Golo ❤ Java package acme; import java.lang.String; import java.lang.System; public class Toon { private String name; public String getName() { return name; } public void setName(String value) { name = value; } public Toon(String name) { this.name = name; } public static Toon getInstance(String name) { return new Toon(name); } public void hug(Toon toon) { System.out.println("I ❤ " + toon.name); } } module toons import acme function main = |args| { let buster = Toon("Buster") # no new buster: name("Buster Bunny") println(buster: name()) let elmira = Toon.getInstance("elmira") elmira: hug(buster) }

Slide 75

Slide 75 text

and …

Slide 76

Slide 76 text

Golo ✨⚡ Java module toons import acme augment acme.Toon { function hey = |this, message| { println(this: name() + ": " + message) } } function main = |args| { let buster = Toon("Buster") buster: hey("I'm a ") }

Slide 77

Slide 77 text

Golo and the other frameworks

Slide 78

Slide 78 text

Simplification with Golo

Slide 79

Slide 79 text

Java web application with Vert-x int httpPort = 8080; Vertx vertx = Vertx.vertx(); HttpServer server = vertx.createHttpServer(); Router router = Router.router(vertx); router.route().handler(BodyHandler.create()); router.get("/about").handler(routingContext -> { routingContext.response() .putHeader("content-type", "application/json;charset=UTF-8") .end((new JsonObject().put("about", " hello from Golo")).toString()); }); router.post("/hello").handler(routingContext -> { String name = routingContext.getBodyAsJson().getString("name"); routingContext.response() .putHeader("content-type", "application/json;charset=UTF-8") .end((new JsonObject().put("message", " Hello " + name)).toString()); }); server.requestHandler(router::accept).listen(httpPort);

Slide 80

Slide 80 text

A few augmentations later…

Slide 81

Slide 81 text

Simplify Vert-x let httpPort = 8080 let server = createHttpServer() let router = getRouter() router: get("/about", |context| { context: response(): djson(): about(" hello from Golo"): send() }) router: post("/hello", |context| { let data = context: dbody() let name = data: name() context: response(): djson(): message(" Hello " + name): send() }) startHttpServer(server, router, httpPort, "/*")

Slide 82

Slide 82 text

some of the augmentations augment io.vertx.ext.web.Router { function get = |this, uri, handler| { return this: get(uri): handler(handler) } function post = |this, uri, handler| { return this: post(uri): handler(handler) } } augment io.vertx.core.http.HttpServerResponse { function djson = |this| { this: putHeader("content-type", "application/json;charset=UTF-8") let d = DynamicObject() d: define("send", |self| { this: end(JSON.stringify(self), "UTF-8") }) return d } } augment io.vertx.ext.web.RoutingContext { function dbody = |this| -> JSON.toDynamicObjectFromJSONString(this: getBodyAsString()) }

Slide 83

Slide 83 text

Simplify Vert-x - Functional way let httpPort = Integer.parseInt(System.getenv(): get("PORT") orIfNull "8080") let server = createHttpServer() let router = getRouter() router: get("/about", |context| { context: response(): djson(): about(" hello from Golo"): send() }) router: post("/hello", |context| { trying({ let data = context: dbody() return data: name() }) : either( |name| -> context: response(): djson(): message(" Hello " + name): send(), |error| -> context: response(): djson(): error(" " + error: message()): send() ) }) startHttpServer(server, router, httpPort, "/*")

Slide 84

Slide 84 text

the source code https://github.com/golo-x/golo-x-web

Slide 85

Slide 85 text

Augmenting Golo

Slide 86

Slide 86 text

I am a superhero I’m not a real Java Developer I ❤ JavaScript and Node But I made (accepted) PR on the Golo project

Slide 87

Slide 87 text

It’s very easy to add features to Golo 3 ways Java Golo Invokedynamic

Slide 88

Slide 88 text

It’s very easy to add features to Golo 3 ways Java Golo Invokedynamic

Slide 89

Slide 89 text

the Golo project https://github.com/eclipse/golo-lang

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Demo

Slide 93

Slide 93 text

I do fun “things” with Golo

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

IOT & MQTT experiments the benefit of the entire Java Ecosystem easy experiments https://github.com/golo-x/golo-x-mqtt

Slide 96

Slide 96 text

Jarvis-CI https://github.com/k33g/jarvis-ci SparkJava + eGit ⚠ V0

Slide 97

Slide 97 text

GolOctokit GitHub API https://github.com/k33g/goloctokit

Slide 98

Slide 98 text

IndyBot Slack RocketChat https://github.com/bots-squad/indybot

Slide 99

Slide 99 text

Microservices https://github.com/wey-yu/poks-server https://github.com/wey-yu/poks-service https://github.com/wey-yu/poks-consumer https://github.com/wey-yu/poks-core-libs

Slide 100

Slide 100 text

Thank you very much to you and devtalks

Slide 101

Slide 101 text

One more thing: https://tech.io

Slide 102

Slide 102 text

The end. Any Questions? (not too hard)