Last active
March 17, 2022 04:56
-
-
Save youchenlee/5b0a8ac0406215eda05e447aa5c1d835 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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