自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從數(shù)組Reduce中構(gòu)建Map等12個函數(shù)

開發(fā) 前端
如我們所知,JavaScript是當今流行語言中對函數(shù)式編程支持最好的編程語言。而函數(shù)式編程"從一到無窮”的起點是 reduce,本文我們將嘗試從 reduce 起步構(gòu)建所有的其他函數(shù)。

[[388814]]

如我們所知,JavaScript是當今流行語言中對函數(shù)式編程支持最好的編程語言。而函數(shù)式編程"從一到無窮”的起點是 reduce,本文我們將嘗試從 reduce 起步構(gòu)建所有的其他函數(shù)。


一、萬物之始 array.reduce 的應(yīng)用

Reduce的核心在于降維,將數(shù)組reduce為一個值,比如求和:

  1. const arr = [52, 71, 27, 38]; 
  2. const sum = (x, y) => x + y; 
  3. const cusSum = arr.reduce(sum, 0); 

 將reduce作為思考工具,腦子中要始終留有 initial-value 初始值。

二、構(gòu)建 array.map 從數(shù)學到編程

map是數(shù)學思維而直接入編程,從reduce中模擬構(gòu)建為:

  1. const cusMap = (arr, fn) 
  2.                => arr.reduce((x, y) 
  3.                => x.concat(fn(y)), []); 

 三、構(gòu)建 array.flat array.flatMap 拍平數(shù)組

從array.flat我們窺探到 declaratively 編程的優(yōu)勢,只須將精力專注到要完成的任務(wù)上,而不必理會實現(xiàn)細節(jié)。用 reduce 實現(xiàn)為:

當只 flat 到一層深度時候:

  1. # flat only to one level  
  2. const flat1 = arr => [].concat(...arr); 
  3. const flat2 = arr = arr.reduce(acc, v => acc.concat(v), []) 

 當需要 flat 到任意深度時, 用 reduce 完全重構(gòu) flat:

  1. if (!Array.prototype.flat) { 
  2.   Array.prototype.flat = function(n = 1) { 
  3.     this.flatAllX = () => 
  4.       this.reduce( 
  5.         (f, v) => f.concat(Array.isArray(v) ? v.flat(Infinity) : v), 
  6.         [] 
  7.       ); 
  8.  
  9.     this.flatOneX = () => this.reduce((f, v) => f.concat(v), []); 
  10.      
  11.     return n === Infinity 
  12.       ? this.flatAllX() 
  13.       : n === 1 
  14.       ? this.flatOneX() 
  15.       : this.flatOneX().flat(n - 1); 
  16.   }; 

 四、array.filter 邁向高階的邏輯判斷 logic predicate

為什么要用 reduce 重新構(gòu)建,因為能夠幫助在頭腦中始終擦亮 function 與 最終輸出 acculator 的概念。

  1. const cusFilter = (arr, fn) 
  2.                 =>  arr.reduce((acc, val) 
  3.                 => (fn(val) ? acc.concat(y) 
  4.                         : acc), []); 

 五、array.find 與 array.findIndex 只找出首個元素

array.filter將會篩選出來全部的符合要求的元素,當我們只要單個元素的時候則應(yīng)用 array.find.

  1. const cusFind = arr.reduce((acc, val) 
  2.    => (acc === undefined && fn(val) ? val 
  3.        : acc), undefined); 

 重新構(gòu)建 array.findIndex:

  1. const cusFindIndex = arr.reduce((x, y, i) 
  2.                      => (x == -1 && fn(y) ? i  
  3.                          : x), -1); 

 進而,我們用 find 與 findIndex 簡單的構(gòu)建 includes 與 indexOf。

  1. arr.includes(value); // arr.find(v => v === value) 
  2. arr.indexOf(value);  // arr.findIndex(v => v === value) 

 六、快捷的 array.some 與 array.every

array 與 some 兩函數(shù)雖然簡單,思考和使用的時候尤其順手。

  1. // arr.every(fn); 
  2. arr.reduce((a, v) => a && fn(v), true); // a for accumulator,  
  3. // arr.some(fn); 
  4. arr.reduce((a, v) => a|| fn(v), false); // v for value  

 至此,我們從reduce出發(fā),將其他幾個高階函數(shù)全部模擬出來,reduce模擬幫助我們強化對每個函數(shù)中輸入的arguments與輸出的 result 的辨識。

 

責任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2024-01-10 08:47:48

Python函數(shù)Map()

2025-04-11 08:00:00

函數(shù)式編程Python

2024-06-04 00:20:00

Python函數(shù)

2024-11-08 12:42:34

Rustmapfilter

2010-06-03 16:46:23

Hadoop Map-

2022-07-06 10:04:45

JavaScript數(shù)組前端

2023-01-03 13:30:14

C++代碼map

2021-03-05 07:45:59

JSreducemap

2022-05-07 19:51:22

微軟WindowsWindows 12

2014-03-18 10:16:58

SVM

2020-07-29 07:52:41

JavaScript

2020-07-19 15:32:13

JavaScript開發(fā)技術(shù)

2020-12-22 14:11:45

JS forEach()map()

2017-03-28 21:25:19

無循環(huán)代碼JavaScript

2021-09-29 06:03:37

JavaScriptreduce() 前端

2022-10-21 08:02:40

reduce?初始值循環(huán)

2023-05-06 07:27:47

2021-08-24 09:39:23

ReduceJS數(shù)組

2022-06-01 09:06:58

ES6數(shù)組函數(shù)

2023-11-14 08:10:06

高級函數(shù)Python
點贊
收藏

51CTO技術(shù)棧公眾號