SlideShare a Scribd company logo
1 of 35
Download to read offline
From OOP to FP
✅ The Validation case
Emmanuel Nhan - JUG Toulouse 11 juin 2019
😡 😡 😡 🤬
Let’s see the code…
💡There gotta be a
better way ! 🤔
Sticking in Javaland ☕
• Standard is JSR 303 : Bean Validation

• Let’s give it a try: Fortunately it is built-in in Spring !
In Action 🛠
JSR 303 results : the good
( Accumulates the errors for us in (found that in the docs): 

Set<ConstraintViolation<…!>>
( Automatic 400 Bad Requests (although this is mostly
done by Spring)

( Support for basic use cases
JSR 303 results : the bad
) No support for the parse error of HostAndPort

) Need custom handling for inter-dependent fields

) Annotations are not type-checked

) Weird mix of custom/covered cases

) Smelling reflection down there…
JSR 303 results : the weirdo
🤔 Automatic translation of error messages
Why is presentation even related to Validation ???

*
💡There gotta be a
better way ! 🤔
(Again !!!)
Do It Yourself 🛠
Let’s go back to the essence of our problem…
Validating one
• Result of a validation is either an error or a value

• Validation is just the function producing that result

• It looks like Optional structure but with an error type 

• We need a way to use the validated result
Validated ✅ (Kotlin style)
sealed class Validated<E, V> {
data class Valid<E,V>(val value: V) :
Validated<E,V>()
data class Invalid<E, V>(val error: E):
Validated<E, V>()
}
So far…
We can :

• create a Validated with success with Valid(a)

• create a Validated with error with Invalid(e)
• map over a Valid value
Representing errors
• Let’s use an ADT. In Kotlin :

sealed class AlertCreationError {
data class ThresholdATooLow(!/*!!...!*/):
AlertCreationError()
data class ThresholdCTooHigh(!/*!!...!*/):
AlertCreationError()
data class ThresholdBNotGreater(!/*…!*/):
AlertCreationError()
!// And so on
}
So far…
We can :

• create a Validated with success with Valid(a)
• create a Validated with error with Invalid(e)
• map over a Valid value

• fix the type of field validation to
Validated<AlertCreationError, T>
Validating many 🤔
• Finding a way to compose validation errors

• Finding a way to compose valid results
We need Composition !
Error Accumulation 📋
• JSR 303 used a Set

• When in error, we will at least have one element

• Let’s add some precision
NonEmpty*
• Two types exists

• NonEmptyList (NEL)

• NonEmptyChain (NEC)

• Structures which look like a linked list 

but which contains at least one element

• Ensuring this condition via smart constructor
Keeping Validated generic
• We need the E type in Validated<E, R> to be
combinable

• That is a function combine: (E, E) !-> E
• In the jargon, it is called Semigroup

• We need to go from

Validated<E, R> to Validated<Nel<E>, R>
So far…
We can :

• create a Validated with success with Valid(a)
• create a Validated with error with Invalid(e)
• map over a Valid value

• fix the type of field validation to
Validated<AlertCreationError, T>
• lift the error type from E to Nel<E> (leftMap)
Validating many
• Validating field A is independent of validating field B

• We need a function, with the shape :

(F<A>,F<B>,(A,B) !-> C) !-> F<C>
• Where F is a type like ValidatedNel with a fixed type:

(ValidatedNel<E,A>,ValidatedNel<E,B>,

(A,B) !-> C) !-> ValidatedNel<E, C>
• In the jargon, this is called Applicative
So far…
We can :

• create a Validated with success with Valid(a)
• create a Validated with error with Invalid(e)
• map over a Valid value

• fix the type of field validation to
Validated<AlertCreationError, T>
• lift the error type from E to Nel<E> & accumulate errors

• Combine all values when all fields are in Valid state
• Keep invalid when at least one field is in Invalid state
Not really DIY
In Action 🛠 (Kotlin)
Take away 🥡🍜
Validation is a (simple) problem which can be solved with
just 2 things :

✓Appropriate data structures (some may say ADT) 

to model errors & validation results

