Re: Canonical use of namespaces

2013-04-21 Thread Thorsten Jolitz
Alexander Burger  writes:

Hi Henrik, Hi Alex,

thanks for your replies - question answered!

-- 
cheers,
Thorsten

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Canonical use of namespaces

2013-04-21 Thread Alexander Burger
Hi Thorsten,

> I haven't seen so many uses of the (quite recently) introduced
> namespaces in PicoLisp, so I would like to ask what is the canonical way
> to use them?

An important question. I've thought about this, too from time to time,
but never actually used namespaces in production code so far.


> Say I have a library called 'pio.l'. In Emacs Lisp I now would prefix
> all definitions in this library with "pio-", i.e. 
> 
> ,-
> | (de pio-do-something (X Y) ...)
> `-

This is a prefectly valid strategy, and anyway is the only way to go in
PicoLisps without namespaces (i.e. on 32-bits).


> Since there are namespaces available now, I would rather avoid this and
> use something like this:
> 
> ,
> | (symbols "pio" 'pico)
> | (local do-something X Y)
> | 
> | (de do-something (X Y) ...)
> `
> 
> My questions are now:
> 
> 1. Is using a transient namespace the default,

Transient namespaces I would only use for small packages. Doing so would
require prefixing "exported" symbols with a target-namespace, e.g.
'pico', to make them resident there

   (de pico~myPioFunction (X Y)
  .. )

because otherwise _all_ definitions of that namespace would be gone when
"pio" goes out of scope. With the above, 'myPioFunction' can be directly
used in the 'pico' namespace.


Transient namespaces are nice if you want to avoid using transient
symbols for local variables in cases they are necessary. So instead of

   (de pico~myPioFunction ("X" "Y")
  (fun "X" .. "Y" ..) )

you could write

   (symbols "pio" 'pico)
   (local X Y)

   (de pico~myPioFunction (X Y)
  (fun X .. Y ..) )


> or should I use
> 
>  ,-
>  | (symbols 'pio 'pico)
>  `-

This I would use for creating larger packages. In that case I would not
prefix "exported" symbols:

   (symbols 'pio 'pico)
   (local X Y)

   (de myPioFunction (X Y)
  (fun X .. Y ..) )

so that 'myPioFunction' can be accessed from other packages as

   (pio~myPioFunction ..)

or by spaning other packages as descendenants from 'pio'

   (symbols 'myApp 'pio)

or possibly from more than one package

   (symbols 'myApp 'pio 'bla 'mumble)


> 2. Is it necessary to put all library definitions (and all ARGS) into
> the (local ...) function to define them as part of the new namespace?

I would not do so, to avoid clobbering the sources with too many 'local'
calls.

Instead, I would just go ahead, and 'load' the new package, watching for
the "redefined" messages of possible collisions. This is a matter of
taste, though.

♪♫ Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: Canonical use of namespaces

2013-04-21 Thread Henrik Sarvell
1.) I use namespaces in what I currently work on. Works good, I use
them wherever I would've earlier just created a "static" class as a
container. It helps to communicate the intent better where intent in
this case is just a collection of functions that I want to
group/encapsulate together.

Here is the top of a namespace that I use to extend the default
functions that operate on lists:

(symbols 'exlst 'pico)

(local range replace)

(de range (Start End . @)
  (let Pad (next)
 (make
(for (N Start (>= End N) (inc N)) (link (if Pad (pad Pad N) N))) ) ) )

(de replace (Search Replace Stack)
   (use @A @B
  (ifn (match (append (list '@A) Search (list '@B)) Stack)
 Stack
 (append @A Replace @B) ) ) )

(de flatten @
   (make
  (for L (rest)
 (recur (L)
(for El L
   (if (lst? El)
  (recurse El)
  (link El)))

2.) As you can see the call to local there is needed to avoid clashes
with functions that are already defined. So no, you don't need to put
everything there, for instance flatten in my case since it doesn't
exist in 'pico (at least not per default).

3.) No, don't keep it "simple".


On Sun, Apr 21, 2013 at 3:12 PM, Thorsten Jolitz  wrote:
>
> Hi List,
>
> I haven't seen so many uses of the (quite recently) introduced
> namespaces in PicoLisp, so I would like to ask what is the canonical way
> to use them?
>
> Say I have a library called 'pio.l'. In Emacs Lisp I now would prefix
> all definitions in this library with "pio-", i.e.
>
> ,-
> | (de pio-do-something (X Y) ...)
> `-
>
> Since there are namespaces available now, I would rather avoid this and
> use something like this:
>
> ,
> | (symbols "pio" 'pico)
> | (local do-something X Y)
> |
> | (de do-something (X Y) ...)
> `
>
> My questions are now:
>
> 1. Is using a transient namespace the default, or should I use
>
>  ,-
>  | (symbols 'pio 'pico)
>  `-
>
> instead?
>
> 2. Is it necessary to put all library definitions (and all ARGS) into
> the (local ...) function to define them as part of the new namespace?
>
> 3. Should I use namespaces anyway, or rather keep it simple and use the
> "pio-" prefix?
>
> --
> cheers,
> Thorsten
>
>
> --
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe