[英]Make functions in map point-free
我有這樣的情況:
const fun = (x,y) => fun1(fun2(x,y), fun3(x,y), fun4(x,y));
const a = [ 1, 2, 3, 4 ].map(fun);
我到處都看到x
, y
。 有什么辦法可以使這一點變得毫無意義嗎?
更新:
現實生活中的例子:
data.map((gist, index) =>
ifElse(
checkForSingleFile(gist, index),
toGistFile(gist, index),
toGistFolder(gist, index)
)
);
我正在使用 Ramda 的IfElse
您可以將 arguments 傳遞給ifElse
,因為它返回 apt function ,它將具有toGistFile
或toGistFolder
的 arguments ,在相同的情況下:
const { ifElse } = R; const data = [1, 2, 3, 4]; const checkForSingleFile = () => { return Math.random() > 0.5; } const toGistFile = (gist, index) => { console.log('file'); //return function(gist, index) {}; } const toGistFolder = (gist, index) => { console.log('folder'); //return function(gist, index) {}; } data.map( ifElse( checkForSingleFile, toGistFile, toGistFolder ) );
<script src="//cdn.jsdelivr.net/npm/ramda@0.25/dist/ramda.min.js"></script>
好的,第一個片段向您展示了如何使用 Vanilla JS 進行操作:
const compose = (points, fns) => { let output = points; points.map( (p,i) => { fns.map( fn => output[i] = fn(output[i]) ); }); return output; } const funA = ({x,y}) => ({x: x*2, y: y*2}); const funB = ({x,y}) => ({x: x*2, y: y*2}); const sqrt = ({x,y}) => ({x: Math.sqrt(x), y: Math.sqrt(y)}); const points = [{x:1, y:1}, {x:2, y:2}]; const res = compose(points, [funA, funB, sqrt]); // sqrt(funB(funA(points))) = funA ¤ funB ¤ sqrt console.log(res);
現在有了一個專為 function 組成的庫(ramda.js):
const funA = ({x,y}) => ({x: x*2, y: y*2}); const funB = ({x,y}) => ({x: x*2, y: y*2}); const sqrt = ({x,y}) => ({x: Math.sqrt(x), y: Math.sqrt(y)}); const points = [{x:1, y:1}, {x:2, y:2}]; const curryFun = R.compose(sqrt, funA, funB); const resCurry = points.map( curryFun ); console.log(resCurry);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js" integrity="sha256-43x9r7YRdZpZqTjDT5E0Vfrxn1ajIZLyYWtfAXsargA=" crossorigin="anonymous"></script>
在這里,我給出了一個關於二維函數的例子,我們有一個 abscicca 的 linspace,我們想要 plot 組合的 fn:
// declare fn const lin = x => x * 1.2 + 0.9; const sin = x => Math.sin(x); const cos = x => Math.cos(x); // set the linspace const linspace = [...new Array(50)].map((val,i) => i/10 ); // set the composed fn const composed = R.compose(lin, sin, cos); // we can write: composed = R.compose(lin, Math.sin, Math.cos) // graph var ctx = document.getElementById("myChart"); var data = { labels: linspace, datasets: [{ label: "lin", function: lin, borderColor: "rgba(75, 192, 192, 1)", data: [], fill: false }, { label: "sin", function: sin, borderColor: "rgba(153, 102, 255, 1)", data: [], fill: false }, { label: "cos", function: cos, borderColor: "rgba(255, 206, 86, 1)", data: [], fill: false }, { label: "composed", function: composed, borderColor: "rgba(255, 106, 86, 1)", data: [], fill: false } ] }; Chart.pluginService.register({ beforeInit: function(chart) { var data = chart.config.data; for (var i = 0; i < data.datasets.length; i++) { for (var j = 0; j < data.labels.length; j++) { var fct = data.datasets[i].function, x = data.labels[j], y = fct(x); data.datasets[i].data.push(y); } } } }); var myBarChart = new Chart(ctx, { type: 'line', data: data, options: { scales: { yAxes: [{ ticks: { beginAtZero:true } }] } } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js" integrity="sha256-43x9r7YRdZpZqTjDT5E0Vfrxn1ajIZLyYWtfAXsargA=" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" integrity="sha256-Uv9BNBucvCPipKQ2NS9wYpJmi8DTOEfTA/nH2aoJALw=" crossorigin="anonymous"></script> <canvas id="myChart"></canvas>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.