newLISP Fan Club

Forum => newLISP newS => Topic started by: ale870 on May 30, 2009, 11:35:24 AM

Title: Please explain me how this code works!
Post by: ale870 on May 30, 2009, 11:35:24 AM
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!
Title:
Post by: newdep on May 30, 2009, 02:21:49 PM
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? ;-)
Title:
Post by: cormullion on May 30, 2009, 02:27:34 PM
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.. :)
Title:
Post by: newdep on May 30, 2009, 02:33:22 PM
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)
Title:
Post by: ale870 on May 30, 2009, 03:00:14 PM
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 :-)
Title:
Post by: newBert on May 31, 2009, 12:47:30 AM
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



;-)
Title:
Post by: ale870 on May 31, 2009, 05:57:38 AM
QuoteMe too ... With (map set ...) you can also swap easily and neatly


Great trick!