おそらくRamdaを使って、もっと機能的な方法がありますか?Ramdaでサブアレイを実行するための機能的方法
var time = 100;
sequenceInstruments.forEach(function(instrument){
if(instrument.on)
{
playInstrument(time, instrument.duration);
}
})
おそらくRamdaを使って、もっと機能的な方法がありますか?Ramdaでサブアレイを実行するための機能的方法
var time = 100;
sequenceInstruments.forEach(function(instrument){
if(instrument.on)
{
playInstrument(time, instrument.duration);
}
})
Ramdaの機能をポイントフリーの方法で利用するだけで、この例のようになります。
const play = R.forEach(R.when(R.prop('on'),
R.compose(R.partial(playInstrument, [time]),
R.prop('duration'))))
play(sequenceInstruments)
私は、多くの場合、無名関数の使用は、おそらく、コードを読みやすくし、より明確に意図を伝えることができた場合、少しそれをバックにダイヤルする方が良いことができると思いますが。
const play = R.forEach(R.when(R.prop('on'), i => playInstrument(time, i.duration)))
play(sequenceInstruments)
私はポイント-FULソリューションを使用すると、ポイント・無料版の開発に興味を持っている場合は、思い付くしそうだ任意のポイント、無料版よりも理解しやすいというスコット・クリストファーに同意する一方で、 time
を最終関数のパラメータにしたい場合、Ramdaは役立つ関数、useWith
を提供します。 (関連する機能は、わずかに異なる状況のために有用converge
は、もあります。)これはあなたのplayInstrument
機能がカレーさによって異なります。
const play = R.useWith(R.forEach, [
playInstrument,
R.compose(R.pluck('duration'), R.filter(R.prop('on')))
]);
play(100, sequenceInstruments);
あなたはRamda REPLにこの動作を確認することができます。
私は@ftorに同意します:filter
は、完全に読み取り可能なポイントフリーコードにつながるより線形に構成することができます。
const play = pipe(
filter(prop('on')), // take only the instruments that are 'on'
map(prop('duration')), // take the duration of each of those
forEach(playInstrument(100)) // play'm all
);
play(sequenceInstruments);
これはすでにplayInstruments
がカレーされていると仮定しています。
lodash/fp
の速記であなたもこれを行うことができます:
const play = pipe(
filter('on'),
map('duration'),
forEach(playInstrument(100))
);
素敵なシンプルなパイプラインのためのプラス、恐ろしいオーバーロードされた "ショートハンド"のためのマイナス。 –
@ScottSauyet私はあなたのPOVを理解しています。 Ramdaは "純粋主義者"の機能的アプローチを持っていますが、Lodashをより "実用的"と呼ぶかもしれません。私はあまりにも簡単に怖がらない;) – RIAstar
おそらく、それはjQueryが何年も主張してきたものだ。どういうわけか、それはすべての体重の下で沈んでいるようです。それにかかわらず、これは素晴らしい解決策です! –
あなたはArray.prototype.filter' 'と' else'枝せずに文if' '避けることができることに注意してください。単に 'map'を' filter'で構成してください。中間配列も避けたい場合は、それらのトランスデューサ実装を使用してください。 – ftor
"サブアレイを実行する"? – naomik