froebel icon indicating copy to clipboard operation
froebel copied to clipboard

Utility Request: Unary

Open lingdocs opened this issue 2 years ago • 2 comments

It would be great to have a unary utility. Like this in plain JS but not sure how to implement it in TS:

const unary = (f) => {
  return f.length === 1 ? f : (args) => f(args)
}

That way we could do things like:

['1', '2', '3'].map(unary(parseInt))

Example this discussion/explanation about why the unary utility is useful.

My apologies if this already exists in here under a different name. This is a fantastic library! 👏

lingdocs avatar Sep 05 '22 13:09 lingdocs

Thanks for the suggestion. I think a unary function would make a great addition to the library!

I'll do some research on what common naming conventions for this are in other libraries and languages. Maybe it's also worthwhile to add a more generic function that changes the number of arguments to any arbitrary number and have unary be a special case of that function.

Will add the utility some time in the coming days. Unless, that is, you want to have a stab at adding it yourself.

MathisBullinger avatar Sep 08 '22 10:09 MathisBullinger

Thanks that would be great! I've tried to do a typed version of the js example above but I'm not able to get it working, sorry. Looking forward to seeing how you implement it and learning from your TypeScript skills! 🙌

lingdocs avatar Sep 11 '22 22:09 lingdocs