✓An abstraction to express independent computations
(Applicative)
💡There gotta be a
better way ! 🤔
(Again & again !!!)
This is nice and all but…
Can we go further ? 🚀
💡We could try to make impossible states unrepresentable
possible
values for A
acceptable
values for A
acceptable
values for A
Validation :
Uses a predicate to determine
the acceptable subset of A
Is it possible to encode this in code ?
Type system ✨
• Predicate encoding in the type system

• Compiler automatic derivation

• There is this thing called Refined

• Let’s see some (hardcore) Scala… 

(sorry I’m not fluent in Haskell 🙊)
Refined in action
Not there yet ! 💫
• Clearly I’m not an expert (but if you are, let’s talk 🤩)

• Even for a simple case like Validation research is
important

• Mathematics are the (best) base tool at hand
Q&A time
Thanks for listening

@enhan on Github &
@nhanmanu on Twitter
References & credits
• Code: 

• https://github.com/enhan/oop-to-fp-validation

• https://github.com/enhan/refined-validation

• Articles: 

• https://typelevel.org/cats/datatypes/validated.html

• https://arrow-kt.io/docs/arrow/data/validated/

• https://www.enhan.eu/how-to-in-fp/

• http://lara.epfl.ch/~kuncak/papers/
SchmidKuncak16CheckingPredicate.pdf
References & credits
• Books:

• Functional Programming in Scala (Paul Chiusano and
Runar Bjarnason)

• Category Theory for Programmers (Bartosz Milewski)
References & credits
• Libs:

• Spring: https://spring.io/

• Arrow: https://arrow-kt.io/

• Cats: https://typelevel.org/cats/

• Refined: https://github.com/fthomas/refined
References & credits
• Pictures (all from unsplash, in order) :

• SpaceX

• Anthony Cantin

• Mirko Blicke

• Simon Caspersen

• Alexey Sukhariev

• Designecologist

More Related Content

What's hot

Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developertroubledkumi
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
How do i - create a native interface
How do i -  create a native interfaceHow do i -  create a native interface
How do i - create a native interfaceShai Almog
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to FunctionsCharles Russell
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking FrameworksDror Helper
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noiseNeeraj Bhusare
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1Yi-Huan Chan
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy CodeAndrea Polci
 
Typed Drupal - A great combination of Drupal 8 and PHP7
Typed Drupal - A great combination of Drupal 8 and PHP7Typed Drupal - A great combination of Drupal 8 and PHP7
Typed Drupal - A great combination of Drupal 8 and PHP7Aditya Ghan
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Andrzej Jóźwiak
 
Testdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMockTestdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMockschlebu
 
Functional programming with Xtend
Functional programming with XtendFunctional programming with Xtend
Functional programming with XtendSven Efftinge
 

What's hot (20)

Writing tests
Writing testsWriting tests
Writing tests
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
 
Introduction to kotlin for Java Developer
Introduction to kotlin for Java DeveloperIntroduction to kotlin for Java Developer
Introduction to kotlin for Java Developer
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
How do i - create a native interface
How do i -  create a native interfaceHow do i -  create a native interface
How do i - create a native interface
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
 
Battle of The Mocking Frameworks
Battle of The Mocking FrameworksBattle of The Mocking Frameworks
Battle of The Mocking Frameworks
 
Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
 
EasyMock for Java
EasyMock for JavaEasyMock for Java
EasyMock for Java
 
Working With Legacy Code
Working With Legacy CodeWorking With Legacy Code
Working With Legacy Code
 
Rspec
RspecRspec
Rspec
 
Typed Drupal - A great combination of Drupal 8 and PHP7
Typed Drupal - A great combination of Drupal 8 and PHP7Typed Drupal - A great combination of Drupal 8 and PHP7
Typed Drupal - A great combination of Drupal 8 and PHP7
 
Java script basic
Java script basicJava script basic
Java script basic
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
Do I need tests when I have the compiler - Andrzej Jóźwiak - TomTom Dev Day 2020
 
Testdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMockTestdriven Development using JUnit and EasyMock
Testdriven Development using JUnit and EasyMock
 
Working Effectively with Legacy Code
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Code
 
Functional programming with Xtend
Functional programming with XtendFunctional programming with Xtend
Functional programming with Xtend
 

