DEV Community

Enhancing our toolbox

artydev on November 23, 2023

There is a little problems with some of our functions. const pickprop = (prop) => (obj) => obj[prop] ?? null; const map = (f) => (ar...
Collapse
 
artydev profile image
artydev • Edited

Thank you for your question Eckehard, at first sight here is what I would do:


const test = (x, i) =>  console.log(x + i) 

function _loop (n, fn, args) {
  [...Array(n)].forEach((_, i) => fn([...args].join(''), i + 1))
}

_loop(6, test, "This is a test ");

console.log("_____________________________")

const loop = curry(_loop);

loop(5, test, "this is a test ");

loop(5, test)("this is a test ");

loop(5)(test)("this is a test ");

loop(5)(test, "this is a test ");


Enter fullscreen mode Exit fullscreen mode

Demo

Collapse
 
artydev profile image
artydev • Edited

FInally you could write :


const test = (x, i) =>  console.log(x + i) 

const loop = curry((n, fn, args) =>
  [...Array(n)].forEach((_, i) => fn(args, i + 1)));

loop(5)(test, "this is a test ");

Enter fullscreen mode Exit fullscreen mode

Demo

Collapse
 
artydev profile image
artydev

Thanks, to gave me the idea to include such a loop, in my micro lib :-)

Collapse
 
efpage profile image
Eckehard • Edited

Do you know a way to build a simplified but readable loop? Instead of for( let i=0; i<5; i++) fn(args)

Ideally it would be

loop(5) { ... }

We can do things like this, but this is far from ideal:

function loop (n, fn, ...args) { 
  for (let i = 0; i < n; i++) 
    fn(...args, i + 1) 
}

function test(x, i) { console.log(x + i) }
loop(5, test, "This is loop ")
Enter fullscreen mode Exit fullscreen mode

Demo

Collapse
 
artydev profile image
artydev • Edited

Hy Eckehard

Here is a possible answer.
If I find something clearer i will post it

const test = (x, i) =>  console.log(x + i) 

function _loop (n, fn, args) {
  [...Array(n)].forEach((_, i) => fn([...args].join(''), i + 1))
}

_loop(6, test, "This is a test ");

console.log("_____________________________")

const loop = curry(_loop);

loop(5, test, "this is a test ");

loop(5, test)("this is a test ");

loop(5)(test)("this is a test ");

loop(5)(test, "this is a test ");

Enter fullscreen mode Exit fullscreen mode

Demo

Collapse
 
efpage profile image
Eckehard • Edited

Hy,

I´m not sure this works: fn([...args].join('')? And you can call it loop(), as there is no loop keyword in JS. Anyway, a nice option.

Here are 3 working versions

function loop1(n, fn, ...args) {
    for (let i = 0; i < n; i++)
        fn(...args, i + 1)
}

function loop2(n, fn, ...args) {
    if (n > 1) 
        loop2(n - 1, fn, ...args)
    fn(...args, n)
}


function _loop(n, fn, ...args) {
    [...Array(n)].forEach((_, i) => fn(...args, i + 1))
}

function test(x, y, i) { console.log(x, y * i) }

loop1(3, test, "This is loop 1:", 10); console.log("-----------")
loop2(3, test, "This is loop 2:", 10); console.log("-----------")
_loop(3, test, "This is loop 3:", 10); console.log("-----------")
Enter fullscreen mode Exit fullscreen mode

Demo

Thread Thread
 
artydev profile image
artydev

Eckehard, have you clicked on the démo link?
That works perfectly

Thread Thread
 
artydev profile image
artydev • Edited

Those functions can t be curried
I really liké the recursion way

Thread Thread
 
efpage profile image
Eckehard

The Demo works, but only with a single string parameter. Neither multiple arguments nor a numerical parameter seem to work.