-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
functions-module #6143
base: main
Are you sure you want to change the base?
functions-module #6143
Conversation
Are I personally think inline arrow functions express the operation like const myNewFunc = set_arguments(myFunc, 1, undefined, 3);
// vs
const myNewFunc = (x: number) => myFunc(1, x, 3); |
Both
|
I don't understand this. Inline arrow function example should work with any parameters available in that scope. I think that has the same capability as what BTW do you suggest
Can you elaborate on more specific examples where const func = pipe(Math.abs, Math.sqrt, Math.floor);
//
const func = (num: number) => Math.floor(Math.sqrt(Math.abs(num))) |
I see Yes, of course, you can create an arrow function to inject parameters: const fnWithFirstDeps = (dataArg: Parameters<typeof fn>[2]) => fn(dependency1, dependency2, dataArg) I think it's a common and rather generic need, so having a utility that obviates the need to create a local function for it and improves readability is handy, while also encouraging a good coding practice (dependency injection). Sometimes different parts of the code inject different dependencies, having a utility encourages doing the injection locally and not creating another function or re-using the above function which entangles concerns. const fnWithFirstDeps = setArguments(fn, dependency1, dependency2,)
const extracted = {
bedrooms: pipe(
selectOne.attributes,
parseMatchingText("bedrooms"),
removeLabel,
parseNumber,
),
bathrooms: pipe(
selectOne.attributes,
h.parseMatchingText("bathrooms"),
removeLabel,
parseNumber,
),
} |
I'm also not a fan of these typings. |
(BTW while I'm personally not in favor of this, we are open for adding this package if there's enough community support to this idea.) |
Typing for a number of arguments is very common and gives a good experience. As far as I can see, it's a theoretical, not a practical issue. |
This PR and #4386, which has been open since February, have had near-zero community support. Perhaps, it'd be best to have this done as a 3rd party package and see if it evolves enough to provide a stronger argument for its addition sometime in the future. |
Honestly |
@guy-borderless Can you limit this PR to |
Of course! On it. |
Hi,
I'd like to suggest adding an std module for utilities related to functions.
related: #4386
There are common manipulations to functions implemented in numerous npm packages and toolbelts, and using them where appropriate greatly improves code quality. It would be handy to have these in deno_std.
I want to suggest for starters the following two:
These two scenarios have some typescript knowledge behind them and it makes sense to reach for a proper utility.
Both of these have many different implementations in terms of code and typings. In my opinion, the implementation most in line with the pragmatic spirit of the std should avoid functional programming jargon for simplicity and accessibility to beginners.
For discussion in this PR I have included two implementations (without tests) that I think would suit the deno_std
pipe has good typescript performance and small implementation. I'm also using it in several projects.
set_arguments has a very non-academic intuitive signature that I think suites nicely with the deno_std. This signature does not expect a data-last coding convention, so it doesn't leak to the rest of the code. I have also provided a more classic functional programming implementation of this called curry to contrast.
I can significantly simplify the typing definition of
set_arguments
if you'd like me to go ahead with preparing this PR.Since functions are an important primitive in JS, I'm sure more utilities will be added.