Good
Afternoon
Is mise
Cory Brown
employed @ nav
I am happy to be here
JS default values
JS default values
const someFn = ({
doStuffForActiveItems = id,
config = {},
list = [],
delimiter = ''
} = {}) =>
list
.filter(({ isActive = false }) => isActive)
.map((item = {}) =>
doStuffForActiveItems({...config, ...item})
)
.map(({ name = ''}) => name)
.join(delimiter)
const someFn = ({
doStuffForActiveItems,
config,
list,
delimiter,
}) => {
if (Array.isArray(list)) {
const names = list
.filter(({ isActive }) => isActive)
.map((item) => {
const item = item || {}
if (
_.isPlainObject(config) &&
_.isPlainObject(item) &&
typeof doStuffForActiveItems === 'function'
) {
return doStuffForActiveItems({...config, ...item})
}
})
.map(({ name }) => {
name = name || ''
return name
})
if (typeof delimeter !== 'string') {
delimeter = ''
}
return name.join(delimiter)
} else {
return ''
}
}
We ❤️ JS default values
What value do we make
the default?
🤔
Like a placeholder?
const gimme1stThingName = (things = [{ name: 'thing name' }]) => {
const [first = { name: 'first thing name ' }] = things
return first.name
}
With the most likely value?
const gimme1stThingName = (things = [{ name: 'Boaty McBoatface' }]) => {
const [first = { name: 'Boaty McBoatface' }] = things
return first.name
}
Throw an error?
const gimme1stThingName = (things = missingArg(`Y U No giv things?`)) => {
const [first = missingArg(`Y things empty?`)] = things
return first.name
}
const missingArg = (message) => { throw new Error(message) }
🤷🏼♀️
Monoidal
Identity
Elements
🎙💧
You're Welcome!
?
What
the fork
is that?
?
Other great talks happing right now...
- Managing An Open Source Project by Kent C. Dodds
- The Evolution of an API: A Case For GraphQL by Kylie Stewart
Monoidal
Identity
Elements
Monoid
An algebraic structure with a single associative binary operation and an identity element
Monoid
An algebraic structure with a single associative binary operation and an identity element
Monoid
An algebraic structure with a single associative binary operation and an identity element
Monoid
An algebraic structure with a single associative binary operation and an identity element
Monoid
- A data type (String, Number, Array, etc.)
- An associative, binary operator (concat)
- An element with left and right identity
Number
+
-
*
/
N + i
i + N
=
N
=
i = 0
N * i
i * N
=
N
=
i = 1
Object to hold fragment
Monoid #1
Type: Number
Operator: addition (+)
Identity Element: 0
Monoid #2
Type: Number
Operator: multiplication (*)
Identity Element: 1
String
+
`${x}${y}`
''.concat
S + i = i + S = S
i = ''
`${S}${i}` = `${i}${S}` = S
S.concat(i) = i.concat(S) = S
Monoid #3
Type: String
Operator: concat (+, `${}${}`, ''.concat)
Identity Element: ''
Boolean
&&
||
B && i = i && B = B
i = true
B || i = i || B = B
i = false
Monoid #4
Type: Boolean
Operator: &&
Identity Element: true
Monoid #5
Type: Boolean
Operator: ||
Identity Element: false
Array
[].concat
[...]
A.concat(i) = i.concat(A) = A
i = []
[...A, ...i] = [...i, ...A] = A
Monoid #6
Type: Array
Operator: concat, [...]
Identity Element:[]
Object
Object.assign
{...}
Object.assign(O, i) = Object.assign(i, O) = O
i = {}
{...O, ...i} = {...i, ...O} = O
Monoid #7
Type: Object
Operator: merge (Object.assign, {...})
Identity Element: {}
Function
🤔
F 🤔 i = i 🤔 F = F
i = 🤷♀️
Function
Just a
const compose = (...fns) => (data) => fns.reduceRight((result, fn) => fn(result), data)
compose
compose(F, i) = compose(i, F) = F
i = 🤷♀️
i = (x) => x
Monoid #8
Type: Function
Operator: compose
Identity Element: (x) => x
const someFn = ({
doStuffForActiveItems = id, /* <- monoidal identity element */
config = {}, /* <- monoidal identity element */
list = [], /* <- monoidal identity element */
delimiter = '' /* <- monoidal identity element */
} = {} /* <- monoidal identity element */) =>
list
.filter(({ isActive = false }) => isActive)
.map((item = {} /* <- monoidal identity element */) =>
doStuffForActiveItems({...config, ...item})
)
.map(({ name = '' /* <- monoidal identity element */}) => name)
.join(delimiter)
Tapadh leibh
p.s. Nav is hiring. Come talk to me. 😁
Monodial Identity Elements
By Cory Brown
Monodial Identity Elements
- 925