Please explain me how this code works!

Notices and updates
Post Reply
ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Please explain me how this code works!

Post by 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):

Code: Select all

(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
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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:

Code: Select all

(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
Posts: 2019
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

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
Posts: 144
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

ale870 wrote: I like map usage for multiple assignment!
Me too ... With (map set ...) you can also swap easily and neatly

Code: Select all

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

;-)
BertrandnewLISP v.10.7.6 64-bit on Linux (Peppermint 9)

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

Me too ... With (map set ...) you can also swap easily and neatly
Great trick!
--

Post Reply