-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Helper for creating a new function via partial application #36
Comments
I agree that this would be marvellous! I'll give the matter some thought. |
It would be nice to support aliases as well. Sanctuary, for example, will soon export both |
👍 on this all the way, I have been re-writing a LOT of type signatures to the point that I end up resorting to things like const baseSig = [$.String, $.String]
// then later...
def('someFunction', {}, [...baseConfig, $.Boolean], implementationStuff) |
I would now write the original code as follows: // add :: Number -> Number -> Number
const add = x => y => x + y;
exports.add =
def ('add')
({})
([$.Number, $.Number, $.Number])
(add);
exports.incr =
def ('incr')
({})
([$.Number, $.Number])
(add (1)); The implementation is shared yet type checking only occurs at the module boundary. |
I ran into a minor issue with the error messages generated by sanctuary def when you export a function that has already been partially applied.
For example, if we have a library that exports an
incr
function via a partial application ofadd
like so:And a separate library imports it and calls
incr(1, 2)
, sanctuary-def will throw aTypeError
complaining thatadd
expected two arguments but got three. This can be confusing because 1) theadd
function was never called directly and 2) theincr
function expects only one argument, but the error message complains about a function that expects two arguments.We could rewrite our
incr
like so:For this simple case it's not so bad, but I was recently working on a library with 10-12 functions that were exported this way, each with different types of arguments. The boilerplate can become annoying and gets hard to maintain if you want refactor a function signature later (because now you have to change it in two places).
It would be awesome if sanctuary-def provided a helper function for defining a new function that is implemented as a partial application of another. It might look something like this:
redef
takes a new name, a function which is returned by a partial application of some other function defined withdef
, and returns a new, curried function which accepts any arguments not already provided.So now if you called
incr(1, 2)
, the error message would explain thatincr
expects one argument but received two.I'm not sure exactly what to call this helper function or if sanctuary-def has enough information to do the introspection that would be required here, but it would be extremely nice to have.
The text was updated successfully, but these errors were encountered: