-
Notifications
You must be signed in to change notification settings - Fork 265
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
Idea: J-style forks #544
Comments
Neat! Thanks for sharing @techieji. I can see value in this. I'd like to play around with this a little more to try to come up with a good motivating example. Can you share other examples of when you have used this? |
I personally used this function in a programming language I'm writing to turn def var(self, tree):
return self.env[childn(0, tree)] into the point-free var = fork(dict.__getitem__, attrgetter('env'), childn(0)) # childn is curried |
!!! I just realized - this is (a special case of) The more general pattern here is the combination of partial application and function composition concepts; a function composition tree rather than a line; partial function composition where each composition only applies to some arguments! Or in code example instead of words, the generalization is (actually this is a simplified version):
So we can see how |
So I oversimplified the above description of The full variation of the concept I have in mind (which might be overly complex to actually implement), is to inspect the signatures of all the composed functions, and use that to determine which arguments the partially composed functions "consume" and/or "share". The logic for that variant is pretty convoluted to describe, but the general gist is: how might we generalize it if we had functions that wanted to preprocess multiple arguments at once, or functions that wanted to preprocess specific named arguments, and how might we handle variadic arguments, and so on and so on? But even for that version of the concept, |
Note also that If we have def always_returns(value):
def returner():
return value
return returner then
and
So there is some larger abstract conceptual shape here, and |
So I would discourage naming it "fork" because that name is very general, vague, to me counter-intuitive (it only makes sense to me after connecting it to my And I would somewhat discourage having a function that only partially composes positional arguments, because that's kinda like having a But I do think there is a very, very good idea in this direction. |
I recently wrote a rough Python implementation of J-style forks which I ended up using a lot, and I wanted to see whether such a function belonged in toolz:
The most similar function currently in toolz is
juxt
, but while (I think)juxt
aims to contrast different functions,fork
preprocesses the arguments to the main function. It also has precedent in J (but whether you can use J for precedent is up for debate).If this belongs in
toolz
, I'd love to submit a PR for it.The text was updated successfully, but these errors were encountered: