<script src="../fx.js"></script>
마플오픈소스로 es6기반, 지연성, 동시성을 더욱 풍성하게 지원해준다.
function f1(limit, list){
let acc=0;
for(const a of list){
if(a%2){
const b=a*a;
acc+=b;
if(--limit==0) break;
}
}
console.log(acc); // 35
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
function f1(limit, list){
let acc=0;
for(const a of L.filter(a=>a%2, list))
const b=a*a;
acc+=b;
if(--limit==0) break;
}
}
console.log(acc); // 35
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
function f1(limit, list){
let acc=0;
for(const a of L.map(a=>a*a, L.filter(a=>a%2, list)))
acc+=a;
if(--limit==0) break;
}
}
console.log(acc); // 35
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
function f1(limit, list){
let acc=0;
for(const a of L.take(limit, L.map(a=>a*a,
L.filter(a=>a%2, list))))
acc+=a;
}
}
console.log(acc); // 35
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
const add = (a,b) => a+b;
function f1(limit, list){
console.log(
_.reduce(add,
0,
L.take(limit,
L.map(a=>a*a,
L.filter(a=>a%2, list)))));
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
go함수를 이용해 파이프라인을 통해 순서를 뒤집어 읽기 쉽게 할 수도 있다.
const add = (a,b) => a+b;
function f1(limit, list){
_.go(
List,
L.filter(a=>a%2),
L.map(a=>a*a),
L.take(limit),
_.reduce(add),
console.log);
}
f1(3,[1,2,3,4,5,6,7,8,9,10]);
function f3(end){
let i=0;
while(i<end){
console.log(i);
i++
}
}
f3(10);
// range로
function f4(end){
_.each(console.log,
L.ranhe(end));
}
f4(10);
console.log(
_.reduce(add,
L.map(u=>u.age,
L.filter(u=>u.age<30, users))));
// 다음과 같다
console.log(
_.reduce(add,
L.filter(n=>n<30,
L.map(u=>u.age, users))));