Share
Embed
All Things Open 2018: 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. You will 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, you will leave this talk with a deeper understanding and appreciation for functional programming so you can start exploring it yourself.
Jeremy Fairbank
October 22, 2018
Transcript
213 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 -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
const url = ids => `/orders?ids=${ids.join(',')}` const orderDetails = ({
id, date, customer }) => `Order ${id} on ${date} by ${customer}` const detailsForOrders = orders => orders.map(orderDetails) -
const url = ids => `/orders?ids=${ids.join(',')}` 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' } -
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) details.forEach(orderDetails => { console.log(orderDetails) }) } -
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: Cannot add/remove sealed array elements -
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('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
const greet = greeting => name => `${greeting}, ${name}` const
greetInEnglish = greet('Hi') const greetInSpanish = greet('Hola') greetInEnglish('All Things Open') // Hi, All Things Open greetInSpanish('All Things Open') // Hola, All Things Open -
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" -
greetInEnglish "All Things Open" -- Hi, All Things Open greetInSpanish
"All Things Open" -- Hola, All Things Open -
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('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const toUpper = string => string.toUpperCase() const greet = greeting
=> name => `${greeting}, ${name}` const exclaim = phrase => `${phrase}!` const excitedGreeting = name => exclaim(greet('Hi')(toUpper(name))) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, // ALL THINGS OPEN greet('Hi'), exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), // Hi, ALL THINGS OPEN exclaim, )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
const R = require('ramda') const excitedGreeting = name => R.pipe(
toUpper, greet('Hi'), exclaim, // Hi, ALL THINGS OPEN! )(name) excitedGreeting('All Things Open') // Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper -- ALL THINGS OPEN |> greet "Hi" |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" -- Hi, ALL THINGS OPEN |> exclaim excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
greet greeting name = greeting ++ ", " ++ name
exclaim phrase = phrase ++ "!" excitedGreeting name = name |> String.toUpper |> greet "Hi" |> exclaim -- Hi, ALL THINGS OPEN! excitedGreeting "All Things Open" -- Hi, ALL THINGS OPEN! -
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 } } -
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 ------------------------- Responsibilities.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 ------------------------- Responsibilities.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 ------------------------- Responsibilities.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 = "$" ++ String.fromFloat amount -
conference : String conference = "Lambda Squared" life : Int
life = 42 greet : String -> String greet name = "Hello, " ++ name formatCurrency : Float -> String formatCurrency amount = "$" ++ String.fromFloat amount -
conference : String conference = "Lambda Squared" life : Int
life = 42 greet : String -> String greet name = "Hello, " ++ name formatCurrency : Float -> String formatCurrency amount = "$" ++ String.fromFloat amount -
type alias Item = { price : Float } type
alias ShoppingCart = { items : List Item , discounts : List Float , tax : Float , shippingCost : Float } -
type alias Item = { price : Float } type
alias ShoppingCart = { items : List Item , discounts : List Float , tax : Float , shippingCost : Float } -
type alias Item = { price : Float } type
alias ShoppingCart = { items : List Item , discounts : List Float , tax : Float , shippingCost : Float } type alias Order = { status : String , customer : String , total : Float } -
type alias Order = { status : String , customer
: String , total : Float } -
type alias Order = { status : String , customer
: String , total : Float } -
type Status = New | Processing | Shipped type alias
Order = { status : Status , customer : String , total : Float } -
createOrder : ShoppingCart -> String -> Order 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 } -
Hard to test Code breaks unexpectedly Too much code Scary
to refactor Oh, null -
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 } } } -
getUserName : Id -> Dict Id User -> String getUserName
id users = Dict.get id users -
getUserName : Id -> Dict Id User -> String getUserName
id users = Dict.get id users -
getUserName : Id -> Dict Id User -> String getUserName
id users = Dict.get id users -
getUserName : Id -> Dict Id User -> String getUserName
id users = Dict.get id users -
getUserName : Id -> Dict Id User -> String getUserName
id users = Dict.get id users Maybe User -
getUserName : Id -> Dict Id User -> String getUserName
id users = case Dict.get id users of Just user -> user.name -
getUserName : Id -> Dict Id User -> String getUserName
id users = case Dict.get id users of Just user -> user.name -
getUserName : Id -> Dict Id User -> String getUserName
id users = case Dict.get id users of Just user -> user.name user -
getUserName : Id -> Dict Id User -> String getUserName
id users = case Dict.get id users of Just user -> user.name -
-- MISSING PATTERNS ------------------------------ App.elm This `case` does not have
branches for all possibilities: 31|> case Dict.get id users of 32|> Just user -> 33|> user.name Missing possibilities include: Nothing I would have to crash if I saw one of those. Add branches for them! -
getUserName : Id -> Dict Id User -> String getUserName
id users = case Dict.get id users of Just user -> user.name Nothing -> "<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 } } } -
getBestFriendLocation : Id -> Dict Id User -> Maybe Location
getBestFriendLocation id users = case Dict.get id users of Just user -> case user.bestFriendId of Just bestFriendId -> case Dict.get bestFriendId users of Just friend -> friend.location Nothing -> Nothing Nothing -> Nothing Nothing -> Nothing -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
getBestFriendLocation id users = Dict.get id users |> Maybe.andThen (\user
-> user.bestFriendId) |> Maybe.andThen (\fid -> Dict.get fid users) |> Maybe.andThen (\friend -> friend.location) -
Hard to test Code breaks unexpectedly Too much code Scary
to refactor Oh, null -
Hard to test Pure Functions Code breaks unexpectedly Too much
code Scary to refactor Oh, null -
Code breaks unexpectedly Immutable Data Too much code Scary to
refactor Oh, null Hard to test Pure Functions -
Too much code Curried, Composable Functions Scary to refactor Oh,
null Code breaks unexpectedly Immutable Data Hard to test Pure Functions -
Scary to refactor Strong, Static Types Oh, null Too much
code Curried, Composable Functions Code breaks unexpectedly Immutable Data Hard to test Pure Functions -
Oh, null 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