Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Programming with pure functions - Dutch version

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Programming with pure functions - Dutch version

Avatar for Janne Siera

Janne Siera

February 10, 2020

More Decks by Janne Siera

Other Decks in Programming

Transcript

  1. Hoofdstuk 1: Excel Analytics Hoofdstuk 2: 'Procedures' en 'Maps' Hoofdstuk

    3: Werken met 'Maps' Hoofdstuk 4: ANDs en ORs Hoofdstuk 5: Higher-order functions Hoofdstuk 6: Van 'whiteboards' naar 'pipelines'
  2. ONZE OPDRACHT ONZE OPDRACHT Ontwikkel een tool om excel data

    te analyseren en 'business analytics' te produceren voor een international.
  3. DE DATA DE DATA // Mock const readExcel = ():

    Array<Invoice> => { return [ { InvoiceId: "101", CountryCode: "BE" }, { InvoiceId: "102", CountryCode: "BR" }, { InvoiceId: "103", CountryCode: "BR" } ] }
  4. DE DATA DE DATA // So we get them like

    this const invoices: Array<Invoice> = readExcel(); // Mock const readExcel = (): Array<Invoice> => { return [ { InvoiceId: "101", CountryCode: "BE" }, { InvoiceId: "102", CountryCode: "BR" }, { InvoiceId: "103", CountryCode: "BR" } ] }
  5. HOEVEEL BELGISCHE FACTUREN? HOEVEEL BELGISCHE FACTUREN? const getBelgianInvoices = (invoices:

    Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BE') { invoices.splice(i,1); } } return invoices; }
  6. HOEVEEL BELGISCHE FACTUREN? HOEVEEL BELGISCHE FACTUREN? const getNumberOfBelgianInvoices = ():

    number => { const invoices = readExcel(); const belgianInvoices = getBelgianInvoices(invoices); return belgianInvoices.length; } const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BE') { invoices.splice(i,1); } } return invoices; }
  7. HOEVEEL BELGISCHE FACTUREN? HOEVEEL BELGISCHE FACTUREN? getNumberOfBelgianInvoices(); // 1 const

    getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BE') { invoices.splice(i,1); } } return invoices; } const getNumberOfBelgianInvoices = (): number => { const invoices = readExcel(); const belgianInvoices = getBelgianInvoices(invoices); return belgianInvoices.length; }
  8. HOEVEEL BRAZILIAANSE FACTUREN? HOEVEEL BRAZILIAANSE FACTUREN? getBrazilianInvoices 'BR' getNumberOfBrazilianInvoices const

    = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== ) { invoices.splice(i,1); } } return invoices; } const = (): number => { const invoices = readExcel(); const brazilianInvoices = getBrazilianInvoices(invoices); return brazilianInvoices.length; }
  9. HOEVEEL BRAZILIAANSE FACTUREN? HOEVEEL BRAZILIAANSE FACTUREN? getNumberOfBrazilianInvoices(); // 2 const

    getBrazilianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BR') { invoices.splice(i,1); } } return invoices; } const getNumberOfBrazilianInvoices = (): number => { const invoices = readExcel(); const brazilianInvoices = getBrazilianInvoices(invoices); return brazilianInvoices.length; }
  10. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? const calculateFavoritePaymentMethod = (): PaymentMethod =>

    { const numberOfBelgianInvoices = getNumberOfBelgianInvoices(); const numberOfBrazilianInvoices = getNumberOfBrazilianInvoices(); if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" } enum PaymentMethod { Bancontact, Visa, Mastercard }
  11. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? Verwachte output: 1 enum PaymentMethod {

    Bancontact, /* 0 */ Visa, /* 1 */ Mastercard /* 2 */ } const readExcel = () => { return [ { InvoiceId: "101", CountryCode: "BE" }, // Bancontact 0 { InvoiceId: "102", CountryCode: "BR" }, // Visa 1 { InvoiceId: "103", CountryCode: "BR" } // Visa 1 ] }
  12. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? calculateFavoritePaymentMethod(); // 1 const calculateFavoritePaymentMethod =

    (): PaymentMethod => { const numberOfBelgianInvoices = getNumberOfBelgianInvoices(); const numberOfBrazilianInvoices = getNumberOfBrazilianInvoices(); if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" }
  13. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? getNumberOfBelgianInvoices() getNumberOfBrazilianInvoices() const calculateFavoritePaymentMethod = ():

    PaymentMethod => { const numberOfBelgianInvoices = ; const numberOfBrazilianInvoices = ; if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" } calculateFavoritePaymentMethod(); // 1
  14. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? const invoices = readExcel(); getBelgianInvoices(invoices).length getBrazilianInvoices(invoices).length

    const calculateFavoritePaymentMethod = (): PaymentMethod => { const numberOfBelgianInvoices = ; const numberOfBrazilianInvoices = ; if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" }
  15. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? calculateFavoritePaymentMethod(); // 0 const calculateFavoritePaymentMethod =

    (): PaymentMethod => { const invoices = readExcel(); const numberOfBelgianInvoices = getBelgianInvoices(invoices).length; const numberOfBrazilianInvoices = getBrazilianInvoices(invoices).length; if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" }
  16. FAVORIETE BETAALMETHODE? FAVORIETE BETAALMETHODE? invoices invoices invoices const calculateFavoritePaymentMethod =

    (): PaymentMethod => { const = readExcel(); const numberOfBelgianInvoices = getBelgianInvoices( ).length; const numberOfBrazilianInvoices = getBrazilianInvoices( ).length; if(numberOfBelgianInvoices > numberOfBrazilianInvoices) return PaymentMethod.Bancontact; else if (numberOfBrazilianInvoices > numberOfBelgianInvoices) return PaymentMethod.Visa else throw "Can't decide" } calculateFavoritePaymentMethod(); // 0
  17. EXCEL UPDATEN EXCEL UPDATEN LastUpdated: Date type Invoice = {

    InvoiceId: string; CountryCode: string; }
  18. EXCEL UPDATEN EXCEL UPDATEN LastUpdated: new Date() LastUpdated: new Date()

    LastUpdated: new Date() type Invoice = { InvoiceId: string; CountryCode: string; LastUpdated: Date } const readExcel = (): Array<Invoice> => { return [ { InvoiceId: "101", CountryCode: "BE", }, { InvoiceId: "102", CountryCode: "BR", }, { InvoiceId: "103", CountryCode: "BR", } ] }
  19. EXCEL UPDATEN EXCEL UPDATEN const createExcel = (fileName: string, invoices:

    Array<Invoice>): void => { console.log(invoices); } type Invoice = { InvoiceId: string; CountryCode: string; LastUpdated: Date } const readExcel = (): Array<Invoice> => { return [ { InvoiceId: "101", CountryCode: "BE", LastUpdated: new Date() }, { InvoiceId: "102", CountryCode: "BR", LastUpdated: new Date() }, { InvoiceId: "103", CountryCode: "BR", LastUpdated: new Date() } ] }
  20. EXCEL UPDATEN EXCEL UPDATEN const updateInvoice = (invoice: Invoice): void

    => { invoice.LastUpdated = new Date(); // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === new Date().getTime()); createExcel("Update Report", updatedInvoices); }
  21. EXCEL UPDATEN EXCEL UPDATEN Output: Alle 3 facturen worden naar

    de console geschreven const updateInvoice = (invoice: Invoice): void => { invoice.LastUpdated = new Date(); // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === new Date().getTime()); createExcel("Update Report", updatedInvoices); }
  22. EXCEL UPDATEN EXCEL UPDATEN await sleep(1000); const updateInvoice = (invoice:

    Invoice): void => { invoice.LastUpdated = new Date(); // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === new Date().getTime()); createExcel("Update Report", updatedInvoices); }
  23. EXCEL UPDATEN EXCEL UPDATEN Output: Een lege array wordt naar

    de console geschreven const updateInvoice = (invoice: Invoice): void => { invoice.LastUpdated = new Date(); // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } await sleep(1000); const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === new Date().getTime()); createExcel("Update Report", updatedInvoices); }
  24. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Throw Exceptions

    Mutate references Create references Iterate lists
  25. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Return values

    Mutate references Create references Iterate lists Throw Exceptions
  26. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Een 'HTTP

    request' maken Mutate references Create references Iterate lists Throw Exceptions Return values
  27. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Een bestand

    uitlezen Mutate references Create references Iterate lists Throw Exceptions Return values Een 'HTTP request' maken
  28. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Naar een

    bestand schrijven Mutate references Create references Iterate lists Throw Exceptions Return values Een 'HTTP request' maken Een bestand uitlezen
  29. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Met een

    database praten Mutate references Create references Iterate lists Throw Exceptions Return values Een 'HTTP request' maken Een bestand uitlezen Naar een bestand schrijven
  30. WAT EEN 'PROCEDURE' KAN WAT EEN 'PROCEDURE' KAN Alles! Mutate

    references Create references Iterate lists Throw Exceptions Return values Een 'HTTP request' maken Een bestand uitlezen Naar een bestand schrijven Met een database praten
  31. // Reads from a file const getNumberOfBelgianInvoices = (): number

    => { ... } // Mutates a reference const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { ... }
  32. // Reads from a file // Throws exception const calculateFavoritePaymentMethod

    = (): PaymentMethod => { ... } // Mutates a reference const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { ... } // Reads from a file const getNumberOfBelgianInvoices = (): number => { ... }
  33. Een 'map' associeert de elementen van een input set (verzameling)

    met de elementen van een output set (verzameling). Regel #1: Slechts 1 pijl per input element Regel #2: Alle input elementen moeten een pijl hebben
  34. SET ≈ TYPE SET ≈ TYPE bool is { True,

    False } number is { ..., -2, -1, 0, 1, 2, ... }
  35. SET ≈ TYPE SET ≈ TYPE bool is { True,

    False } number is { ..., -2, -1, 0, 1, 2, ... } string is { "", "a", "aa", "aaa", ..., "hello", ..., "dsfewsdf", ..., "hello world!", ... }
  36. SET ≈ TYPE SET ≈ TYPE bool is { True,

    False } number is { ..., -2, -1, 0, 1, 2, ... } string is { "", "a", "aa", "aaa", ..., "hello", ..., "dsfewsdf", ..., "hello world!", ... } Countries is { Belgium, Brazil }
  37. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD enum PaymentMethod { Bancontact,

    Visa, Mastercard } // Output set enum Countries { Belgium, Brazil } // Input set
  38. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD const FavoritePaymentMethod = (country:

    Countries): PaymentMethod => { ... } enum Countries { Belgium, Brazil } // Input set enum PaymentMethod { Bancontact, Visa, Mastercard } // Output set
  39. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD switch(country) { case Countries.Belgium:

    return PaymentMethod.Bancontact; } enum Countries { Belgium, Brazil } // Input set enum PaymentMethod { Bancontact, Visa, Mastercard } // Output set const FavoritePaymentMethod = (country: Countries): PaymentMethod => { }
  40. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD TypeScript error: Function lacks

    ending return statement and return type does not include 'undefined' enum Countries { Belgium, Brazil } // Input set enum PaymentMethod { Bancontact, Visa, Mastercard } // Output set const FavoritePaymentMethod = (country: Countries): PaymentMethod => switch(country) { case Countries.Belgium: return PaymentMethod.Bancontact; } }
  41. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD case Countries.Brazil: return PaymentMethod.Mastercard;

    enum Countries { Belgium, Brazil } // Input set enum PaymentMethod { Bancontact, Visa, Mastercard } // Output set const FavoritePaymentMethod = (country: Countries): PaymentMethod => switch(country) { case Countries.Belgium: return PaymentMethod.Bancontact; } }
  42. FAVORITE PAYMENT METHOD FAVORITE PAYMENT METHOD enum Countries { Belgium,

    Brazil } // Input set enum PaymentMethod { Bancontact, Visa, Mastercard } // Output set const FavoritePaymentMethod = (country: Countries): PaymentMethod => switch(country) { case Countries.Belgium: return PaymentMethod.Bancontact; case Countries.Brazil: return PaymentMethod.Mastercard; } }
  43. ABSOLUTE VALUE ABSOLUTE VALUE const Absolute Value = (inputValue: number):

    number => { if(inputValue >= 0) return inputValue else return (-1) * inputValue; }
  44. MAP ≈ FUNCTION MAP ≈ FUNCTION Referential Transparency "Kan ik

    een 'function application' vervangen door zijn resultaat?"
  45. MAP ≈ FUNCTION MAP ≈ FUNCTION Referential Transparency "Kan ik

    een 'function application' vervangen door zijn resultaat?" doSomething(Increment(1)) === doSomething(2)
  46. MAP ≈ FUNCTION MAP ≈ FUNCTION Referential Transparency "Als ik

    functie meermaals aanroep met dezelfde parameters, krijg ik dan steeds hetzelfde resultaat?"
  47. MAP ≈ FUNCTION MAP ≈ FUNCTION Referential Transparency "Als ik

    functie meermaals aanroep met dezelfde parameters, krijg ik dan steeds hetzelfde resultaat?" const value1 = Increment(1); const value2 = Increment(1); value1 === value2
  48. MAP ≈ FUNCTION MAP ≈ FUNCTION Functional Equivalence const IncrementA

    = (number: number): number => { return number + 1; } const IncrementB = (number: number): number => { return number + 2 - 1; } IncrementA(x) === IncrementB(x) // Voor elke mogelijke x!
  49. MAP ≈ FUNCTION MAP ≈ FUNCTION Honest Type Signatures =

    Geen impliciete input Geen impliciete output
  50. MAP ≈ FUNCTION MAP ≈ FUNCTION Honest Type Signatures =

    a.k.a Geen 'side effects' Geen impliciete input Geen impliciete output
  51. MAP ≈ FUNCTION MAP ≈ FUNCTION PURE FUNCTIONS PURE FUNCTIONS

    Transformeer input naar output Geen 'side effects'
  52. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' // Old const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BE') { invoices.splice(i,1); } } return invoices; }
  53. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' // Old invoices.splice(i,1) // Implicit output const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { if(invoices[i].CountryCode !== 'BE') { ; } } return invoices; }
  54. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' // New: Create a new list, and copy over const newList: Array<Invoice> = []; // Explicit output if(invoices[i].CountryCode !== 'BE') { // Do nothing } else { newList.push(invoices[i]); } const getBelgianInvoices = (invoices: Array<Invoice>): Array<Invoice> => { for (var i = invoices.length - 1; i >= 0; i--) { } return invoices; }
  55. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' // Old const updateInvoice = (invoice: Invoice): void => { invoice.LastUpdated = new Date(); // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === new Date().getTime()); createExcel("Update Report", updatedInvoices); }
  56. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' // Old new Date() // implicit input new Date().getTime() // Implicit const updateInvoice = (invoice: Invoice): void => { invoice.LastUpdated = ; // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i]); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === ); createExcel("Update Report", updatedInvoices); }
  57. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' , date: Date date const date = new Date(); , date date.getTime() const updateInvoice = (invoice: Invoice ): void => { invoice.LastUpdated = ; // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i] ); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === ); createExcel("Update Report", updatedInvoices); }
  58. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' void invoice.LastUpdated = // Implicit output invoices[i] const updateInvoice = (invoice: Invoice, date: Date): => { date; // Update other information } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); for(var i=0; i < invoices.length; i++) { updateInvoice( , date); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); createExcel("Update Report", updatedInvoices); }
  59. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' Invoice return { LastUpdated = date, // Update other information } invoices[i] = const updateInvoice = (invoice: Invoice, date: Date): => { } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i], date); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); createExcel("Update Report", updatedInvoices); }
  60. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' invoices[i] = // Implicit output ??? const updateInvoice = (invoice: Invoice, date: Date): Invoice => { return { LastUpdated = date, // Update other information } } const updateInvoices = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); for(var i=0; i < invoices.length; i++) { updateInvoice(invoices[i], date); } const updatedInvoices = invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); createExcel("Update Report", updatedInvoices); }
  61. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' const updateInvoicesPure = (invoices: Array<Invoice>, date: Date): Array<Invoice> = for(var i=0; i < invoices.length; i++) { invoices[i] = updateInvoice(invoices[i], date); // Implicit output ??? } return invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); } const updateInvoicesImpure = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); const updatedInvoices = updateInvoicesPure(invoices, date); createExcel("Update Report", updatedInvoices); }
  62. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' invoices[i] = updateInvoice(invoices[i], date); // Implicit output ??? const updateInvoicesPure = (invoices: Array<Invoice>, date: Date): Array<Invoice> = for(var i=0; i < invoices.length; i++) { } return invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); } const updateInvoicesImpure = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); const updatedInvoices = updateInvoicesPure(invoices, date); createExcel("Update Report", updatedInvoices); }
  63. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' .map(i => updateInvoice(i, date)) const updateInvoicesPure = (invoices: Array<Invoice>, date: Date): Array<Invoice> = return invoices .filter(i => i.LastUpdated.getTime() === date.getTime()); } const updateInvoicesImpure = async (): Promise<void> => { const invoices = readExcel(); const date = new Date(); const updatedInvoices = updateInvoicesPure(invoices, date); createExcel("Update Report", updatedInvoices); }
  64. EEN 'PROCEDURE' OMVORMEN NAAR EEN 'MAP' EEN 'PROCEDURE' OMVORMEN NAAR

    EEN 'MAP' const updateInvoicesPure = (invoices: Array<Invoice>, date: Date): Array<Invoice> = return invoices .map(i => updateInvoice(i, date)) .filter(i => i.LastUpdated.getTime() === date.getTime()); } const updateInvoicesImpure = async (): Promise<void> => { // IO (Input) const invoices = readExcel(); const date = new Date(); // Pure (domain) logic const updatedInvoices = updateInvoicesPure(invoices, date); // IO (Output) createExcel("Update Report", updatedInvoices); }
  65. WAT IS HET LAND VERBONDEN MET EEN WAT IS HET

    LAND VERBONDEN MET EEN FACTUUR? FACTUUR?
  66. WAT IS HET LAND VERBONDEN MET EEN WAT IS HET

    LAND VERBONDEN MET EEN FACTUUR? FACTUUR? const toCountry = (invoice: Invoice): Countries => { if (invoice.CountryCode === 'BE') return Countries.Belgium; if (invoice.CountryCode === 'BR') return Countries.Brazil; return ???; }
  67. WAT IS HET LAND VERBONDEN MET EEN WAT IS HET

    LAND VERBONDEN MET EEN FACTUUR? FACTUUR? undefined const toCountry = (invoice: Invoice): Countries => { if (invoice.CountryCode === 'BE') return Countries.Belgium; if (invoice.CountryCode === 'BR') return Countries.Brazil; return ; }
  68. WAT IS HET LAND VERBONDEN MET EEN WAT IS HET

    LAND VERBONDEN MET EEN FACTUUR? FACTUUR? TypeScript error: Type 'undefined' is not assignable to type 'Countries' const toCountry = (invoice: Invoice): Countries => { if (invoice.CountryCode === 'BE') return Countries.Belgium; if (invoice.CountryCode === 'BR') return Countries.Brazil; return undefined; }
  69. WAT IS HET LAND VERBONDEN MET EEN WAT IS HET

    LAND VERBONDEN MET EEN FACTUUR? FACTUUR? Countries | undefined const toCountry = (invoice: Invoice): => { if (invoice.CountryCode === 'BE') return Countries.Belgium; if (invoice.CountryCode === 'BR') return Countries.Brazil; return undefined; }
  70. WELKE BETAALMETHODE IS VERMOEDELIJK WELKE BETAALMETHODE IS VERMOEDELIJK GEASSOCIEERD AAN

    EEN FACTUUR? GEASSOCIEERD AAN EEN FACTUUR? const toFavoritePaymentMethod = (invoice: Invoice): PaymentMethod => { const country: Countries | undefined = toCountry(invoice); return FavoritePaymentMethod(country); }
  71. WELKE BETAALMETHODE IS VERMOEDELIJK WELKE BETAALMETHODE IS VERMOEDELIJK GEASSOCIEERD AAN

    EEN FACTUUR? GEASSOCIEERD AAN EEN FACTUUR? TypeScript error: Argument of type 'Countries | undefined' is not assignable to parameter of type 'Countries'. Type 'undefined' is not assignable to type 'Countries'. FavoritePaymentMethod(country); const toFavoritePaymentMethod = (invoice: Invoice): PaymentMethod => { const country: Countries | undefined = toCountry(invoice); return }
  72. WELKE BETAALMETHODE IS VERMOEDELIJK WELKE BETAALMETHODE IS VERMOEDELIJK GEASSOCIEERD AAN

    EEN FACTUUR? GEASSOCIEERD AAN EEN FACTUUR? PaymentMethod | undefined if(country === undefined) return undefined; else return FavoritePaymentMethod(country); const toFavoritePaymentMethod = (invoice: Invoice): => { const country: Countries | undefined = toCountry(invoice); }
  73. WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS

    GEASSOCIEERD MET EEN FACTUUR? GEASSOCIEERD MET EEN FACTUUR?
  74. WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS

    GEASSOCIEERD MET EEN FACTUUR? GEASSOCIEERD MET EEN FACTUUR? enum BalanceType { Credit, Debit }
  75. WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS

    GEASSOCIEERD MET EEN FACTUUR? GEASSOCIEERD MET EEN FACTUUR? const ToBalanceType = (country: PaymentMethod): BalanceType => { switch(country) { case PaymentMethod.Bancontact: return BalanceType.Debit; case PaymentMethod.Visa: return BalanceType.Credit; case PaymentMethod.Mastercard: return BalanceType.Credit; } } enum BalanceType { Credit, Debit }
  76. WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS

    GEASSOCIEERD MET EEN FACTUUR? GEASSOCIEERD MET EEN FACTUUR? const countryToBalanceType = (country: Country): BalanceType => { return BalanceType(FavoritePaymentMethod(country)); } enum BalanceType { Credit, Debit } const ToBalanceType = (country: PaymentMethod): BalanceType => { switch(country) { case PaymentMethod.Bancontact: return BalanceType.Debit; case PaymentMethod.Visa: return BalanceType.Credit; case PaymentMethod.Mastercard: return BalanceType.Credit; } }
  77. WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS WELKE 'BALANCE TYPE' (CREDIT/DEBIT) IS

    GEASSOCIEERD MET EEN FACTUUR? GEASSOCIEERD MET EEN FACTUUR? const toFavoriteBalanceType = (invoice: Invoice): BalanceType | undefined => { const country: Countries | undefined = toCountry(invoice); if(country === undefined) return undefined; else return countryToBalanceType(country); } enum BalanceType { Credit, Debit } const countryToBalanceType = (country: Country): BalanceType => { return BalanceType(FavoritePaymentMethod(country)); }
  78. MEERDERE PARAMETERS MEERDERE PARAMETERS type Payment = { Method: PaymentMethod

    Amount: number } const processPayment = (method: PaymentMethod, amount: number): void => { ... }
  79. MEERDERE PARAMETERS MEERDERE PARAMETERS const processPayment = (payment: Payment): void

    => { ... } const processPayment = (method: PaymentMethod, amount: number): void => { ... } type Payment = { Method: PaymentMethod Amount: number }
  80. EEN REPRESENTATIE VOOR 'OR' EEN REPRESENTATIE VOOR 'OR' type Currency

    = { Eur: number | null Real: number | null }
  81. EEN REPRESENTATIE VOOR 'OR' EEN REPRESENTATIE VOOR 'OR' const fiveEur:

    Currency = { Eur: 5, Real: null } type Currency = { Eur: number | null Real: number | null }
  82. EEN REPRESENTATIE VOOR 'OR' EEN REPRESENTATIE VOOR 'OR' const fiveReal:

    Currency = { Eur: null, Real: 5 } type Currency = { Eur: number | null Real: number | null }
  83. EEN REPRESENTATIE VOOR 'OR' EEN REPRESENTATIE VOOR 'OR' const fiveEurAndReal:

    Currency = { Eur: 5, Real: 5 } type Currency = { Eur: number | null Real: number | null }
  84. EEN REPRESENTATIE VOOR 'OR' EEN REPRESENTATIE VOOR 'OR' const noEurNoReal:

    Currency = { Eur: null, Real: null } type Currency = { Eur: number | null Real: number | null }
  85. DISCRIMINATED UNION DISCRIMINATED UNION (DISJOINT UNION/SUM TYPE/TAGGED UNION) (DISJOINT UNION/SUM

    TYPE/TAGGED UNION) (DISJUNCTE VERENIGING) (DISJUNCTE VERENIGING)
  86. DISCRIMINATED UNION DISCRIMINATED UNION type Eur = number type Real

    = number type Currency = Eur | Real const processCurrency = (val: Currency): number => { val // Is this in Eur or in Real?? }
  87. DISCRIMINATED UNION DISCRIMINATED UNION tag: 'Eur' tag: 'Real' type Eur

    = { , Value: number } type Real = { , Value: number } type Currency = Eur | Real
  88. DISCRIMINATED UNION DISCRIMINATED UNION const processCurrency = (val: Currency): number

    => { switch(val.tag) { case 'Eur': // Do something with eur return val.Value; case 'Real': // Do something with real return val.Value; } // TypeScript infers all cases have been handled } type Eur = { tag: 'Eur', Value: number } type Real = { tag: 'Real', Value: number } type Currency = Eur | Real
  89. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS?
  90. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS? // (a: number, b: number) => number const Add = (a: number, b: number): number => { return a + b; }
  91. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS? // (a: number) => (b: number) => number const Add = (a: number): ((b: number) => number) => { const func = (b: number): number => { return a + b; } return func; } // (a: number, b: number) => number const Add = (a: number, b: number): number => { return a + b; }
  92. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS? // (a: number) => (b: number) => number const Add = (a: number) => (b: number): number => { return a + b; } // (a: number, b: number) => number const Add = (a: number, b: number): number => { return a + b; } // (a: number) => (b: number) => number const Add = (a: number): ((b: number) => number) => { const func = (b: number) => { return a + b; } return func; }
  93. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS? (a: number, b: number): number (a: number) => (b: number): number // (a: number, b: number) => number const Add = => { return a + b; } // (a: number) => (b: number) => number const Add = (a: number): ((b: number) => number) => { const func = (b: number) => { return a + b; } return func; } // (a: number) => (b: number) => number const Add = => { return a + b; }
  94. WAT ALS EEN ELEMENT ZELF EEN 'MAP' IS? WAT ALS

    EEN ELEMENT ZELF EEN 'MAP' IS? (a: number, b: number): number const two = Add(1, 1); (a: number) => (b: number): number const two = Add(1)(1); // (a: number, b: number) => number const Add = => { return a + b; } // (a: number) => (b: number) => number const Add = => { return a + b; }
  95. PARTIAL APPLICATION PARTIAL APPLICATION const Add2: (b: number) => number

    = Add(2); const Add = (a: number) => (b: number): number => { return a + b; }
  96. PARTIAL APPLICATION PARTIAL APPLICATION const x = Add2(1); // 3

    const Add = (a: number) => (b: number): number => { return a + b; } const Add2: (b: number) => number = Add(2);
  97. PARTIAL APPLICATION PARTIAL APPLICATION const y = Add2(2); // 4

    const Add = (a: number) => (b: number): number => { return a + b; } const Add2: (b: number) => number = Add(2); const x = Add2(1); // 3
  98. PARTIAL APPLICATION PARTIAL APPLICATION const z = Add2(3); // 5

    const Add = (a: number) => (b: number): number => { return a + b; } const Add2: (b: number) => number = Add(2); const x = Add2(1); // 3 const y = Add2(2); // 4