-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add -npartial
for arbitrary partial application
#204
base: master
Are you sure you want to change the base?
Conversation
Offset 1 as "first" is confusing I think.. I assumed that skips the first argument. Wouldn't
|
Actually, I think I don't understand how the offset works at all. Because you only give it one argument (5) to be |
I did't spot I'll try to explain the offset better. It basically says where to place the curried arguments in the list of additional arguments. If the offset is 0, (defun -npartial (n fn &rest args)
(lambda (&rest args-around) (apply fn (append (-take n args-around)
args
(-drop n args-around))))) It's a bit difficult to write good examples to show this happening – perhaps some sort of string concatenation would be better than a calculation that's difficult to follow – because this is only useful for a function which has many arguments, but I'll give it a shot later. |
Also: shouldn't the |
I don't know why the build fails, it has to do with when the code is compiled vs evaled... Same problem happens in another issue. We are dropping e23 support anyway so don't worry. |
I had a quick look to see how many functions have loads of arguments – and (at least in my config), there are the following functions (w/ arities). Note that these are only the ones that were loaded in Emacs at the time.
Though I really like the syntax of ;; (speedbar-insert-button TEXT FACE MOUSE FUNCTION &optional TOKEN PREVLINE)
(-npartial 3 #'speedbar-insert-button #'my/button-function)
(-cut speedbar-insert-button <> <> <> #'my/button-function <> <>) Though perhaps the latter is more readable! Either way, it's nice to have alternatives – that's why we have aliases, right? |
Yea, I don't really have a problem adding this, but it was difficult for me to understand how this works. Could you please remove the texi/info changes from the patch? I will regenerate it on my version (we haven't figured out yet how to build this reliably... we should probably do that on travis and push back to the repo). |
Done! I've made a slight change also, to allow the offset to be negative (i.e. from the end of the list). The commit message is (I think) also more explanatory. Add `-npartial' for arbitrary partial applicationCurrently, we have
ARGS will be spliced in after the Nth element in the additional args. If This function satisfies the following laws: (-npartial 0 ...) ≡ (-partial ...)
(-npartial -1 ...) ≡ (-rpartial ...) E.g.: (funcall (-npartial 1 (lambda (a b c) (+ a (* b c))) 5)
2 3) ;; => 17
(funcall (-npartial 2 (lambda (a b c d) (+ a (* b c) d)) 3)
2 4 5) ;; => 19
(funcall (-npartial -1 #'concat "last " "words.")
"These " "are " "the ") ;; => "These are the last words."
(funcall (-npartial 3 (lambda (a b c d e f) (concat a b c d e f "."))
"penultimate " "words ")
"I'll " "put " "the " "here")
;; => "I'll put the penultimate words here." EDIT: I've also added myself to the list of contributors in the two template files. |
-npartial
for arbitrary currying-npartial
for arbitrary partial application
Currently, we have `-partial' and `-rpartial' for currying with arguments at either the left or the right. `-npartial' is a generalisation of `-partial' and `-rpartial'. It takes an offset N to determine where to put the curried arguments, a function FN, and the partially applied ARGS. ARGS will be spliced in after the Nth element in the additional args. If N is negative, -1 is taken to be the final item, -2, the penultimate item, etc. This function satisfies the following laws: (-npartial 0 ...) ≡ (-partial ...) (-npartial -1 ...) ≡ (-rpartial ...)" E.g.: (funcall (-npartial 1 (lambda (a b c) (+ a (* b c))) 5) 2 3) ;; => 17 (funcall (-npartial 2 (lambda (a b c d) (+ a (* b c) d)) 3) 2 4 5) ;; => 19 (funcall (-npartial -1 #'concat "last " "words.") "These " "are " "the ") ;; => "These are the last words." (funcall (-npartial 3 (lambda (a b c d e f) (concat a b c d e f ".")) "penultimate " "words ") "I'll " "put " "the " "here") ;; => "I'll put the penultimate words here."
Currently, we have
-partial
and-rpartial
forcurryingpartial application witharguments at either the left or the right,
but we don't have anythingfor truly arbitrary application of curried arguments.
ThereforeI've included-npartial
to plug that gap. It takes an offsetn
to determine where to put the curried arguments, a functionfn
, andthe curried
args
.E.g.:
(Note that I have a slightly different version of texinfo – 6 instead
of 4 – so the way that the paragraphs have been filled is slightly
differently, but I'm afraid there's nothing much I can do, because Arch
can't downgrade all the way to 4. If this is a big problem, I can go
through and edit this manually, but I'd rather not if it isn't necessary.)