Binding a value to multiple values

Q&A's, tips, howto's
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Binding a value to multiple values

Post by Tim Johnson »

Hi:
In python I can do this:

Code: Select all

>>> 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?

Code: Select all

> (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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Binding a value to multiple values

Post by itistoday »

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

Code: Select all

$ 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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Binding a value to multiple values

Post by Tim Johnson »

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

Re: Binding a value to multiple values

Post by cormullion »

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:

Code: Select all

> (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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Binding a value to multiple values

Post by Tim Johnson »

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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Re: Binding a value to multiple values

Post by itistoday »

Tim Johnson wrote: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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Binding a value to multiple values

Post by Tim Johnson »

itistoday wrote:
Tim Johnson wrote: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
Posts: 21
Joined: Sun Apr 19, 2009 10:12 pm

Re: Binding a value to multiple values

Post by nallen05 »

an easier hack in this case might be to just to use the symbol itself as a container and evaluate it upon access
(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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Binding a value to multiple values

Post by Tim Johnson »

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

Locked