There is a little problems with some of our functions.
const pickprop = (prop) => (obj) => obj[prop] ?? null; const map = (f) => (arr) => arr.map(f); const filter = (f) => (arr) => arr.filter(f);
Given :
let pets = [ { petname : "Bill", breed : "poodle", weight: 12, owner : { ownername : "Paul", contribution : { amount : 32, payed : false }, address : { city : "Paris" } } }, { petname : "Maya", race : "pointer", weight: 27, owner : { ownername : "Henri", contribution : { amount : 12, payed : true }, address : { city : "London" } } }, { petname : "Ooper", race : "setter", weight: 20, owner : { ownername : "Nicolas", contribution : { amount : 12, payed : true }, address : { city : "London" } } } ]
We can only pass one argument at a time. So we can not write :
const petNameOne = pickprop("petname", pets[0])
That's because pickprop is not really curried
So, let's "spice" them :
const curry = function (fn) { const curried = (...args) => { if (args.length >= fn.length) { return fn.apply({}, args) } else { return (...vargs) => curried.apply({}, args.concat(vargs)) } } return curried } const compose = (...fns) => arg => fns.reduceRight((acc, f) => f(acc), arg); const pipe = (...fns) => arg => fns.reduce((acc, f) => f(acc), arg); const asyncpipe = (...fns) => arg => fns.reduce((p, fn) => p.then(fn), Promise.resolve(arg)); const pickprop = curry((prop, obj) => obj[prop] ?? null); const map = curry((f, arr) => arr.map(f)); const filter = curry((f, arr) => arr.filter(f));
We can now call our function pickprop using two syntax :
let petNameOne = pickprop("petname", pets[0]) console.log(petNameOne) petNameOne = pickprop("petname")(pets[0]) console.log(petNameOne)
Great :-)
You can play with the demo here : Demo
Top comments (15)
Thank you for your question Eckehard, at first sight here is what I would do:
Demo
FInally you could write :
Demo
Thanks, to gave me the idea to include such a loop, in my micro lib :-)
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:
Demo
Hy Eckehard
Here is a possible answer.
If I find something clearer i will post it
Demo
Hy,
I´m not sure this works:
fn([...args].join('')
? And you can call itloop()
, as there is no loop keyword in JS. Anyway, a nice option.Here are 3 working versions
Demo
Eckehard, have you clicked on the démo link?
That works perfectly
Those functions can t be curried
I really liké the recursion way
The Demo works, but only with a single string parameter. Neither multiple arguments nor a numerical parameter seem to work.
And this version ? :
Thank you for the suggestions.
Demo
In my understanding, loop should be a shortcut for "for". So it should not be too specific. The most common use would be:
I agree with this seems the most elegant way.
And if one wish all sort of variations is possible :
Demo
Cool, just don´t forget to call
fn(...args, n)
, otherwise you cannot access the index from within your function.And you should decide, if the index counts from from 1 to n (which is intuitive) or from 0 to n-1, which makes it easier to deal with array indices.
Happy coding!
:-) Thank you Eckehard
Hy Eckehard,
I published a answer
Regards