Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure...

106
Java, with a Clojure mindset @DanLebrero www.akvo.org

Transcript of Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure...

Page 1: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Java, with a Clojure mindset

@DanLebrerowww.akvo.org

Page 2: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 3: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 4: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 5: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 6: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 7: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 8: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 9: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

((((((

((((((()))))))

))))))

Page 10: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Clojure•Functional •Hosted •Dynamic •Strongly typed •Lisp

Page 11: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 12: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 13: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Give 10$ free cash if client makes 1000 bets

in less than 1 week

Page 14: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Functional (vs OO)

Page 15: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Pure Functions

Page 16: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 17: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

“Programmers are constantly in maintenance mode.” ― The Pragmatic Programmer

Page 18: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Side Effects

Page 19: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 20: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Side effects are the enemy

Page 21: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Side Effects

State IO

EffectsCo-effects

Page 22: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

State

Page 23: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

ClientBonus12

23

54

Client

Bonus

DepositList

Deposit

Deposit

BetList Bet

Bet

Page 24: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Atom =~

j.u.c.a.AtomicReference

Page 25: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Atom

Thread-1

Thread-2 fn2( ) =

fn1( ) =

====fn2( ) =

==?==? ==?

Page 26: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Atom

Thread-1

Thread-2 X

Page 27: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Atom

Thread-1

Thread-2

Thread-3

Page 28: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

12

23

54

ClientBonusClientBonus

BetList

Bet

12

23

54

Client

Bonus

DepositList

Deposit

Deposit

BetList Bet

Bet

Page 29: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

“Is this thread safe?” ― Every Java developer, every day.

Page 30: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

1. State is consistent2. Function must be pure3. Only safe within the pure function

Page 31: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class ClientBonus {

private final Client client; private final Bonus bonus; private final DepositList deposits;

Page 32: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

ClientBonus12

23

54

Client

Bonus

DepositList

Deposit

Deposit

BetList Bet

Bet

Page 33: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public interface ConcurrentMap<…> extends Map<…> {

V compute(K key, BiFunction<…> remappingFunction) V computeIfAbsent(K key, Function<…> mappingFunction) V computeIfPresent(K key, BiFunction<…> remappingFunction) …}

Page 34: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class TheStateHolder {

private final Map<Long, ClientBonus> state = new ConcurrentHashMap<>();

public ClientBonus nextState(Long client, Bet bet) { return state.computeIfPresent( client, (k, currentState) -> currentState.nextState(bet)); }

Page 35: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class ClientBonus {

private final Client client; private final Bonus bonus; private final DepositList deposits;

public ClientBonus nextState(Bet bet) { ... }

Page 36: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Effects

Page 37: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

xxxxService

INotificationSender

NotificationSenderImpl

TheStateHolder

Page 38: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class ClientBonus {

private final Client client; private final Bonus bonus; private final DepositList deposits;

public ClientBonus nextState(Bet bet) { ... }

Page 39: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class ClientBonus {

private final Client client; private final Bonus bonus; private final DepositList deposits;

public Pair<ClientBonus,Effects> next(Bet bet) { ... }

Page 40: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

NotifyClientEffect

Page 41: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

IgnoreError

NotifyClientEffect

Page 42: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

IgnoreError

NotifyClientEffect

StopTheJVM

NotifyClientEffect

Page 43: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Effect1 Effect2 Effect3SequentialEffects

Page 44: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Effect1

Effect2

Effect3

IndependentEffects

Page 45: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

StopTracking Pay NotifyClient

Page 46: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public interface Effect { void run(AllDependencies dependencies);}

Page 47: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Pair<ClientBonus, Effects> pair = theStateHolder.nextState(bet);pair.effects.run(dependencies);

Page 48: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Pair<ClientBonus, Effects> pair = theStateHolder.nextState(bet);pair.effects.run(dependencies);

“Is this thread safe?”

Page 49: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Thread-1

Thread-2

Pair<> pair = theStateHolder.nextState(bet);

Pair<> pair = theStateHolder.nextState(bet);

pair.effects.run(dependencies);

pair.effects.run(dependencies);

Page 50: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Pair<> pair = theStateHolder.nextState(bet);

Pair<> pair = theStateHolder.nextState(bet);

pair.effects.run(dependencies);

pair.effects.run(dependencies);

Thread-1

Thread-1

Thread-2

Thread-2

Page 51: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Agents (=~~~ Actors)

Page 52: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 53: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Can I pay?Can I pay?

ACID

Can I pay?

Page 54: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

NoYes No

Page 55: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

AtMostOnceStopTracking Pay NotifyClient

Page 56: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Co-Effects

Page 57: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 58: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Event Sourcing

Functional Programming

Page 59: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Event Sourcing

Functional Programming

Page 60: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class KafkaConsumer {

private AllDependencies allDependencies; private TheStateHolder theStateHolder; public void run() { while (!stop) { Bet bet = readNext(); Effects effects = theStateHolder.event(bet); effects.run(allDependencies); } }

…}

Page 61: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class KafkaConsumer {

private AllDependencies allDependencies; private TheStateHolder theStateHolder; public void run() { while (!stop) { Bet bet = readNext(); Effects effects = theStateHolder.event(bet); effects.run(allDependencies); } }

…}

Page 62: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class KafkaConsumer {

private AllDependencies allDependencies; private TheStateHolder theStateHolder; public void run() { while (!stop) { Bet bet = readNext(); Effects effects = theStateHolder.event(bet); effects.run(allDependencies); } }

…}

Page 63: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class KafkaConsumer {

private AllDependencies allDependencies; private TheStateHolder theStateHolder; public void run() { while (!stop) { Bet bet = readNext(); Effects effects = theStateHolder.event(bet); effects.run(allDependencies); } }

…}

Page 64: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class KafkaConsumer {

private AllDependencies allDependencies; private TheStateHolder theStateHolder; public void run() { while (!stop) { Bet bet = readNext(); Effects effects = theStateHolder.event(bet); effects.run(allDependencies); } }

…}

Page 65: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

•No getters•No locks or synch blocks•No try/catch•No logging•No mocks•No useless interfaces

Page 66: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Imperative Shell

https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell

Functional Core

Page 67: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

INotificationSender

NotificationSenderImpl

ClientBonus

Page 68: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Dynamic (vs Static) typing

Page 69: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

clientBonus = Map.of( "client", Map.of("id", "123233"), "deposits", List.of( Map.of("amount", 3, "type", "CASH"), Map.of("amount", 234, "type", "CARD")));

((List) clientBonus.get("deposits")) .stream() .collect( Collectors.summarizingInt( m -> (int) ((Map) m).get("amount")));

Page 70: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 71: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

public class Bet { private String id; private int amount; private long timestamp;}

Page 72: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

“It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.”

― Alan Perlis

Page 73: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:type :bet :id "client1" :amount 23 :timestamp 123312321323}

Page 74: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

{:type :bet :id "client1" :amount 23 :timestamp 123312321323}

Page 75: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:request-method :get :uri “/foobaz" :query-params {"somekey" “somevalue"} :headers {"accept-encoding" "gzip, deflate" "connection" "close"} :body nil :scheme :http :content-length 0 :server-port 8080 :server-name “localhost"}

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 76: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:status 200 :headers {"Content-Type" "text/html"} :body "Hello World"}}

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 77: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:select [:id :client :amount] :from [:transactions] :where [:= :client "a"]}

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 78: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

