Skip to content

Instantly share code, notes, and snippets.

@youchenlee
Last active March 17, 2022 04:56
Show Gist options
  • Select an option

  • Save youchenlee/5b0a8ac0406215eda05e447aa5c1d835 to your computer and use it in GitHub Desktop.

Select an option

Save youchenlee/5b0a8ac0406215eda05e447aa5c1d835 to your computer and use it in GitHub Desktop.
// input: "12 / 3", output: 4
// map
(() => {
const split = str => str.split('/')
const divide = arr => arr[0] / arr[1]
const input = [
"12 / 3",
"6 / 3",
"6 / 0"
]
let res = input.map(split).map(divide)
console.log(res)
})();
// flatmap 處理副作用
(() => {
const split = str => {
let r = str.split('/')
if (r.length != 2) {
return []
}
return [r]
}
const divide = arr => {
if (arr[1] == 0) {
return []
}
return [arr[0] / arr[1]]
}
const input = [
"12 / 3",
"6 / 3",
"6 / 0"
]
let res = input.flatMap(split).flatMap(divide)
console.log(res)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment