This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const func = ( | |
{ | |
fallbackMessage: msg = 'Unknown', | |
offset = 4, | |
pos: { x = 0, y = 0 } = {}, | |
...options | |
} = {}, | |
data = {}, | |
) => { | |
const { title = '', items: [head, ...tail] = [] } = data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const func = (params = {}, data = {}) => { | |
const { | |
fallbackMessage: msg = 'Unknown', | |
offset = 4, | |
pos = {}, | |
...options | |
} = params; | |
const { x = 0, y = 0 } = pos; | |
const { title = '', items = [] } = data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Circle.prototype.area.bind({ r: 4 })() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialState = { | |
1: circle([2.5, 0], 1.5), | |
2: circle([-2, 0], 2), | |
}; | |
const { getState, dispatch } = createStore(initialState, reducer); | |
area(getState(1).r); // -> 7.0685834705770345; | |
area(getState(2).r); // -> 12.566370614359172; | |
const [hit] = intersect(getState(1), getState(2)); | |
hit; // -> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducer = (state, action) => { | |
const actions = { | |
move: ({ id, payload }) => ({ ...state, [id]: setPos(state[id], payload) }), | |
hit: ({ id1, id2 }) => ({ | |
...state, | |
[id1]: incHits(state[id1]), | |
[id2]: incHits(state[id2]), | |
}), | |
}; | |
return actions[action.type](action); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepFreeze = item => { | |
const complex = Object(item) === item; | |
if (Object.isFrozen(item) || !complex) return item; | |
Object.values(item).forEach(deepFreeze); | |
return Object.freeze(item); | |
}; | |
const createStore = (initState = {}, reducer) => { | |
let state = deepFreeze(initState); | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeCircle = (pos = [0, 0], r = 0) => { | |
let state = circle(pos, r); | |
return { | |
getState: () => state, | |
incHits: () => (state = incHits(state)), | |
setPos: pos => (state = setPos(state, pos)), | |
area: () => area(r), | |
intersect: c2 => { | |
let hit; | |
[hit, state] = intersect(state, c2.getState()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let c1 = circle([2.5, 0], 1.5); | |
let c2 = circle([-2, 0], 2); | |
const c2ref = c2; // Use a reference to c2 at this point in time | |
area(c1.r); // -> 7.0685834705770345 | |
area(c2.r); // -> 12.566370614359172 | |
let hit; | |
[hit, c1, c2] = intersect(c1, c2); | |
hit; // -> false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Circle data object creator | |
const circle = (pos = [0, 0], r = 0) => ({ pos, r, hits: 0 }); | |
// Immutable-updates helpers | |
const incHits = obj => ({ ...obj, hits: (obj.hits || 0) + 1 }); | |
const setPos = (obj, pos = [0, 0]) => ({ ...obj, pos }); | |
// Circle computations | |
const area = r => r ** 2 * Math.PI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const c1 = makeCircle([2.5, 0], 1.5); | |
const c2 = makeCircle([-2, 0], 2); | |
c1.area(); // -> 7.0685834705770345 | |
c2.area(); // -> 12.566370614359172 | |
c1.intersect(c2); // -> false | |
c1.getHits(); // -> 0 | |
c1.pos[0] = 1; |
NewerOlder