functional programming in scala @ing bank

47
Functional Programming Alexandru Nedelcu & Mihai Simu In SCALA @ING Bank November 2021

Upload: others

Post on 12-May-2022

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functional Programming In SCALA @ING Bank

Functional Programming

Alexandru Nedelcu & Mihai Simu

In SCALA @ING Bank

November 2021

Page 2: Functional Programming In SCALA @ING Bank

Vesper @ ING

1.Romania’s payment engineWith ambitions for more

2.In productionAnd heavily refactored 😛

3.We have superpowers😎

2

Page 3: Functional Programming In SCALA @ING Bank

Distributed / remote team

Vesper @ ING

Page 4: Functional Programming In SCALA @ING Bank

Distributed / remote teamScala / JVM

Vesper @ ING

Page 5: Functional Programming In SCALA @ING Bank

Distributed / remote teamScala / JVMFunctional programming

Vesper @ ING

Page 6: Functional Programming In SCALA @ING Bank

Distributed / remote teamScala / JVMFunctional programmingAkka

StreamsClusterEvent Sourcing

Vesper @ ING

Page 7: Functional Programming In SCALA @ING Bank

Distributed / remote teamScala / JVMFunctional programmingAkka

StreamsClusterEvent Sourcing

Reusable componentsResiliency, consistency, horizontal scalability

Vesper @ ING

Page 8: Functional Programming In SCALA @ING Bank

Functional

Programming (FP)

Page 9: Functional Programming In SCALA @ING Bank

Functional Programming

f: X → Y, ∀ x1, x2 ∈ Xf(x1) ≠ f(x2) => x1 ≠ x2

Page 10: Functional Programming In SCALA @ING Bank

Functional Programming

f: X → Y, ∀ x1, x2 ∈ Xf(x1) ≠ f(x2) => x1 ≠ x2

Page 11: Functional Programming In SCALA @ING Bank

Functional Programming

• Programming with math functions• Aka “pure functions”, or functions without side-effects

• Programming with values • Aka immutable data-structures

Page 12: Functional Programming In SCALA @ING Bank

Referential Transparency

An expression is called referentially transparent if it can be replaced with its corresponding value (and vice-versa) without changing the program's behavior.

Page 13: Functional Programming In SCALA @ING Bank

Referential Transparency

Page 14: Functional Programming In SCALA @ING Bank

Referential Transparency

Page 15: Functional Programming In SCALA @ING Bank

Referential Transparency

Page 16: Functional Programming In SCALA @ING Bank

Functional Programming :: Example

Page 17: Functional Programming In SCALA @ING Bank

1

2

3

Page 18: Functional Programming In SCALA @ING Bank

Scala• Static type system (really static J)• Culture oriented towards FP• Optimal language for FP• Expression based• “Union types”• “Higher-kinded types”• “Type-classes”• Typelevel ecosystem

Page 19: Functional Programming In SCALA @ING Bank

• Static type system (really static J)• Culture oriented towards FP• Optimal language for FP• Expression based• “Union types”• “Higher-kinded types”• “Type-classes”• Typelevel ecosystem

Scala

Page 20: Functional Programming In SCALA @ING Bank

Functional Programming + Scala

• Reduced defects rate [1] [2]

1. A Large Scale Study of Programming Languages and Code Quality in Github2. To Type or Not to Type: Quantifying Detectable Bugs in JavaScript

Page 21: Functional Programming In SCALA @ING Bank

• Reduced defects rate• Easier maintenance (tests, refactoring)

Functional Programming + Scala

Page 22: Functional Programming In SCALA @ING Bank

• Reduced defects rate• Easier maintenance (tests, refactoring)• Local reasoning• Mathematical rigor

Functional Programming + Scala

Page 23: Functional Programming In SCALA @ING Bank

• Reduced defects rate• Easier maintenance (tests, refactoring)• Local reasoning• Mathematical rigor• We’re hiring great people• We’re still learning• It’s fun!

Functional Programming + Scala

Page 24: Functional Programming In SCALA @ING Bank
Page 25: Functional Programming In SCALA @ING Bank

Payment-processing

Destination Queue- Transaction is

considered finished

Perform Processing- Debit and credit

accounts

Input Queue- Contains transactions- Must delete from

queue when finished

Page 26: Functional Programming In SCALA @ING Bank

Payment-processing - example• Transactions are received as String• Processor must

• parse• check fraud-detection• processes only during working hours• delete from queue when finished• be processed at-most-once

• Errors can occur and should be handled

Page 27: Functional Programming In SCALA @ING Bank

Payment-processing - example• Transactions are received as String• Processor must

• parse• check fraud-detection• processes only during working hours• delete from queue when finished• be processed at-most-once

• Errors can occur and should be handled

Page 28: Functional Programming In SCALA @ING Bank

Payment-processing code (1)

Page 29: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Each step is a Stream component

val source: SourceShape[TString] = ???val parse: FlowShape[TString, TParsed] = ???val fraudDetection: FanIn1FanOut2Shape[TParsed, TVerified, TFraudInfo] = ???

Page 30: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Each step is a Stream component

• Components are black-boxes with ports

val source: SourceShape[TString] = ???val parse: FlowShape[TString, TParsed] = ???val fraudDetection: FanIn1FanOut2Shape[TParsed, TVerified, TFraudInfo] = ???

Page 31: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Each step is a Stream component

• Components are black-boxes with ports

• Compose Components with GraphDSL• Types of ports must match

val source: SourceShape[TString] = ???val parse: FlowShape[TString, TParsed] = ???val fraudDetection: FanIn1FanOut2Shape[TParsed, TVerified, TFraudInfo] = ???

source ~> parse ~> fraudDetection ~> …

Page 32: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Each step is a Stream component

