[parenscript-devel] A PS version of NCONC

szergling senatorzergling at gmail.com
Tue Mar 9 18:05:07 UTC 2010


On 3/8/10, Vladimir Sedach <vsedach at 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.




More information about the parenscript-devel mailing list