Create a cond
function that takes 2 arguments:
- An matrix in the format:
[
[firstCondition, firstCallback],
[secondCondition, secondCallback],
...
]
Create a cond
function that takes 2 arguments:
[
[firstCondition, firstCallback],
[secondCondition, secondCallback],
...
]
/* | |
* imap lazily maps an interable. | |
* @param ref Either an iterable or an iterator | |
* @param fn Map function | |
* | |
* It works by always returning a new iterator so that | |
* you can chain other imaps without looping once more | |
* over the same array! | |
* | |
* Since iterators are returned, to start the real mapping |
const validPost = post => | |
post.title && post.title.length > 10 && post.content | |
const posts = | |
[ {title: 'short'} | |
, {title: 'fairly long', content: 'Foobar'} | |
, {title: 'yet another', content: 'fixture'} | |
] | |
const validPosts = posts.every(validPost) |
const range = (start, end) => | |
Array.from({length: end - start}, (_, i) => start + i) | |
const divides = x => y => | |
x % y === 0 | |
const prime = n => { | |
if (n < 2) | |
return false | |
if (n === 2) |
const checkAndLog = x => { | |
console.log('passed by', x) | |
return Number.isInteger(x) | |
} | |
const xs = ['a', 1, 2, 3, 4] | |
const ys = ['a', 'b', 'c', 'd', 'e'] | |
const xsHasinteger = xs.some(checkAndLog) | |
// passed by a |
const checkAndLog = x => { | |
console.log('passed by', x) | |
return Number.isInteger(x) | |
} | |
const xs = [1, 2, 3, 4, 5] | |
const ys = [6, 7, 'a', 9, 10] | |
const areXsIntegers = xs.every(checkAndLog) | |
// passed by 1 |
const xs = ['a', 'b', 'c', 'd', 5] | |
const ys = ['a', 'b', 'c', 'd', 'e'] | |
const xsHasinteger = xs.some(Number.isInteger) | |
const ysHasinteger = ys.some(Number.isInteger) | |
console.log(xsHasinteger) // true | |
console.log(ysHasinteger) // false |
const xs = [1, 2, 3, 4, 5] | |
const ys = [1, 2, 'a', 4, 5] | |
const areXsIntegers = xs.every(Number.isInteger) | |
const areYsIntegers = ys.every(Number.isInteger) | |
console.log(areXsIntegers) // true | |
console.log(areYsIntegers) // false |
const add = (x, y) => x + y | |
const sub = (x, y) => x - y | |
console.log('10 + 3 - 4 =', sub( add(10, 3), 4 ) ) |
const add = x => y => x + y | |
const sub = x => y => x - y | |
console.log('10 + 3 - 4 =', sub( add(10)(3) )(4) ) |