On Sat, Mar 13, 2021 at 01:30:27PM +0100, pd wrote:
> Composing strings (packing) have two main advantages:
> 
> 1. Strings may be inmutable (you can return a new different string, maybe a
> copy)
> 2. Strings can be passed as parameter

Yes. (And strings are always immutable in PicoLisp anyway)


> : (setq X (<p> hallo))
> <p></p>-> "</p>"
> : X
> -> "</p>"
> 
> You can argue return value is nothing you must worry about because you're
> only going to print the result, so better use it as a side effect. But this
> is not always true, returning a string you can further compose it and make
> transformations to it

Right. The examples were for output only.


> Ovbiously you can arrange the program (P) you pass to the fexpr and get
> those problems solved by example packing into the result but it may become
> not easy to write the function due to side effects:
> 
> : (de pp P (prin "<p>") (run P) (prin "</p>") (pack "<p>" (run P) "</p>") )

This is a bit problematic, because the body in P is executed twice. Not only is
this slower, but may create havoc because in a typical GUI program *all* logic
happens in these bodies. This logic would be executed several times, doing lots
of unexpecyed things. Better then:

   (de pp P
      (prin (pack "<p>" (run P) "</p>")) )

'prin' returns the argument string.


> the example given by Alex to get a multiline div is simply this one-liner:
> 
> : (prinl (d "red" (glue "^J" (mapcar p '("ABC" "DEF" "GHI")))))
> 
> <div class="red"><p>ABC</p>
> <p>DEF</p>
> <p>GHI</p></div>

For such a simple example it works. But keep in mind that typically 'P'
is a large program, with lots of 'if's, 'while's  and arbitrarily deeply
nested other HTML tags.

☺/ A!ex

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

Reply via email to