<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Ramda 教程 > 原文: [http://zetcode.com/javascript/ramda/](http://zetcode.com/javascript/ramda/) Ramda 教程展示了如何使用 Ramda 庫,該庫為 JavaScript 中的高級函數編程提供了工具。 在本教程中,我們可以互換使用術語列表和數組。 ## Ramda Ramda 是 JavaScript 程序員的實用函數庫。 該庫專注于不變性和無副作用的函數。 Ramda 函數也會自動進行更新,從而只需不提供最終參數就可以從舊函數中構建新函數。 在本教程中,我們在 Node 應用中使用 Ramda。 ## 安裝 Ramda 首先,我們安裝 Ramda。 ```js $ nodejs -v v9.11.2 ``` 我們使用 Node 版本 9.11.2。 ```js $ npm init ``` 我們啟動一個新的 Node 應用。 ```js $ npm i ramda ``` 我們使用`npm i ramda`命令安裝 Ramda。 ```js const R = require('ramda'); ``` 按照慣例,該庫使用 R 字母。 ## Ramda `add()`,`subtract()`函數 `add()`函數將兩個值相加,`subtract()`函數將兩個值相減。 `add_sub.js` ```js const R = require('ramda'); console.log(R.add(2, 5)); console.log(R.subtract(2, 5)); let res = R.add(R.add(2, 5), R.subtract(2, 10)); console.log(res); ``` 該示例同時使用`add()`和`subtract()`函數。 ```js let res = R.add(R.add(2, 5), R.subtract(2, 10)); ``` 在這里,我們結合了這些函數。 ```js $ node add_sub.js 7 -3 -1 ``` 這是輸出。 ## Ramda `flip()`函數 `flip()`函數從提供的函數返回一個新函數,該函數的參數相反。 `flipfun.js` ```js const R = require('ramda'); let val = R.subtract(2, 10); console.log(val); let val2 = R.flip(R.subtract)(2, 10); console.log(val2); ``` 該示例使用`flip()`反轉`subtract()`函數的參數。 ```js $ node flipfun.js -8 8 ``` 這是輸出。 ## Ramda `call()`函數 `call()`函數在用逗號分隔的參數上調用提供的函數。 `calling.js` ```js const R = require('ramda'); let res = R.call(R.add, 1, 2); console.log(res); console.log(R.call(R.repeat, 'x')(5)); R.call(console.log, [1, 2, 3]); ``` 該示例使用`call()`函數。 ```js let res = R.call(R.add, 1, 2); ``` 我們調用`add()`函數將兩個整數相加。 ```js console.log(R.call(R.repeat, 'x')(5)); ``` 我們調用`repeat()`函數來生成五個`"x"`字母的列表。 ```js R.call(console.log, [1, 2, 3]); ``` 最后,我們使用`call()`函數輸出列表。 ```js $ node calling.js 3 [ 'x', 'x', 'x', 'x', 'x' ] [ 1, 2, 3 ] ``` 這是輸出。 ## Ramda `apply()`函數 `apply()`函數在參數列表上調用提供的函數。 `applyfun.js` ```js const R = require('ramda'); let nums = [3, 5, 7, 8, 2, 1]; let res = R.apply(Math.min, nums); console.log(res); let res2 = R.apply(Math.max, nums); console.log(res2); ``` 該示例使用`apply()`函數來計算最小值和最大值。 ```js let res = R.apply(Math.min, nums); ``` 我們在`nums`列表上調用`Math.min`函數。 我們從這些值中獲得最小值。 ```js $ node applyfun.js 1 8 ``` 我們得到最小和最大。 ## Ramda 自動柯里 柯里化是將需要多個參數的函數轉換為另一個函數的過程,當提供較少的參數時,該函數將返回一個等待其余參數的新函數。 `currying.js` ```js const R = require('ramda'); let addOneToAll = R.map(R.add(1)); let res = addOneToAll([1,2,3]); console.log(res); ``` 在示例中,我們創建了一個`addOneToAll()`函數,該函數將列表中的每個元素加 1。 ```js $ node currying.js [ 2, 3, 4 ] ``` 這是輸出。 ## Ramda `head()`,`tail()`,`init()`,`last()`函數 `head()`返回給定列表或字符串的第一個元素。 `tail()`返回給定列表或字符串的除第一個元素外的所有元素。 `init()`返回給定列表或字符串的最后一個元素以外的所有元素。 `last()`返回給定列表或字符串的最后一個元素。 `head_tail.js` ```js const R = require('ramda'); let nums = [2, 4, 6, 8, 10]; console.log(R.head(nums)); console.log(R.tail(nums)); console.log(R.init(nums)); console.log(R.last(nums)); ``` 該示例在值數組上使用`head()`,`tail()`,`init()`和`last()`函數。 ```js $ node head_tail.js 2 [ 4, 6, 8, 10 ] [ 2, 4, 6, 8 ] 10 ``` 這是輸出。 ## Ramda `length()`函數 `length()`函數返回列表中的元素數。 `lengthfun.js` ```js const R = require('ramda'); let nums = [1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 7]; let n1 = R.length(nums); console.log(n1); let n2 = R.length(R.uniq(nums)); console.log(n2); ``` 在示例中,我們計算列表中的元素數和列表中的唯一元素數。 ```js $ node lengthfn.js 12 7 ``` 列表中有十二個元素,列表中有七個唯一元素。 ## Ramda `prop()`函數 `prop()`函數返回對象的指定屬性(如果存在)。 `propfun.js` ```js const R = require('ramda'); console.log(R.prop('name', { name: 'John', age: 25 })); console.log(R.prop('age', { name: 'John', age: 25 })); ``` 使用`prop()`函數,我們可以獲得`name`和`age`屬性的值。 ```js $ node propfun.js John 25 ``` 這是輸出。 ## Ramda `pluck()`函數 `pluck()`函數通過從提供的列表中的所有對象上拔出指定的屬性來返回新列表。 `plucking.js` ```js const R = require('ramda'); const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 } ]; console.log(R.pluck('age', users)); console.log(R.pluck('name', users)); ``` 通過`pluck()`函數,我們獲得`name`和`age`屬性,并形成兩個新列表。 ```js $ node plucking.js [ 25, 51, 43, 81, 43, 76, 47, 72 ] [ 'John', 'Lenny', 'Andrew', 'Peter', 'Anna', 'Albert', 'Adam', 'Robert' ] ``` 在下面的示例中,我們將使用形成的列表。 `plucking2.js` ```js const R = require('ramda'); const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 } ]; let maxAge = R.apply(Math.max, R.pluck('age', users)); // let maxAge = Math.max(... R.pluck('age', users)); console.log(`The oldest person is ${maxAge} years old.`); ``` 在示例中,我們找出一個人的最大年齡。 ```js let maxAge = R.apply(Math.max, R.pluck('age', users)); ``` 通過在年齡列表上調用`Math.max()`函數,我們可以獲得最老的年齡。 ```js // let maxAge = Math.max(... R.pluck('age', users)); ``` 另一種帶注釋的解決方案使用擴展運算符代替`apply()`函數。 ```js $ node plucking2.js The oldest person is 81 years old. ``` 這是輸出。 ## Ramda 拆分列表 使用`splitEvery()`函數,我們可以將列表分成指定長度的塊。 `chunks.js` ```js const R = require('ramda'); let nums = [1, 2, 3, 4, 5, 6]; console.log(R.splitEvery(2, nums)); console.log(R.splitEvery(3, nums)); ``` 在示例中,我們將數組分為 2 個元素和 3 個元素的塊。 ```js $ node chunks.js [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ``` 這是輸出。 ## Ramda `contains()`函數 如果指定的值在列表中,則`contains()`函數返回`true`。 `containsfun.js` ```js const R = require('ramda'); const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 } ]; let isJohn = R.contains('John', R.pluck('name', users)); if (isJohn) { console.log('There is John in the list'); } ``` 在示例中,我們檢查指定的用戶是否在列表中。 ```js let isJohn = R.contains('John', R.pluck('name', users)); ``` 首先,我們使用`pluck()`函數從`name`屬性中形成一個列表。 然后我們用`contains()`檢查'John'是否在列表中。 ```js $ node containsfun.js There is John in the list ``` 這是輸出。 ## Ramda `range()`函數 `range()`函數返回從起始值(包含)到結束值(不含)的數字列表。 `rangefun.js` ```js const R = require('ramda'); console.log(R.range(1, 10)); let vals = R.range(2, 12); vals.forEach(x => console.log(x)); ``` 該示例顯示了如何使用`range()`函數。 ```js console.log(R.range(1, 10)); ``` 在這一行中,我們創建一個`1..9`整數列表。 我們將它們打印到控制臺。 ```js let vals = R.range(2, 12); vals.forEach(x => console.log(x)); ``` 在這里,我們生成一個`2..11`值的列表。 我們使用`forEach()`函數瀏覽列表。 ```js $ node rangefun.js [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] 2 3 4 5 6 7 8 9 10 11 ``` 這是輸出。 ## Ramda `sum()`函數 `sum()`函數對列表的所有元素求和。 `summation.js` ```js const R = require('ramda'); let nums = [2, 4, 6, 8, 10]; console.log(R.sum(nums)); console.log(R.sum(R.range(1, 11))); ``` 該示例使用`sum()`函數對整數值求和。 ```js let nums = [2, 4, 6, 8, 10]; console.log(R.sum(nums)); ``` 在這里,我們對`nums`數組的值求和。 ```js console.log(R.sum(R.range(1, 11))); ``` 在這一行中,我們對`range()`函數生成的列表的值求和。 ```js $ node summation.js 30 55 ``` 這是輸出。 ## Ramda `product()`函數 `product()`函數將列表的所有元素相乘。 `productfun.js` ```js const R = require('ramda'); let nums = [2, 4, 6, 8, 10]; console.log(R.product(nums)); ``` 該示例計算整數列表的乘積。 ```js $ node productfun.js 3840 ``` 這是輸出。 ## Ramda `sort()`,`reverse()`函數 `sort()`函數返回列表的副本,該列表根據比較器函數排序。 比較器函數一次接受兩個值,如果第一個值較小,則返回一個負數;如果較大,則返回一個正數;如果相等,則返回零。 `reverse()`函數以相反的順序返回帶有元素或字符的新列表或字符串。 `sort_reverse.js` ```js const R = require('ramda'); let nums = [3, 1, 4, 2, 8, 5, 6]; console.log('sorting:') // sort ascending console.log(R.sort((x, y) => x - y , nums)); // sort descending console.log(R.sort((x, y) => y - x , nums)); console.log('reversing:') // reversing console.log(R.reverse(nums)); console.log(R.reverse('forest')); ``` 該示例按升序和降序對整數進行排序,并反轉整數和字符串。 ```js $ node sort_reverse.js sorting: [ 1, 2, 3, 4, 5, 6, 8 ] [ 8, 6, 5, 4, 3, 2, 1 ] reversing: [ 6, 5, 8, 2, 4, 1, 3 ] tserof ``` 這是輸出。 我們還可以使用內置的`R.lt`和`R.gt`比較器。 `sort_comp.js` ```js const R = require('ramda'); let nums = [3, 1, 4, 2, 8, 5, 6]; console.log('sorting:') // sort ascending console.log(R.sort(R.comparator(R.lt), nums)); // sort descending console.log(R.sort(R.comparator(R.gt), nums)); ``` 該示例按升序和降序對整數進行排序。 ## Ramda `sortBy`函數 `sortBy()`函數根據提供的函數對列表進行排序。 `sorting_objects.js` ```js const R = require('ramda'); const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 } ]; console.log('Sorted by age:'); let sortedByAge = R.sortBy(R.prop('age'), users); console.log(sortedByAge); console.log('Sorted by name:'); let sortedByName = R.sortBy(R.prop('name'), users); console.log(sortedByName); ``` 在示例中,我們按`age`和`name`屬性以升序對用戶列表進行排序。 ```js $ node sorting_objects.js Sorted by age: [ { name: 'John', age: 25 }, { name: 'Andrew', age: 43 }, { name: 'Anna', age: 43 }, { name: 'Adam', age: 47 }, { name: 'Lenny', age: 51 }, { name: 'Robert', age: 72 }, { name: 'Albert', age: 76 }, { name: 'Peter', age: 81 } ] Sorted by name: [ { name: 'Adam', age: 47 }, { name: 'Albert', age: 76 }, { name: 'Andrew', age: 43 }, { name: 'Anna', age: 43 }, { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Peter', age: 81 }, { name: 'Robert', age: 72 } ] ``` 這是輸出。 ## Ramda `find`,`findLast`函數 `find()`函數返回與謂詞匹配的列表的第一個元素;如果不匹配,則返回`undefined`。 `findLast()`函數返回列表中與謂詞匹配的最后一個元素,如果沒有元素匹配,則返回`undefined`。 `finding.js` ```js const R = require('ramda'); const isPositive = x => x > 0; let values = [-1, 0, -4, 5, 6, -1, 9, -2] let val = R.find(isPositive, values); console.log(val); let val2 = R.findLast(isPositive, values); console.log(val2); ``` 在示例中,我們找到第一個和最后一個正值。 ```js const isPositive = x => x > 0; ``` `isPositive()`是一個謂詞函數,對于大于零的值返回`true`。 ```js let val = R.find(isPositive, values); ``` 使用`find()`,我們發現第一個出現的正數。 ```js let val2 = R.findLast(isPositive, values); ``` 使用`findLast()`,我們找到最后一個正數。 ```js $ node finding.js 5 9 ``` 第一個正值為 5,最后為 9。 在下面的示例中,我們在對象列表上使用`find()`函數。 `finding2.js` ```js const R = require('ramda'); const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 }, { name: 'Robert', age: 26 }, ]; console.log(R.find(R.propEq('name', 'Robert'))(users)); console.log(R.find(R.propEq('age', 81))(users)); ``` 通過`find()`和`propEq()`函數的組合,我們尋找具有指定屬性的用戶。 ```js console.log(R.find(R.propEq('name', 'Robert'))(users)); ``` 在這里,我們尋找一個名叫羅伯特的人。 有兩個羅伯茨(Roberts),并且第一場比賽被返回。 ```js $ node finding2.js { name: 'Robert', age: 72 } { name: 'Peter', age: 81 } ``` 這是輸出。 ## Ramda `map()`函數 `map()`函數將提供的函數映射到每個容器的值。 `mapping.js` ```js const R = require('ramda'); nums = [2, 4, 5, 6, 7, 8, 9]; let res = R.map(x => x * 2, nums); console.log(res); const isEven = x => x % 2 === 0; let res2 = R.map(isEven, nums); console.log(res2); let repeated = R.map(R.call, R.repeat(Math.random, 5)); console.log(repeated); ``` 該示例演示了`map()`的用法。 ```js let res = R.map(x => x * 2, nums); ``` 我們將匿名函數映射到整數列表上。 生成一個新列表,其中每個值都乘以 2。 ```js const isEven = x => x % 2 === 0; let res2 = R.map(isEven, nums); ``` 在這里,我們在每個元素上應用`isEven()`函數。 `res2`是正確和錯誤值的列表。 如果我們只想選擇事件號,則可以使用`filter()`函數。 ```js let repeated = R.map(R.call, R.repeat(Math.random, 5)); ``` 在第三種情況下,我們生成五個隨機值的列表。 ```js $ node mapping.js [ 4, 8, 10, 12, 14, 16, 18 ] [ true, true, false, true, false, true, false ] [ 0.22019193556521865, 0.415950206671615, 0.8770997167119405, 0.23393806619678315, 0.8181008680173825 ] ``` 這是輸出。 ## Ramda `filter()`函數 `filter()`函數根據提供的謂詞函數過濾可過濾對象(例如列表或普通對象)。 (謂詞是一個返回布爾值的函數)。 `filtering.js` ```js const R = require('ramda'); nums = [-3, -1, 0, 2, 3, 4, 5, 6, 7] let res = R.filter(x => x > 0, nums); console.log(res); let res2 = R.filter(x => x < 0, nums); console.log(res2); const isEven = x => x % 2 === 0; let filtered = R.filter(isEven, nums); console.log(filtered); ``` 在示例中,我們有一個整數值列表。 我們使用`filter()`函數過濾出正,負和偶數值。 ```js let res = R.filter(x => x > 0, nums); ``` 此行中的`filter()`函數采用匿名函數,該函數對于所有大于零的值都返回`true`。 然后將謂詞應用于列表的每個元素。 這樣,我們形成一個僅包含正值的新列表。 ```js $ node filtering.js [ 2, 3, 4, 5, 6, 7 ] [ -3, -1 ] [ 0, 2, 4, 6 ] ``` 這是輸出。 在下面的示例中,我們將`filter()`函數應用于用戶列表。 `filtering2.js` ```js const R = require('ramda'); // senior is a person who is 70+ const users = [ { name: 'John', age: 25 }, { name: 'Lenny', age: 51 }, { name: 'Andrew', age: 43 }, { name: 'Peter', age: 81 }, { name: 'Anna', age: 43 }, { name: 'Albert', age: 76 }, { name: 'Adam', age: 47 }, { name: 'Robert', age: 72 } ]; console.log(R.filter(user => user.age >= 70, users)); ``` 該示例將高級用戶過濾掉。 我們將年長者定義為 70 歲以上的人。 ```js $ node filtering2.js [ { name: 'Peter', age: 81 }, { name: 'Albert', age: 76 }, { name: 'Robert', age: 72 } ] ``` 我們有三個高級用戶。 ## `reject()`函數 `reject()`是`filter()`的補充。 它排除謂詞為其返回`true`的可過濾元素。 `rejecting.js` ```js const R = require('ramda'); const users = [ { name: 'John', city: 'London', born: '2001-04-01' }, { name: 'Lenny', city: 'New York', born: '1997-12-11' }, { name: 'Andrew', city: 'Boston', born: '1987-02-22' }, { name: 'Peter', city: 'Prague', born: '1936-03-24' }, { name: 'Anna', city: 'Bratislava', born: '1973-11-12' }, { name: 'Albert', city: 'Bratislava', born: '1940-18-19' }, { name: 'Adam', city: 'Trnava', born:'1983-12-01' }, { name: 'Robert', city: 'Bratislava', born: '1935-05-15' }, { name: 'Robert', city: 'Prague', born:'1998-03-14' } ]; let res = R.reject(R.propEq('city', 'Bratislava'))(users); console.log(res); let res2 = R.filter(R.propEq('city', 'Bratislava'))(users); console.log(res2); ``` 在示例中,我們使用`reject()`函數形成不包含布拉迪斯拉發城市的對象的新列表。 我們還使用`filter()`函數形成包含布拉迪斯拉發城市的對象的新列表。 ```js $ node rejecting.js [ { name: 'John', city: 'London', born: '2001-04-01' }, { name: 'Lenny', city: 'New York', born: '1997-12-11' }, { name: 'Andrew', city: 'Boston', born: '1987-02-22' }, { name: 'Peter', city: 'Prague', born: '1936-03-24' }, { name: 'Adam', city: 'Trnava', born: '1983-12-01' }, { name: 'Robert', city: 'Prague', born: '1998-03-14' } ] [ { name: 'Anna', city: 'Bratislava', born: '1973-11-12' }, { name: 'Albert', city: 'Bratislava', born: '1940-18-19' }, { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ] ``` 這是輸出。 第一個列表包含不包含布拉迪斯拉發城市屬性的所有對象。第二個列表僅包含具有布拉迪斯拉發城市屬性的對象。 ## `partition()`函數 `partition()`函數將`filter`分為兩個獨立的對象:一個滿足謂詞,另一個不滿足。 `partitionfun.js` ```js const R = require('ramda'); let nums = [4, -5, 3, 2, -1, 7, -6, 8, 9]; let [ neg, pos ] = R.partition(e => e < 0, nums); console.log(neg); console.log(pos); ``` 使用`partition()`函數,我們將整數列表分為兩個單獨的列表:負數和正數。 ```js $ node partitionfun.js [ -5, -1, -6 ] [ 4, 3, 2, 7, 8, 9 ] ``` 第一個列表包含負值,第二個列表包含正值。 ## Ramda `groupBy`函數 `groupBy()`函數基于在每個元素上調用`String`返回函數并根據返回的值對結果進行分組的結果,將列表分為存儲在對象中的子列表。 `grouping.js` ```js const R = require('ramda'); let students = [ { name: 'Adam', score: 84 }, { name: 'Eddy', score: 58 }, { name: 'Peter', score: 69 }, { name: 'Roman', score: 93 }, { name: 'Jane', score: 56 }, { name: 'Lucy', score: 76 }, { name: 'Jack', score: 88 }, ]; var groupByGrade = R.groupBy((student) => { let score = student.score; return score < 65 ? 'F' : score < 70 ? 'D' : score < 80 ? 'C' : score < 90 ? 'B' : 'A'; }); let grouped = groupByGrade(students); console.log('Student(s) having A grade:'); console.log(grouped['A']); console.log('Student(s) having B grade:'); console.log(grouped['B']); console.log('Student(s) having C grade:'); console.log(grouped['D']); console.log('Student(s) having D grade:'); console.log(grouped['D']); console.log('Student(s) having F grade:'); console.log(grouped['F']); ``` 在此示例中,我們將學生按分數分組到成績子列表中。 ```js $ node grouping.js Student(s) having A grade: [ { name: 'Roman', score: 93 } ] Student(s) having B grade: [ { name: 'Adam', score: 84 }, { name: 'Jack', score: 88 } ] Student(s) having C grade: [ { name: 'Peter', score: 69 } ] Student(s) having D grade: [ { name: 'Peter', score: 69 } ] Student(s) having F grade: [ { name: 'Eddy', score: 58 }, { name: 'Jane', score: 56 } ] ``` 這是輸出。 ## Ramda `reduce()`函數 `reduce()`函數將列表值聚合為一個值。 它對一個累加器和列表中的每個元素(從左到右)應用一個函數,以將其減少為單個值。 `reducefun.js` ```js const R = require('ramda'); let nums = [2, 3, 4, 5, 6, 7]; let sum = R.reduce((x, y) => x+y, 0, nums); console.log(sum); let product = R.reduce((x, y) => x*y, 1, nums); console.log(product); ``` 該示例使用`reduce()`函數來計算整數列表的總和。 ```js let sum = R.reduce((x, y) => x+y, 0, nums); ``` 在這一行中,我們計算值的總和。 第一個參數是應用于值的函數。 第二個是累加器,它是起始值。 第三是包含值的列表。 ```js let product = R.reduce((x, y) => x*y, 1, nums); ``` 在這里,我們計算列表值的乘積。 ```js $ node reducefun.js 27 5040 ``` 這是輸出。 下面的示例計算表達式:`1*2 + 3*4 + 5*6`。 `reduce_fun2.js` ```js const R = require('ramda'); let nums = [1, 2, 3, 4, 5, 6]; let ret = R.reduce((acc, x) => acc + x[0] * x[1], 0, R.splitEvery(2, nums)); console.log(ret); ``` 在示例中,我們將列表分成幾對,并對這些對應用歸約操作。 ```js $ node reduce_fun2.js 44 ``` 這是輸出。 ## `where()`函數 `where()`函數允許在對象上創建復雜的查詢。 `wherefun.js` ```js const R = require('ramda'); const moment = require('moment'); const users = [ { name: 'John', city: 'London', born: '2001-04-01' }, { name: 'Lenny', city: 'New York', born: '1997-12-11' }, { name: 'Andrew', city: 'Boston', born: '1987-02-22' }, { name: 'Peter', city: 'Prague', born: '1936-03-24' }, { name: 'Anna', city: 'Bratislava', born: '1973-11-18' }, { name: 'Albert', city: 'Bratislava', born: '1940-12-11' }, { name: 'Adam', city: 'Trnava', born: '1983-12-01' }, { name: 'Robert', city: 'Bratislava', born: '1935-05-15' }, { name: 'Robert', city: 'Prague', born: '1998-03-14' } ]; let res1 = R.filter(R.where({ city: R.equals('Bratislava') }))(users); console.log(res1); let res2 = R.filter(R.where({ city: R.equals('Bratislava'), name: R.startsWith('A') }))(users); console.log(res2); let res3 = R.filter(R.where({ born: (dt) => getAge(dt) > 40}))(users); console.log(res3); function getAge(dt) { return moment.duration(moment() - moment(dt, 'YYYY-MM-DD', true)).years(); } ``` 在示例中,我們在用戶列表上使用`where()`創建查詢。 ```js let res1 = R.filter(R.where({ city: R.equals('Bratislava') }))(users); ``` 在這里,我們找出居住在布拉迪斯拉發的所有用戶。 ```js let res2 = R.filter(R.where({ city: R.equals('Bratislava'), name: R.startsWith('A') }))(users); ``` 在此代碼中,我們找到了居住在布拉迪斯拉發的用戶,其名稱以`"A"`開頭。 ```js let res3 = R.filter(R.where({ born: (dt) => getAge(dt) > 40}))(users); ``` 最后,我們找出 40 歲以上的用戶。 ```js function getAge(dt) { return moment.duration(moment() - moment(dt, 'YYYY-MM-DD', true)).years(); } ``` 要從提供的出生日期算起年齡,我們使用矩模塊。 ```js $ node where_fun.js [ { name: 'Anna', city: 'Bratislava', born: '1973-11-18' }, { name: 'Albert', city: 'Bratislava', born: '1940-12-11' }, { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ] [ { name: 'Anna', city: 'Bratislava', born: '1973-11-18' }, { name: 'Albert', city: 'Bratislava', born: '1940-12-11' } ] [ { name: 'Peter', city: 'Prague', born: '1936-03-24' }, { name: 'Anna', city: 'Bratislava', born: '1973-11-18' }, { name: 'Albert', city: 'Bratislava', born: '1940-12-11' }, { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ] ``` 這是輸出。 在本教程中,我們使用了`Ramda`庫。 您可能也會對以下相關教程感興趣: [Lodash 教程](/javascript/lodash/), [Collect.js 教程](/javascript/collectjs/), [JavaScript 數組](/javascript/arrays/), [Moment.js 教程](/javascript/momentjs/) 。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看