• Components are black-boxes with ports

• Compose Components with GraphDSL• Types of ports must match

• Messages passed asynchronously between components

val source: SourceShape[TString] = ???val parse: FlowShape[TString, TParsed] = ???val fraudDetection: FanIn1FanOut2Shape[TParsed, TVerified, TFraudInfo] = ???

source ~> parse ~> fraudDetection ~> …

Page 33: Functional Programming In SCALA @ING Bank

Payment-processing code (2)

Page 34: Functional Programming In SCALA @ING Bank

Payment-processing code (2)M1 <~ delayedStoreOut

workHrs.o2 ~> del1 ~> delayedStoreInsource ~> parse ~> M1 ~> fraudD; fraudD.o1 ~> workHrs; workHrs.o1 ~> process ~> MEnd ~> del2 ~> sink

fraudD.o2 ~> reject ~> MEnd

Page 35: Functional Programming In SCALA @ING Bank

Payment-processing error-handling

• Assume each step can fail• Differentiate

• Retryable errors (eg: external service unavailable)• NonRetryable errors (eg: parsing exception)

M1 <~ delayedStoreOutworkHrs.o2 ~> del1 ~> delayedStoreIn

source ~> parse ~> M1 ~> fraudD; fraudD.o1 ~> workHrs; workHrs.o1 ~> process ~> MEnd ~> del2 ~> sinkfraudD.o2 ~> reject ~> MEnd

Page 36: Functional Programming In SCALA @ING Bank

Payment-processing error-handling

• Assume each step can fail• Differentiate

• Retryable errors (eg: external service unavailable)• NonRetryable errors (eg: parsing exception)

M1 <~ delayedStoreOutworkHrs.o2 ~> del1 ~> delayedStoreIn

source ~> parse ~> M1 ~> fraudD; fraudD.o1 ~> workHrs; workHrs.o1 ~> process ~> MEnd ~> del2 ~> sinkfraudD.o2 ~> reject ~> MEnd

Page 37: Functional Programming In SCALA @ING Bank

Payment-processing code (3)M1 <~ delayedStoreOut

workHrs.o2 ~> del1 ~> delayedStoreInsource ~> parse; parse.o1 ~> M1 ~> fraudD; fraudD.o1 ~> workHrs; workHrs.o1 ~> process; process.o1 ~> MEnd ~> del2 ~> sink

fraudD.o2 ~> reject; reject.o1 ~> MEndparse.oErr ~> MErr

fraudD.oErr ~> MErrreject.oErr ~> MErr

process.oErr ~> MErrdelayedStoreErr ~> Merr

// Error FlowMErr ~> logNotifyErrors ~> splitErr

splitErr.o1 ~> storeErr ~> MEndsplitErr.o2 ~> errorSink

Page 38: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Main unit of abstraction = Flow component

• Contain pure FP code for business logic and expose ports• Frequent tension: what to model with components vs FP code

Page 39: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Main unit of abstraction = Flow component

• Contain pure FP code for business logic and expose ports• Frequent tension: what to model with components vs FP code

• Components can be arbitrarily complex, eg: • Call HTTP Service/ database query• Maintain FSM of transaction-state

Page 40: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Main unit of abstraction = Flow component

• Contain pure FP code for business logic and expose ports• Frequent tension: what to model with components vs FP code

• Components can be arbitrarily complex, eg: • Call HTTP Service/ database query• Maintain FSM of transaction-state

• We allow transactions to be replayed if RetryableErrors appear• eg: service for verifying Fraud is temporarily unavailable• => components must be idempotent

Page 41: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Main unit of abstraction = Flow component

• Contain pure FP code for business logic and expose ports• Frequent tension: what to model with components vs FP code

• Components can be arbitrarily complex, eg: • Call HTTP Service/ database query• Maintain FSM of transaction-state

• We allow transactions to be replayed if RetryableErrors appear• eg: service for verifying Fraud is temporarily unavailable• => components must be idempotent

• Built-in backpressure

Page 42: Functional Programming In SCALA @ING Bank

Payment-processing with Akka-Streams• Main unit of abstraction = Flow component

• Contain pure FP code for business logic and expose ports• Frequent tension: what to model with components vs FP code

• Components can be arbitrarily complex, eg: • Call HTTP Service/ database query• Maintain FSM of transaction-state

• We allow transactions to be replayed if RetryableErrors appear• eg: service for verifying Fraud is temporarily unavailable• => components must be idempotent

• Built-in backpressure

• Scale horizontally with akka-cluster + sharding

Page 43: Functional Programming In SCALA @ING Bank

Scala and FP – closing remarks

Example: Generate the Fibonacci numbers

Page 44: Functional Programming In SCALA @ING Bank

Scala and FP – closing remarksExample: Generate the Fibonacci numbers

Page 45: Functional Programming In SCALA @ING Bank

Scala and FP – closing remarks

Example: Generate the Fibonacci numbers

Page 46: Functional Programming In SCALA @ING Bank

Scala and FP - choosing an ecosystem

• What you can build with it• Library/Platform Support• How it fits in problem-domain• Corectness guarantees• Fun

Page 47: Functional Programming In SCALA @ING Bank

Questions?

Alexandru [email protected]

Mihai [email protected]

We’re hiring 😉

Nu uitați că avem și un concurs pregătit pentru antreprenorii presenți la eveniment. Am pregătit 10 pachete ING FIX pentru 12 luni consecutive, pentru 10 antreprenori. Toți participanții eligibili la concurs vor primi gratuit, automat, un pachet ING FIX, pentru o perioadă de până la două luni, pentru a testa serviciul ING.

Dacă vreți să vă înscrieți, mergeți la standul ING unde, la descrierea companiei veți găsi un tab cu numeleConcurs, unde este formularul de înscriere.