JavaScript惰性求值的一種實現(xiàn)方法示例
前言
在學習 Haskell 時,我遇到了這種寫法:
sum (takeWhile (<10000) (filter odd (map (^2) [1..])))
這段代碼的意思是,找出自然整數(shù)中小于 10000 的同時是乘方數(shù)和奇數(shù)的數(shù)字,再把這些數(shù)加總。由于 Haskell 的懶運算特性,上面的程序并不會立馬生成從 1 到 無限大的自然數(shù)列表,而是會等待 takeWhile 指令,再生成符合條件的列表。如果用 JS 來寫,很難寫出這么簡潔高表達性的代碼。一個可能的思路就是寫個 while 循環(huán),然后找到符合條件的數(shù)進行加總。這個比較簡單,我就不演示了。
但是如果我們要用高階函數(shù)來模擬 Haskell 的寫法,就要想個辦法實現(xiàn)懶運算了。提到懶,首先想到的就是 Iterator 。沒人踢它一腳告訴它 next(),它會一直坐那兒不動的。
現(xiàn)在我們就來用 Iterator 來實現(xiàn)一個懶運算。
首先定義一個生成從 1 到無窮大自然數(shù)的 generator :
const numbers = function*() { let i = 1 while (true) { yield i++ } }
由于只有在 generator 執(zhí)行后生成的 iterable 上執(zhí)行 next() 方法,yield 才會執(zhí)行,所以我們要做的主要工作就是實現(xiàn)不同的 next 方法,達到目的。
我們需要先創(chuàng)建一個工廠函數(shù) Lazy,Lazy 封裝了我們的各種目標操作 :
const Lazy = iterator => { const next = iterable.next.bind(iterable) const map = () => {} const filter = () => {} const takeWhile = () => {} return { next, map, filter, takeWhile, }
我們先實現(xiàn) map 方法,它會把每次 next 返回的值根據(jù)提供的回調函數(shù)進行修改:
const map = f => { const modifiedNext = () => { const item = next() const mappedValue = f(item.value) return { value: mappedValue, done: item.done, } } const newIter = { ...iterable, next: modifiedNext } return lazy(newIter) }
再定義 filter 方法,它會讓 next 只返回符合判斷條件的值:
const filter = predicate => { const modifiedNext = () => { while (true) { const item = next() if (predicate(item.value)) { return item } } } const newIter = { ...iterable, next: modifiedNext } return lazy(newIter) }
最后,定義 takeWhile,它會限制 next 執(zhí)行的條件,一旦條件不滿足,則停止執(zhí)行 next 并返回歷史執(zhí)行結果:
const takeWhile = predicate => { const result = [] let value = next().value while (predicate(value)) { result.push(value) value = next().value } return result }
主要的方法都定義完了,現(xiàn)在把它們合并起來:
const Lazy = iterable => { const next = iterable.next.bind(iterable) const map = f => { const modifiedNext = () => { const item = next() const mappedValue = f(item.value) return { value: mappedValue, done: item.done, } } const newIter = { ...iterable, next: modifiedNext } return lazy(newIter) } const filter = predicate => { const modifiedNext = () => { while (true) { const item = next() if (predicate(item.value)) { return item } } } const newIter = { ...iterable, next: modifiedNext } return lazy(newIter) } const takeWhile = predicate => { const result = [] let value = next().value while (predicate(value)) { result.push(value) value = next().value } return result } return Object.freeze({ map, filter, takeWhile, next, }) } const numbers = function*() { let i = 1 while (true) { yield i++ } }
現(xiàn)在用我們寫的 Lazy 和 numbers 函數(shù)來實現(xiàn)文章開頭的 Haskell 代碼:
Lazy(numbers()) .map(x => x ** 2) .filter(x => x % 2 === 1) .takeWhile(x => x < 10000) .reduce((x, y) => x + y) // => 16650
參考:
Lazy Evaluation in JavaScript with Generators, Map, Filter, and Reduce
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
javascritp添加url參數(shù)將參數(shù)加入到url中
javascritp添加url參數(shù)方法,將參數(shù)加入到url中,如果原來url中有則覆蓋,下面是示例代碼,感興趣的朋友可以參考下2014-09-09Javascript計算兩個marker之間的距離(Google Map V3)
做地圖開發(fā),最常用到的就是marker一些操作和交互。簡單介紹一下,兩個marker之間的距離計算,感興趣的朋友可以參考下哈,希望對你有所幫助2013-04-04jquery的$getjson調用并獲取遠程的JSON字符串問題
jQuery中常用getJSON來調用并獲取遠程的JSON字符串,將其轉換為JSON對象,如果成功,則執(zhí)行回調函數(shù),本文將詳細介紹,需要的朋友可以參考下2012-12-12