I needed a destructive append to a JS array, so I wrote this version of NCONC for PS. It's primitive, but does what I need.
(defun nconc (arr &rest arrs) (when (and arr (> (length arr) 0)) (loop :for other :in arrs :when (and other (> (length other) 0)) :do ((@ arr :splice :apply) arr (append (list (length arr) (length other)) other)))) arr)
I suppose this would naturally go in some kind of runtime library for PS. (We have quite a few utility functions that might belong there, in fact.) But I don't think there is one anymore. Should there be?
Daniel
Check out runtime/ps-runtime-lib.lisp
All the runtime code gets put into a global variable *ps-lisp-library*, which you can splice into or apply ps* or code walk. It would be nice to have a better mechanism.
Vladimir
2010/3/3 Daniel Gackle danielgackle@gmail.com:
I needed a destructive append to a JS array, so I wrote this version of NCONC for PS. It's primitive, but does what I need.
(defun nconc (arr &rest arrs) (when (and arr (> (length arr) 0)) (loop :for other :in arrs :when (and other (> (length other) 0)) :do ((@ arr :splice :apply) arr (append (list (length arr) (length other)) other)))) arr)
I suppose this would naturally go in some kind of runtime library for PS. (We have quite a few utility functions that might belong there, in fact.) But I don't think there is one anymore. Should there be?
Daniel
parenscript-devel mailing list parenscript-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
On Tue, Mar 2, 2010 at 9:17 PM, Daniel Gackle danielgackle@gmail.com wrote:
I needed a destructive append to a JS array, so I wrote this version of NCONC for PS. It's primitive, but does what I need.
(defun nconc (arr &rest arrs) (when (and arr (> (length arr) 0)) (loop :for other :in arrs :when (and other (> (length other) 0)) :do ((@ arr :splice :apply) arr (append (list (length arr) (length other)) other)))) arr)
I suppose this would naturally go in some kind of runtime library for PS. (We have quite a few utility functions that might belong there, in fact.) But I don't think there is one anymore. Should there be?
I maintain a utility library: http://github.com/gonzojive/paren-util You are welcome to add little functions like that to it--just use the pull request feature on github.
The main Parenscript file can be browsed here:
http://github.com/gonzojive/paren-util/blob/master/src/paren/util.paren
I generally use ASDF to compile parenscript code from various libraries into a single file
-- Red
Daniel
parenscript-devel mailing list parenscript-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
Ok, I pushed a patch with your implementation of NCONC to the repo.
Does anyone else have runtime Parenscript libraries? It would be nice to provide links to them. The Parenscript front page already links to Suave, but doesn't explain about paren-util, that's something that needs to be fied.
Thanks, Vladimir
2010/3/3 Daniel Gackle danielgackle@gmail.com:
I needed a destructive append to a JS array, so I wrote this version of NCONC for PS. It's primitive, but does what I need.
(defun nconc (arr &rest arrs) (when (and arr (> (length arr) 0)) (loop :for other :in arrs :when (and other (> (length other) 0)) :do ((@ arr :splice :apply) arr (append (list (length arr) (length other)) other)))) arr)
I suppose this would naturally go in some kind of runtime library for PS. (We have quite a few utility functions that might belong there, in fact.) But I don't think there is one anymore. Should there be?
Daniel
parenscript-devel mailing list parenscript-devel@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/parenscript-devel
On 3/8/10, Vladimir Sedach vsedach@gmail.com wrote:
Ok, I pushed a patch with your implementation of NCONC to the repo.
Does anyone else have runtime Parenscript libraries? It would be nice to provide links to them. The Parenscript front page already links to Suave, but doesn't explain about paren-util, that's something that needs to be fied.
Thanks, Vladimir
I had a look at the main file in paren-util, and compared it to my (much smaller, more minimalistic) utilities. Mine uses loop a lot more, and is aimed at replicating the CL primitives. util.paren seem to have quite a few replicated definitions: identity, find,
I also use the more Common-Lispy #'fun instead of fun for function values. Both works. I also have an idiom for some of those functions that have equivalent methods or other native support in JS, like so:
(defun first (x) (elt x 0)) (defmacro first (x) `(elt ,x 0))
Both functions and macros are defined so that we can both use the native form, and the first class function object.
CL-USER> (ps:ps (first x)) "x[0];" CL-USER> (ps:ps (funcall #'first x)) "first(x);"
This is either cool, or ugly, depending on your taste...
My functions are, in no particular order:
(defun pop (item) (@ (funcall (@ item splice) 0 1) 1))
(defun push (item arr) (funcall (@ arr splice) 0 0 item))
(defmacro rem (a b) `(% ,a ,b))
(defmacro mod (a b) `(% ,a ,b))
Mostly incomplete (not enough :from-end, :test-not etc), but here's my version of what's already in paren-util:
(defun find-if (pred elements &key (start 0)) (loop for index from start for el in (nthcdr elements start) when (funcall pred el) do (return el)))
(defun find (item elements &key (key #'identity) (test #'eq)) (find-if (lambda (el) (funcall test item (funcall key el))) elements))
(defun count-if (predicate items) (loop for el in items when (funcall predicate items) count t))
(defun position (item items) (loop for index from 0 for el in items when (eq item el) do (return index)))
(defun nthcdr (index elements) (loop ;; This is not quite cl:loop for x on elements for i from 0 while (< i index) finally (return x)))
(defun remove (item elements &key (key #'identity) (test #'eq)) (loop for el in elements unless (funcall test item (funcall key el)) collect el))
This is where loop really shines I guess.
Some of these may not run, as they are mostly untested. I have yet to decide on a way to do unit testing.
Free to copy. Please use and abuse as you see fit. :)
Yong.
parenscript-devel@common-lisp.net