[{:id 1 :client 32 :amount 3} {:id 2 :client 87 :amount 7} {:id 3 :client 32 :amount 4} {:id 4 :client 40 :amount 6}]

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 79: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

[:html [:body [:p "Count: 4"] [:p "Total: 20"]]]

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 80: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:web-server {:listen 8080} :db-config {:host "xxxx" :user "xxxx" :password "xxxx"} :http-defaults {:connection-timeout 10000 :request-timeout 10000 :max-connections 2000} :user-service {:url “http://user-service” :connection-timeout 1000}}

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 81: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

{:id :string :name :string :deposits [{:id :string :amount :int :timestamp :long}]}

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 82: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

•Business logic•Infrastructure code•Configuration•Reflection/Metadata

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 83: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Dynamic (vs Static) development

Page 84: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

REPL

Page 85: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

https://danlebrero.com/repl

Page 86: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Lisp (vs Fortan)

Page 87: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

;X

Page 88: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

,X

Page 89: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 90: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 91: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

.filter(removeCsvHeaders(firstHeader))

.map(splitCsvString())

.map(convertCsvToMap(csvHeaders))

.map(convertToJson(eventCreator))

(filter not-header?)(map parse-csv-line)(map (partial zipmap headers))(map ->event)

12 3 4

5 6 78

9 1011

12

13 14 15 161 2

3 4

8765

9 10 -40%!!!

Page 92: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 93: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

List.of( new Symbol("defn"), new Symbol("plus-one"), List.of( new Symbol("a"), new Symbol("b")), Map.of( new Keyword("time"), List.of(new Symbol("System/currentTimeMillis")), new Keyword("result"), List.of( new Symbol("+"), new Symbol("a"), new Symbol("b"), new Long(1))));

Page 94: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

List.of( new Symbol("defn"), new Symbol("plus-one"), List.of( new Symbol("a"), new Symbol("b")), Map.of( new Keyword("time"), List.of(new Symbol("System/currentTimeMillis")), new Keyword("result"), List.of( new Symbol("+"), new Symbol("a"), new Symbol("b"), new Long(1))));

apply butlast concat cons count cycle diff distinct distinct? drop drop-last drop-while empty empty? every? ffirst filter first flatten fnext for frequencies group-by interleave interpose into into-array keep keep-indexed last lazy-cat map map-indexed mapcat next nfirst nnext not-any? not-empty not-every? nth nthnext partition partition-all partition-by pmap postwalk prewalk rand-nth reduce reductions remove replace rest reverse second seq? seque set shuffle some sort sort-by split-at split-with take take-nth take-while to-array-2d vec walk when-first assoc pop subvec replace conj rseq update-in update get get-in contains? find keys vals assoc assoc-in dissoc merge merge-with select-keys update-in update rename-keys map-invert reduce-kv dissoc-in disj join select project union difference intersection index

Page 95: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Summary

Page 96: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Imperative Shell

Functional Core

Page 97: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

DYNAMIC STATIC

TYPES

Page 98: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

DYNAMIC

STATIC

DEVELOPMENT

Page 99: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

((( )))

Page 100: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

“A language that doesn’t affect the way you think about programming, is not worth knowing.”

― Alan Perlis

Page 101: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 102: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 103: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 104: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

@DanLebrero [email protected] danlebrero.com

@Akvo https://akvo.org/

???

Page 105: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client
Page 106: Java, with a Clojure mindset › assets › uploads › sites › 33 › ... · Clojure •Functional •Hosted •Dynamic •Strongly typed •Lisp. Give 10$ free cash if client

Come to ClojureCooomeeeee…Clojure is your friendClojure is happinessClojure is beauty