The docs:
(later 'var . prg) -> varExecutes prg in a pipe'ed child process. The
return value of prg will later be available in var. Note that later
uses pr and rd to communicate the result, so prg should not write any
data to standard output as a side effect.

: (prog1  # Parallel background calculation of square numbers
   (mapcan '((N) (later (cons) (* N N))) (1 2 3 4))
   (wait NIL (full @)) )
-> (1 4 9 16)

It might help that this code the same.
: (prog1  # Parallel background calculation of square numbers
   (mapcan '((N) (later (list) (* N N))) (1 2 3 4))
   (wait NIL (full @)) )
-> (1 4 9 16)

Or more departmentalized:
(setq B (list ))
-> (NIL)
('((N) (later B (prog1 (* N N) (wait 8000)))) 1)
-> (NIL)
B
-> (NIL)
(wait 8000)
..
B
-> (1)

Later needs a store to place things. This code uses the following non
obvious fact:

 (setq D (list))
-> (NIL)
 (setq `D 8)
-> 8
 D
-> (8)

or in a more general case:
(setq U (range 1 20))
(set (nth U 8) 0)
U
-> (1 2 3 4 5 6 7 0 9 10 11 12 13 14 15 16 17 18 19 20)

What happens step by step:
1.0 prog1 evaluates  (mapcan '((N) (later (list) (* N N))) (1 2 3 4))
and stores the resulting expression
1.1 mapcan calls '((N) (later (list) (* N N))) with 1 through 4.
1.1.1 Each call of this lamda creates a new cell (lets call it temp)
by calling (list) or (cons)
1.1.2  temp is set to be the place where return value of the inner
expression which is calculated by another process is stored by later
1.1.3 the temp cell is returned with the result of the calculation still pending
1.2 mapcan takes these cells and links them destuctively in to one list
1.3 mapcan returns that list and the result it written in to @.

2.0 prog1 evaluates (wait NIL (full @)) ) where @ refers to the return
value of the previous expression at this level of evaluation.
3.0 prog1 returns the value it stores. The value being the value that
was also refered by @

What is @?
@ is the list of cells where (in which car) other process will put
their values into.

Why wait for the full list? This might get really ugly if the list
becomes partially garbage and is written to. I am not sure this
behaves since this is internal to the interpreter and "undefined
behavior"



On Sun, 12 May 2019 at 22:06, C K Kashyap <ckkash...@gmail.com> wrote:
>
> Hi Alex,
> Could you please explain the role of (cons) as the first parameter to later? 
> If I understand correctly, that does not seem to have a role in the the final 
> list return values from the child processes.
> Regards,
> Kashyap
>
> On Sun, May 12, 2019 at 7:17 AM C K Kashyap <ckkash...@gmail.com> wrote:
>>
>> Yup ... that's the first edit I tried :)
>> Regards,
>> Kashyap
>>
>> On Sun, May 12, 2019 at 12:34 AM Alexander Burger <a...@software-lab.de> 
>> wrote:
>>>
>>> On Sat, May 11, 2019 at 10:09:27PM +0200, Alexander Burger wrote:
>>> >    (let Lst
>>> >       (mapcan
>>> >          '((File)
>>> >             (later (cons)
>>> >                (... download File ...)
>>> >                T ) )
>>> >           ...
>>>
>>> Note that instead of 'T' you may also return any non-NIL value, e.g the
>>> downloaded size, or a list with statistical values.
>>>
>>> / A!ex
>>>
>>> --
>>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

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

Reply via email to