Binding a value to multiple values

Started by Tim Johnson, January 05, 2010, 08:32:07 AM

Previous topic - Next topic

Tim Johnson

Hi:

In python I can do this:

>>> inner = []
>>> outer1 = []
>>> outer2 = []
>>> outer1.append(inner)
>>> outer2.append(inner)
>>> inner.append(1)
>>> outer1
[[1]]
>>> outer2
[[1]]

How may I do the same thing in newlisp?

> (set 'outer1 '() 'outer2 '())
()
> (push inner outer1)
(())
> (push inner outer2)
(())
> (push 1 inner)
(1)
> outer1
(())  ;; was hoping for '((1))
> outer2
(()) ;; was hoping for '((1))

TIA

tim
Programmer since 1987. Unix environment.

itistoday

#1
newLISP doesn't do reference passing in most situations (including that one), therefore that won't work because outer1 and outer2 contain copies of inner, not a reference to inner.



If you need to be able to do reference passing in newLISP, try Objective newLISP (link in sig):


$ newlisp ObjNL.lsp
newLISP v.10.1.9-dev on OSX IPv4 UTF-8, execute 'newlisp -h' for more info.

> (new-class 'Container)
Container
> (setf Container:lst '())
()
> (setf inner (instantiate Container))
Container#1
> (setf outer1 (instantiate Container))
Container#2
> (setf outer2 (instantiate Container))
Container#3
> (push inner outer1:lst)
(Container#1)
> (push inner outer2:lst)
(Container#1)
> (push 1 inner:lst)
(1)
> outer1:lst
(Container#1)
> (. (outer1:lst 0) lst)
(1)
Get your Objective newLISP groove on.

Tim Johnson

#2
Hi  itistoday:

I was aware of your object library, and also aware of newlisp's reference passing (or limitations thereof).

Before proceding along your path, or any path that instantiates objects, I'd like to ask your opinion of

the following:

The example that I cite of python can be expanded to more efficient coding without a lot of bulking up

of code and little or no degrading of performance.

My goal (recreating what I have done in python and rebol)

would be to create two outer data structures to offer alternate mappings to the same inner data structures.



One of the data structures would preserve the original order in which the mapping is created, therefore making it

easier codewise to recreate the original source text, but with modification created in the second data structure.

The second data structure would enable easier access to the inner structures.



Perhaps it would more efficient performance to simply map with one data structure even 'tho that might necessitate

more coding. That might be the more "newlispish" way.



Have you done any comparative benchmarking?

thanks

tim
Programmer since 1987. Unix environment.

cormullion

#3
My guess is that Objective newLISP would be the best solution - (tho I wish I understood it better... :(). The closest you can get to what was typed above in plain newLISP might be something like this:


> (define inner:inner)
nil
> (define (outer1:outer1) (inner))
(lambda () (inner))
> (define (outer2:outer2) (inner))
(lambda () (inner))
> (inner "1" 1)
> (outer1)
(("1" 1))
> (outer2)
(("1" 1))
> (inner "2" nil)
nil
> (outer1)
(("1" 1))
> (outer2)
(("1" 1))
>


so both outer1 and outer2 reflect  changes made to inner. But you'd be very limited as to what you could do, I think, and I'm not sure it's what you want anyway!



PS: was replying to your first post, not the second, Tim!

Tim Johnson

#4
Thank you cormullion, we have in you an affirmative second opinion :) so I will give it a try

and implement Objective newLISP. Thus you and I may both learn more. Furthermore, I will

not need to build these data structures every time the application runs. The source will be a

text/html file, data structures will be cached and the code to build the data structures need

to be executed only when the cache file goes away or if the text file has a later MTime.



My time is limited, so I will give this my attention when other obligations don't interfere. I hope that

both you and itistoday will stay subscribed to this thread.

stay tuned....

cheers

tim
Programmer since 1987. Unix environment.

itistoday

#5
Quote from: "Tim Johnson"Have you done any comparative benchmarking?


I'm not really sure what benchmarking you're referring to. Comparing what to what?
Get your Objective newLISP groove on.

Tim Johnson

#6
Quote from: "itistoday"
Quote from: "Tim Johnson"Have you done any comparative benchmarking?


I'm not really sure what benchmarking you're referring to. Comparing what to what?

Never mind. (see my previous post)
Programmer since 1987. Unix environment.

nallen05

#7
an easier hack in this case might be to just to use the symbol itself as a container and evaluate it upon access


Quote
 (set 'outer1 '() 'outer2 '())

()

> (set 'inner '())

()

> (push 'inner outer1) ; quoted inner

(inner)

> (push 'inner outer2) ; quoted inner

(inner)

> (push 1 inner)

(1)

> (map eval outer1) ; evaluating elements of outer1

((1))

> (map eval outer2) ; evaluating elements of outer2

((1))

Tim Johnson

#8
I like your method. thanks
Programmer since 1987. Unix environment.