Similar to OOP to FP Validation

Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
DefensiveProgramming (1).pptx
DefensiveProgramming (1).pptxDefensiveProgramming (1).pptx
DefensiveProgramming (1).pptxazida3
 
Agile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practiceAgile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practicedenis Udod
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...FalafelSoftware
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)Steve Upton
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Damien Seguy
 
Unit testing
Unit testingUnit testing
Unit testingPiXeL16
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
Exception handling in Ruby
Exception handling in RubyException handling in Ruby
Exception handling in RubyAmit Patel
 
Test automation expert days
Test automation   expert daysTest automation   expert days
Test automation expert daysOren Rubin
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWHolger Grosse-Plankermann
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy CodeEyal Golan
 
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle GamesWe Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle GamesUnity Technologies
 

Similar to OOP to FP Validation (20)

Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
CPP10 - Debugging
CPP10 - DebuggingCPP10 - Debugging
CPP10 - Debugging
 
DefensiveProgramming (1).pptx
DefensiveProgramming (1).pptxDefensiveProgramming (1).pptx
DefensiveProgramming (1).pptx
 
Getting Started With Testing
Getting Started With TestingGetting Started With Testing
Getting Started With Testing
 
Agile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practiceAgile latvia evening_unit_testing_in_practice
Agile latvia evening_unit_testing_in_practice
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
DSR Testing (Part 1)
DSR Testing (Part 1)DSR Testing (Part 1)
DSR Testing (Part 1)
 
Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
 
Unit testing
Unit testingUnit testing
Unit testing
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE2600 Thailand #50 From 0day to CVE
2600 Thailand #50 From 0day to CVE
 
Exception handling in Ruby
Exception handling in RubyException handling in Ruby
Exception handling in Ruby
 
Test automation expert days
Test automation   expert daysTest automation   expert days
Test automation expert days
 
Angular Unit Test
Angular Unit TestAngular Unit Test
Angular Unit Test
 
Jest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRWJest: Frontend Testing richtig gemacht @WebworkerNRW
Jest: Frontend Testing richtig gemacht @WebworkerNRW
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
 
Writing clean code
Writing clean codeWriting clean code
Writing clean code
 
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle GamesWe Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
We Love Performance! How Tic Toc Games Uses ECS in Mobile Puzzle Games
 

Recently uploaded

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 

Recently uploaded (20)

Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 

