[英]ramda.js - how to apply a fn to subset of a collection
我正在尋找一種將函數(例如assoc
)應用到集合子集的優雅方法,但是要返回完整的集合。 有人提出了一個類似的想法,即lensWhere 。
在JS中執行此操作的一種方法是:
const applyToSubset = fn => predicate => col =>
col.filter(item => !predicate(item))
.concat(
col.filter(item => predicate(item))
.map(fn)
)
當然,這種方法對集合進行了重新排序,這並不理想。
是否有使用ramda.js或功能性慣用語的更標准方法?
謝謝您的幫助!
我可能會用Ramda這樣寫:
const applyToSubset = (fn) => (pred) => map (when (pred, fn)) console .log ( applyToSubset (triple) (isOdd) ([8, 6, 7, 5, 3, 0, 9]) ) //~> [8, 6, 21, 15, 9, 0, 27] console .log ( applyToSubset (triple) (isOdd) ({a: 8, b: 6, c: 7, d: 5, e: 3, f: 0, g: 9}) ) //~> {a: 8, b: 6, c: 21, d: 15, e: 9, f: 0, g: 27}
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script> <script> const {map, when, multiply} = R const isOdd = n => n % 2 == 1; const triple = multiply(3) </script>
盡管可能有一些無意義的方法可以做到這一點,但它已經很容易理解了。
快速回顧一下,您想對原始列表進行partition
,並在左側與謂詞不匹配,在右側與Tuple(non-matches, matches)
。 然后將fn
僅應用於匹配項。 最后再次整理列表。
const applyToSubset = R.curry((fn, predicate, list) => R.pipe( R.partition(R.complement(predicate)), R.over(R.lensIndex(1), R.map(fn)), R.flatten )(list)); const predicate = n => n >= 5; const square = n => n * n; const list = [2, 3, 4, 5, 6, 7, 8]; console.log( 'result', applyToSubset(square, predicate, list) );
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
如果您對分區不感興趣,但想保持原始數組順序...則可以嘗試以下操作:
const applyToSubset = R.converge(R.map, [ (fn, filter) => R.when(filter, fn), R.nthArg(2), ]); const predicate = n => n >= 5; const square = n => n * n; const list = [2, 3, 4, 5, 6, 7, 8]; console.log( 'result', applyToSubset(square, predicate, list) );
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
使用Array#map
仍然有一種非常簡單的方法:只需檢查每個項目,然后將整個函數f
應用於那些將每個應用於predicate
時輸出true
元素。
const applyToSubset = f => predicate => xs => xs.map (x => predicate (x) ? f (x) : x) const { assoc } = R const input = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }] const output = applyToSubset (assoc ('y') (1)) (({ x }) => x > 1) (input) console.log (output)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.