Share
Embed
CodeMash 2020: Practical Functional Programming
Functional programming is more than just math and monads. Functional programming empowers developers to solve real problems with safe, predictable, and maintainable code. In this talk, discover the basics of functional programming and how to apply functional concepts in a practical manner. Learn how pure functions are easily testable, how to compose specialized functions to create more complex functions, how immutable data prevents bugs, how to prevent runtime errors with static types, how to safely model nulls with special types, and more! Most importantly, leave this talk with a deeper understanding and appreciation for functional programming to start exploring it yourself.
Jeremy Fairbank
January 10, 2020
Transcript
239 slides
-
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
const td = require('testdouble') describe('order details', () => { let
originalLog = console.log let printOrderDetails let fetchJson beforeEach(() => { fetchJson = td.replace('./fetchJson') console.log = td.function('console.log') printOrderDetails = require('./printOrderDetails') }) afterEach(() => { td.reset() console.log = originalLog }) -
const td = require('testdouble') describe('order details', () => { let
originalLog = console.log let printOrderDetails let fetchJson beforeEach(() => { fetchJson = td.replace('./fetchJson') console.log = td.function('console.log') printOrderDetails = require('./printOrderDetails') }) afterEach(() => { td.reset() console.log = originalLog }) -
const td = require('testdouble') describe('order details', () => { let
originalLog = console.log let printOrderDetails let fetchJson beforeEach(() => { fetchJson = td.replace('./fetchJson') console.log = td.function('console.log') printOrderDetails = require('./printOrderDetails') }) afterEach(() => { td.reset() console.log = originalLog }) -
const td = require('testdouble') describe('order details', () => { let
originalLog = console.log let printOrderDetails let fetchJson beforeEach(() => { fetchJson = td.replace('./fetchJson') console.log = td.function('console.log') printOrderDetails = require('./printOrderDetails') }) afterEach(() => { td.reset() console.log = originalLog }) -
it('prints details for multiple orders', async () => { td
.when(fetchJson('/orders?ids=1,2,3')) .thenResolve([ { id: 1, date: '12/1/17', customer: 'Tucker' }, { id: 2, date: '11/25/17', customer: 'Sally' }, { id: 3, date: '3/30/18', customer: 'Joe' }, ]) await printOrderDetails([1, 2, 3]) td.verify(console.log('Order 1 on 12/1/17 by Tucker')) td.verify(console.log('Order 2 on 11/25/17 by Sally')) td.verify(console.log('Order 3 on 3/30/18 by Joe')) }) }) -
it('prints details for multiple orders', async () => { td
.when(fetchJson('/orders?ids=1,2,3')) .thenResolve([ { id: 1, date: '12/1/17', customer: 'Tucker' }, { id: 2, date: '11/25/17', customer: 'Sally' }, { id: 3, date: '3/30/18', customer: 'Joe' }, ]) await printOrderDetails([1, 2, 3]) td.verify(console.log('Order 1 on 12/1/17 by Tucker')) td.verify(console.log('Order 2 on 11/25/17 by Sally')) td.verify(console.log('Order 3 on 3/30/18 by Joe')) }) }) -
it('prints details for multiple orders', async () => { td
.when(fetchJson('/orders?ids=1,2,3')) .thenResolve([ { id: 1, date: '12/1/17', customer: 'Tucker' }, { id: 2, date: '11/25/17', customer: 'Sally' }, { id: 3, date: '3/30/18', customer: 'Joe' }, ]) await printOrderDetails([1, 2, 3]) td.verify(console.log('Order 1 on 12/1/17 by Tucker')) td.verify(console.log('Order 2 on 11/25/17 by Sally')) td.verify(console.log('Order 3 on 3/30/18 by Joe')) }) }) -
it('prints details for multiple orders', async () => { td
.when(fetchJson('/orders?ids=1,2,3')) .thenResolve([ { id: 1, date: '12/1/17', customer: 'Tucker' }, { id: 2, date: '11/25/17', customer: 'Sally' }, { id: 3, date: '3/30/18', customer: 'Joe' }, ]) await printOrderDetails([1, 2, 3]) td.verify(console.log('Order 1 on 12/1/17 by Tucker')) td.verify(console.log('Order 2 on 11/25/17 by Sally')) td.verify(console.log('Order 3 on 3/30/18 by Joe')) }) }) -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
async function printOrderDetails(ids) { let url = `/orders?ids=${ids.join(',')}` let orders
= await fetchJson(url) for (let i = 0; i < orders.length; i++) { let { id, date, customer } = orders[i] let details = `Order ${id} on ${date} by ${customer}` console.log(details) } } -
const add = (x, y) => x + y add(2,
3) === 5 add(2, 3) === 5 add(2, 3) === 5 Pure -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
it('creates details for orders', () => { let orders =
[ { id: 1, date: '12/1/17', customer: 'Tucker' }, { id: 2, date: '11/25/17', customer: 'Sally' }, { id: 3, date: '3/30/18', customer: 'Joe' }, ] expect(detailsForOrders(orders)).toEqual([ 'Order 1 on 12/1/17 by Tucker', 'Order 2 on 11/25/17 by Sally', 'Order 3 on 3/30/18 by Joe', ]) }) -
async function printOrderDetails(ids) { let orders = await fetchJson(url(ids)) let
details = detailsForOrders(orders) for (let i = 0; i < details.length; i++) { console.log(details[i]) } } -
async function printOrderDetails(ids) { let orders = await fetchJson(url(ids)) let
details = detailsForOrders(orders) for (let i = 0; i < details.length; i++) { console.log(details[i]) } } -
async function printOrderDetails(ids) { let orders = await fetchJson(url(ids)) let
details = detailsForOrders(orders) for (let i = 0; i < details.length; i++) { console.log(details[i]) } } -
async function printOrderDetails(ids) { let orders = await fetchJson(url(ids)) let
details = detailsForOrders(orders) for (let i = 0; i < details.length; i++) { console.log(details[i]) } } -
const orderDetails = ({ id, date, customer }) => `Order
${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const orderDetails = ({ id, date, customer }) => `Order
${id} on ${date} by ${customer}` function detailsForOrders(orders) { let details = [] for (let i = 0; i < orders.length; i++) { details.push(orderDetails(orders[i])) } return details } -
const orderDetails = ({ id, date, customer }) => `Order
${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
const orderDetails = ({ id, date, customer }) => `Order
${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
{ id: 1, date: '12/1/17', customer: 'Tucker' } { id:
2, date: '11/25/17', customer: 'Sally' } { id: 3, date: '3/30/18', customer: 'Joe' } -
{ id: 1, date: '12/1/17', customer: 'Tucker' } { id:
2, date: '11/25/17', customer: 'Sally' } { id: 3, date: '3/30/18', customer: 'Joe' } [ 'Order 1 on 12/1/17 by Tucker', ] map orderDetails -
[ 'Order 1 on 12/1/17 by Tucker', 'Order 2 on
11/25/17 by Sally', ] orderDetails { id: 1, date: '12/1/17', customer: 'Tucker' } { id: 2, date: '11/25/17', customer: 'Sally' } { id: 3, date: '3/30/18', customer: 'Joe' } map -
orderDetails [ 'Order 1 on 12/1/17 by Tucker', 'Order 2
on 11/25/17 by Sally', 'Order 3 on 3/30/18 by Joe', ] map { id: 1, date: '12/1/17', customer: 'Tucker' } { id: 2, date: '11/25/17', customer: 'Sally' } { id: 3, date: '3/30/18', customer: 'Joe' } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= hobbies } describe() { if (this.hobbies.length === 0) { return `${this.name} doesn't do much` } let hobbies = this.hobbies.splice(0, 2).join(' and ') return `${this.name} likes to do things such as ${hobbies}` } } -
let person = new Person('Jeremy', [ 'programming', 'reading', 'playing music',
]) person.describe() // Jeremy likes to do things such as programming and reading person.describe() // Jeremy likes to do things such as playing music -
let person = new Person('Jeremy', [ 'programming', 'reading', 'playing music',
]) person.describe() // Jeremy likes to do things such as programming and reading person.describe() // Jeremy likes to do things such as playing music -
let person = new Person('Jeremy', [ 'programming', 'reading', 'playing music',
]) person.describe() // Jeremy likes to do things such as programming and reading person.describe() // Jeremy likes to do things such as playing music -
let person = new Person('Jeremy', [ 'programming', 'reading', 'playing music',
]) person.describe() // Jeremy likes to do things such as programming and reading person.describe() // Jeremy likes to do things such as playing music -
class Person { constructor(name, hobbies) { this.name = name this.hobbies
= Object.freeze(hobbies) } // ... } -
class Person { // ... describe() { // ... let
hobbies = this.hobbies.splice(0, 2).join(' and ') // ... } } -
class Person { // ... describe() { // ... let
hobbies = this.hobbies.splice(0, 2).join(' and ') // ... } } TypeError -
class Person { // ... describe() { // ... let
hobbies = this.hobbies.slice(0, 2).join(' and ') // ... } } -
const create = (name, hobbies) => Object.freeze({ name, hobbies: Object.freeze(hobbies)
}) function describe(person) { if (person.hobbies.length === 0) { return `${person.name} doesn't do much` } let hobbies = person.hobbies.slice(0, 2).join(' and ') return `${person.name} likes to do things such as ${hobbies}` } -
const create = (name, hobbies) => Object.freeze({ name, hobbies: Object.freeze(hobbies)
}) function describe(person) { if (person.hobbies.length === 0) { return `${person.name} doesn't do much` } let hobbies = person.hobbies.slice(0, 2).join(' and ') return `${person.name} likes to do things such as ${hobbies}` } -
const create = (name, hobbies) => Object.freeze({ name, hobbies: Object.freeze(hobbies)
}) function describe(person) { if (person.hobbies.length === 0) { return `${person.name} doesn't do much` } let hobbies = person.hobbies.slice(0, 2).join(' and ') return `${person.name} likes to do things such as ${hobbies}` } -
let person = create('Jeremy', []) person = addHobby('programming', person) person
= addHobby('reading', person) describe(person) // Jeremy likes to do things such as programming and reading -
let person = create('Jeremy', []) person = addHobby('programming', person) person
= addHobby('reading', person) describe(person) // Jeremy likes to do things such as programming and reading -
let person = create('Jeremy', []) person = addHobby('programming', person) person
= addHobby('reading', person) describe(person) // Jeremy likes to do things such as programming and reading -
let person = create('Jeremy', []) person = addHobby('programming', person) person
= addHobby('reading', person) describe(person) // Jeremy likes to do things such as programming and reading -
let person = create('Jeremy', []) person = addHobby('programming', person) person
= addHobby('reading', person) describe(person) // Jeremy likes to do things such as programming and reading -
const greetInEnglish = name => `Hi, ${name}` const greetInSpanish =
name => `Hola, ${name}` -
const greet = (greeting, name) => `${greeting}, ${name}` // ..
const greetInEnglish = name => greet('Hi', name) const greetInSpanish = name => greet('Hola', name) -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('CodeMash') // Hi, CodeMash greetInSpanish('CodeMash') // Hola, CodeMash -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') Curried Function Partial Application -
greet greeting name = greeting ++ ", " ++ name
greetInEnglish = greet "Hi" greetInSpanish = greet "Hola" -
greet greeting name = greeting ++ ", " ++ name
greetInEnglish = greet "Hi" greetInSpanish = greet "Hola" -
greet greeting name = greeting ++ ", " ++ name
greetInEnglish = greet "Hi" greetInSpanish = greet "Hola" -
greet greeting name = greeting ++ ", " ++ name
greetInEnglish = greet "Hi" greetInSpanish = greet "Hola" -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, // CODEMASH greet('Hi'), exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), // Hi, CODEMASH exclaim, )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, // Hi, CODEMASH! )(name) excitedGreeting('CodeMash') // Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper -- CODEMASH |> greet "Hi" |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" -- Hi, CODEMASH |> exclaim excitedGreeting "CodeMash" -- Hi, CODEMASH! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim -- Hi, CODEMASH! excitedGreeting "CodeMash" -- Hi, CODEMASH! -
function createOrder(shoppingCart, customer) { let total = 0 shoppingCart.items.forEach(item =>
{ total += item.price }) shoppingCart.discounts.forEach(discount => { total *= 1 - discount }) total *= 1 + shoppingCart.tax total += shoppingCart.shippingCost total = Math.round(total * 100) / 100 return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts),
addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts),
addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts),
addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
function createOrder(shoppingCart, customer) { let total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts),
addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
Hard to test Hard to follow Code breaks unexpectedly Too
much code Scary to refactor -
function createOrder(shoppingCart, customer) { let total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts),
addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
const createOrder = customer => shoppingCart => { let total
= R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts), addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: 'new', customer, total } } -
createOrder shoppingCart customer = let total = addPrices shoppingCart.items 0
|> addDiscounts shoppingCart.discounts |> addTax shoppingCart.tax |> add shoppingCart.shippingCost |> roundTotal in { status = "new" , customer = customer , total = total } -
createOrder shoppingCart customer = let total = addPrices shoppingCart.items 0
|> addDiscounts shoppingCart.discounts |> addTax shoppingCart.tax |> add shoppingCart.shippingCost |> roundTotal in { status = "new" , customer = customer , total = total } -
createOrder shoppingCart customer = let total = addPrices shoppingCart.items 0
|> addDiscounts shoppingCart.discounts |> addTax shoppingCart.tax |> add shoppingCart.shippingCost |> roundTotal in { status = "new" , customer = customer , total = total } -
createOrder customer shoppingCart = let total = addPrices shoppingCart.items 0
|> addDiscounts shoppingCart.discounts |> addTax shoppingCart.tax |> add shoppingCart.shippingCost |> roundTotal in { status = "new" , customer = customer , total = total } -
-- TYPE MISMATCH ------------------------- App.elm The 2nd argument to `createOrder`
is not what I expect: 66| createOrder myShoppingCart "Tucker" ^^^^^^^^ This argument is a string of type: String But `createOrder` needs the 2nd argument to be: { b | discounts : List Float , items : List { a | price : Float } , shippingCost : Float , tax : Float } -
-- TYPE MISMATCH ------------------------- App.elm The 2nd argument to `createOrder`
is not what I expect: 66| createOrder myShoppingCart "Tucker" ^^^^^^^^ This argument is a string of type: String But `createOrder` needs the 2nd argument to be: { b | discounts : List Float , items : List { a | price : Float } , shippingCost : Float , tax : Float } -
-- TYPE MISMATCH ------------------------- App.elm The 2nd argument to `createOrder`
is not what I expect: 66| createOrder myShoppingCart "Tucker" ^^^^^^^^ This argument is a string of type: String But `createOrder` needs the 2nd argument to be: { b | discounts : List Float , items : List { a | price : Float } , shippingCost : Float , tax : Float } -
conference : String conference = "Lambda Squared" life : Int
life = 42 greet : String -> String greet name = "Hello, " ++ name formatCurrency : Float -> String formatCurrency amount = "$" ++ toString amount -
conference : String conference = "Lambda Squared" life : Int
life = 42 greet : String -> String greet name = "Hello, " ++ name formatCurrency : Float -> String formatCurrency amount = "$" ++ toString amount -
conference : String conference = "Lambda Squared" life : Int
life = 42 greet : String -> String greet name = "Hello, " ++ name formatCurrency : Float -> String formatCurrency amount = "$" ++ toString amount -
type Item = { price: number, } type ShoppingCart =
{ items: Item[], discounts: number[], tax: number, shippingCost: number, } -
type Item = { price: number, } type ShoppingCart =
{ items: Item[], discounts: number[], tax: number, shippingCost: number, } -
type Item = { price: number, } type ShoppingCart =
{ items: Item[], discounts: number[], tax: number, shippingCost: number, } type Order = { status: string, customer: string, total: number, } -
enum Status { New, Processing, Shipped, } type Order =
{ status: Status, customer: string, total: number, } -
function createOrder( shoppingCart: ShoppingCart, customer: string ): Order { let
total = R.pipe( addPrices(shoppingCart.items), addDiscounts(shoppingCart.discounts), addTax(shoppingCart.tax), add(shoppingCart.shippingCost), roundTotal, )(0) return { status: Status.New, customer, total } } -
Hard to test Hard to follow Code breaks unexpectedly Too
much code Scary to refactor Oh, null and undefined -
function getBestFriendLocation(id, users) { let user = users.get(id) let friend
= users.get(user.bestFriendId) return friend.location } -
function getBestFriendLocation(id, users) { let user = users.get(id) let friend
= users.get(user.bestFriendId) return friend.location } -
function getBestFriendLocation(id, users) { let user = users.get(id) let friend
= users.get(user.bestFriendId) return friend.location } -
function getBestFriendLocation(id, users) { let user = users.get(id) let friend
= users.get(user.bestFriendId) return friend.location } -
function getBestFriendLocation(id, users) { let user = users.get(id) let friend
= users.get(user.bestFriendId) return friend.location } null -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user)
{ let friend = users.get(user.bestFriendId) return friend.location } } -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user)
{ let friend = users.get(user.bestFriendId) return friend.location } } null -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user
&& user.bestFriendId) { let friend = users.get(user.bestFriendId) return friend.location } } -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user
&& user.bestFriendId) { let friend = users.get(user.bestFriendId) return friend.location } } null -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user
&& user.bestFriendId) { let friend = users.get(user.bestFriendId) if (friend) { return friend.location } } } -
import { Maybe } from 'true-myth' Maybe.just(42) // Just 42
Maybe.nothing() // Nothing Maybe.of(42) // Just 42 Maybe.of(null) // Nothing Maybe.of(undefined) // Nothing -
function mapGet(key, map) { let result = map.get(key) return result
== null ? Maybe.nothing() : Maybe.just(result) } -
function getUserName(id, users) { let user = mapGet(id, users) if
(user.isJust()) { return user.unsafelyUnwrap().name } } -
function getUserName(id, users) { let user = mapGet(id, users) if
(user.isJust()) { return user.unsafelyUnwrap().name } } Maybe User -
function getUserName(id, users) { let user = mapGet(id, users) if
(user.isJust()) { return user.unsafelyUnwrap().name } } -
function getUserName(id, users) { let user = mapGet(id, users) if
(user.isJust()) { return user.unsafelyUnwrap().name } } user -
function getUserName(id, users) { let user = mapGet(id, users) if
(user.isJust()) { return user.unsafelyUnwrap().name } } Nothing? -
function getUserName( id: Id, users: Map<Id, User> ): string {
let user = mapGet(id, users) if (user.isJust()) { return user.unsafelyUnwrap().name } } -
null.ts:27:4 - error TS2366: Function lacks ending return statement and
return type does not include 'undefined'. 27 ): string { ~~~~~~ Found 1 error. $ tsc --lib es2015 --strictNullChecks null.ts -
function getUserName( id: Id, users: Map<Id, User> ): string {
let user = mapGet(id, users) return user.isJust() ? user.unsafelyUnwrap().name : '<anonymous>' } -
function getBestFriendLocation(id, users) { let user = users.get(id) if (user
&& user.bestFriendId) { let friend = users.get(user.bestFriendId) if (friend) { return friend.location } } } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
let user = mapGet(id, users) if (user.isJust()) { let { bestFriendId } = user.unsafelyUnwrap() if (bestFriendId.isJust()) { let friend = mapGet(bestFriendId.unsafelyUnwrap(), users) if (friend.isJust()) { return friend.unsafelyUnwrap().location } return Maybe.nothing() } return Maybe.nothing() } return Maybe.nothing() } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) // Maybe<User> .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) // Maybe<Id> .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) // Maybe<User> .chain(friend => friend.location) } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) // Maybe<Location> } -
function getBestFriendLocation( id: Id, users: Map<Id, User> ): Maybe<Location> {
return mapGet(id, users) .chain(user => user.bestFriendId) .chain(bestFriendId => mapGet(bestFriendId, users)) .chain(friend => friend.location) // Maybe<Location> } -
Hard to test Hard to follow Code breaks unexpectedly Too
much code Scary to refactor Oh, null and undefined -
Hard to test Pure Functions Hard to follow Code breaks
unexpectedly Too much code Scary to refactor Oh, null and undefined -
Hard to test Pure Functions Hard to follow Declarative Code
Code breaks unexpectedly Too much code Scary to refactor Oh, null and undefined -
Code breaks unexpectedly Immutable Data Too much code Scary to
refactor Hard to test Pure Functions Hard to follow Declarative Code Oh, null and undefined -
Too much code Curried, Composable Functions Scary to refactor Code
breaks unexpectedly Immutable Data Hard to test Pure Functions Hard to follow Declarative Code Oh, null and undefined -
Scary to refactor Strong, Static Types Too much code Curried,
Composable Functions Code breaks unexpectedly Immutable Data Hard to test Pure Functions Hard to follow Declarative Code Oh, null and undefined -
Oh, null and undefined Maybe and Monads Scary to refactor
Strong, Static Types Too much code Curried, Composable Functions Code breaks unexpectedly Immutable Data Hard to test Pure Functions Hard to follow Declarative Code