Please explain me how this code works!

Started by ale870, May 30, 2009, 11:35:24 AM

Previous topic - Next topic

ale870

Hello,



I was using (process) function, in order to communicate with an external application

Following the manual I successfully accomplished to my job, but I cannot understand how this code works!



Look at here (from the manual):


(map set '(myin bcout) (pipe))
(map set '(bcin myout) (pipe))


Can you explain me what's happen in such function?



Thank you for your help!
--

newdep

#1
You need an explenation of what 'pipe' does or what this function does?



..First grab a beer a pencil and a paper..



Start drawing 2 boxes beside eachother

Then put 4 lines between the boxes

then name them myin myout bcin bcout

and give the ends of the lines an arrow





..so far for the piping...



(pipe) returns a list of 2 channels 1x input 1x output

MAP maps these channels to the list.



.. so far for the mapping..



Finished your beer already? ;-)
-- (define? (Cornflakes))

cormullion

#2
Hi there Alessandro!



There's a diagram of this in the Introduction to newLISP, which may help. My understanding is based on that diagram... :)



pipe returns a list of two numbers, one for the input or read channel, and one for the output or write channel, for a process. These are the 'handles' to the pipe, which you can use later in read/write functions.



You have to set up two pipes. though, one to go from newLISP to the bc process (newLISP writes, bc reads), and one for the opposite way (newLISP reads, bc writes). So there are two pipe operations. The process  function lets you provide two pipes...



And map is able to work on two or more lists, so it can set two symbols at the same time, if a list of two values is also provided, which it is here.



So this code:


(map set '(myin bcout) (pipe))
(map set '(bcin myout) (pipe))


sets four symbols, allowing you to read and write - via two pipes - to a single process (not yet started).



I think... :)



And I posted the same time as Norman.. :)

newdep

#3
hahaha.... thanks for the backup..

He probably will understand yours when he finished the beer ;-)



I ment.. He probably understand yours without drinking the beer..



..Aaa never mind Ill finished mine first (La Cerveza Mas Fina.. Corona)
-- (define? (Cornflakes))

ale870

#4
Thank you guys!

Now I need to buy more beers :-) (but I'm italian, and my "fuel" is the coffee!!!)



I made a big mistake from the beginning, since I didn't see that (pipe) was a newLisp function!

I like map usage for multiple assignment!



Your info was very valuable!



Thank you again!



(now I'm creating a pipe from the coffee machine to my mouth :-)
--

newBert

#5
Quote from: "ale870"
I like map usage for multiple assignment!


Me too ... With (map set ...) you can also swap easily and neatly


newLISP v.10.0.6 on Win32 IPv4, execute 'newlisp -h' for more info.

> (map set '(a b c d e f) '(1 2 3 4 5 6))
(1 2 3 4 5 6)
> (map set '(a b c d e f) (list b c a e f d))
(2 3 1 5 6 4)
>


as in Python with tuples:

a, b, c, d, e, f = 1, 2, 3, 4, 5, 6 and a, b, c, d, e, f = b, c, a, e, f, d



;-)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

ale870

#6
QuoteMe too ... With (map set ...) you can also swap easily and neatly


Great trick!
--