OOP to FP Validation

  • 1. From OOP to FP ✅ The Validation case Emmanuel Nhan - JUG Toulouse 11 juin 2019
  • 2. 😡 😡 😡 🤬 Let’s see the code…
  • 3. 💡There gotta be a better way ! 🤔
  • 4. Sticking in Javaland ☕ • Standard is JSR 303 : Bean Validation • Let’s give it a try: Fortunately it is built-in in Spring !
  • 6. JSR 303 results : the good ( Accumulates the errors for us in (found that in the docs): Set<ConstraintViolation<…!>> ( Automatic 400 Bad Requests (although this is mostly done by Spring) ( Support for basic use cases
  • 7. JSR 303 results : the bad ) No support for the parse error of HostAndPort ) Need custom handling for inter-dependent fields ) Annotations are not type-checked ) Weird mix of custom/covered cases ) Smelling reflection down there…
  • 8. JSR 303 results : the weirdo 🤔 Automatic translation of error messages Why is presentation even related to Validation ??? *
  • 9. 💡There gotta be a better way ! 🤔 (Again !!!)
  • 10. Do It Yourself 🛠 Let’s go back to the essence of our problem…
  • 11. Validating one • Result of a validation is either an error or a value • Validation is just the function producing that result • It looks like Optional structure but with an error type • We need a way to use the validated result
  • 12. Validated ✅ (Kotlin style) sealed class Validated<E, V> { data class Valid<E,V>(val value: V) : Validated<E,V>() data class Invalid<E, V>(val error: E): Validated<E, V>() }
  • 13. So far… We can : • create a Validated with success with Valid(a) • create a Validated with error with Invalid(e) • map over a Valid value
  • 14. Representing errors • Let’s use an ADT. In Kotlin : sealed class AlertCreationError { data class ThresholdATooLow(!/*!!...!*/): AlertCreationError() data class ThresholdCTooHigh(!/*!!...!*/): AlertCreationError() data class ThresholdBNotGreater(!/*…!*/): AlertCreationError() !// And so on }
  • 15. So far… We can : • create a Validated with success with Valid(a) • create a Validated with error with Invalid(e) • map over a Valid value • fix the type of field validation to Validated<AlertCreationError, T>
  • 16. Validating many 🤔 • Finding a way to compose validation errors • Finding a way to compose valid results We need Composition !
  • 17. Error Accumulation 📋 • JSR 303 used a Set • When in error, we will at least have one element • Let’s add some precision
  • 18. NonEmpty* • Two types exists • NonEmptyList (NEL) • NonEmptyChain (NEC) • Structures which look like a linked list 
 but which contains at least one element • Ensuring this condition via smart constructor
  • 19. Keeping Validated generic • We need the E type in Validated<E, R> to be combinable • That is a function combine: (E, E) !-> E • In the jargon, it is called Semigroup • We need to go from
 Validated<E, R> to Validated<Nel<E>, R>
  • 20. So far… We can : • create a Validated with success with Valid(a) • create a Validated with error with Invalid(e) • map over a Valid value • fix the type of field validation to Validated<AlertCreationError, T> • lift the error type from E to Nel<E> (leftMap)
  • 21. Validating many • Validating field A is independent of validating field B • We need a function, with the shape : (F<A>,F<B>,(A,B) !-> C) !-> F<C> • Where F is a type like ValidatedNel with a fixed type:
 (ValidatedNel<E,A>,ValidatedNel<E,B>,
 (A,B) !-> C) !-> ValidatedNel<E, C> • In the jargon, this is called Applicative
  • 22. So far… We can : • create a Validated with success with Valid(a) • create a Validated with error with Invalid(e) • map over a Valid value • fix the type of field validation to Validated<AlertCreationError, T> • lift the error type from E to Nel<E> & accumulate errors • Combine all values when all fields are in Valid state • Keep invalid when at least one field is in Invalid state
  • 24. In Action 🛠 (Kotlin)
  • 25. Take away 🥡🍜 Validation is a (simple) problem which can be solved with just 2 things : ✓Appropriate data structures (some may say ADT) 
 to model errors & validation results ✓An abstraction to express independent computations (Applicative)
  • 26. 💡There gotta be a better way ! 🤔 (Again & again !!!) This is nice and all but…
  • 27. Can we go further ? 🚀 💡We could try to make impossible states unrepresentable possible values for A acceptable values for A acceptable values for A Validation : Uses a predicate to determine the acceptable subset of A Is it possible to encode this in code ?
  • 28. Type system ✨ • Predicate encoding in the type system • Compiler automatic derivation • There is this thing called Refined • Let’s see some (hardcore) Scala… 
 (sorry I’m not fluent in Haskell 🙊)
  • 30. Not there yet ! 💫 • Clearly I’m not an expert (but if you are, let’s talk 🤩) • Even for a simple case like Validation research is important • Mathematics are the (best) base tool at hand
  • 31. Q&A time Thanks for listening @enhan on Github & @nhanmanu on Twitter
  • 32. References & credits • Code: • https://github.com/enhan/oop-to-fp-validation • https://github.com/enhan/refined-validation • Articles: • https://typelevel.org/cats/datatypes/validated.html • https://arrow-kt.io/docs/arrow/data/validated/ • https://www.enhan.eu/how-to-in-fp/ • http://lara.epfl.ch/~kuncak/papers/ SchmidKuncak16CheckingPredicate.pdf
  • 33. References & credits • Books: • Functional Programming in Scala (Paul Chiusano and Runar Bjarnason) • Category Theory for Programmers (Bartosz Milewski)
  • 34. References & credits • Libs: • Spring: https://spring.io/ • Arrow: https://arrow-kt.io/ • Cats: https://typelevel.org/cats/ • Refined: https://github.com/fthomas/refined
  • 35. References & credits • Pictures (all from unsplash, in order) : • SpaceX • Anthony Cantin • Mirko Blicke • Simon Caspersen • Alexey Sukhariev • Designecologist