-
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
-zip to "merge" two lists together #14
Comments
This is neat, thanks. :-) In clojure, there is I certainly think that the function taking two lists, and the function taking one list, a function and an initial value, should be separate. Any thoughts on naming the second one? |
The |
Maybe it can be called Didn't know about #', cool. About the name, (defun -zipWith (list1 list2 fn)
"Zip the two lists LIST1 and LIST2 using a function FN. This
function is applied pairwise taking as first argument element of
LIST1 and as second argument element of LIST2 at corresponding
position."
(let ((r nil))
(while (and list1 list2)
(!cons (funcall fn (car list1) (car list2)) r)
(!cdr list1)
(!cdr list2))
(nreverse r)))
then (defun -zip (list1 list2)
"Zip the two lists together. Return the list where elements
are cons pairs with car being element from LIST1 and cdr being
element from LIST2. The length of the returned list is the
length of the shorter one."
(-zipWith list1 list2 'cons)) The version with generator function is something to basically simulate "infinite lists", so you can for example zip a list with |
It would be possible to write a lib with lazy sequences in elisp too, but that's a job for someone else I think. Got enough on my plate. :-) I really like zip-with. This is going in tonight. I can understand the wish to simulate lazy sequences with a function+initial value pair. I'll think on that some more. Thanks! |
I also added an anaphoric version of |
I'm pretty surprised this isn't here already! :D I just had a need for this so here's the code:
Examples:
The last example is in fact so cool that it maybe even deserves its own function. Up to you.
The text was updated successfully, but these errors were encountered: