Return a context from a function

Notices and updates
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Return a context from a function

Post by Tim Johnson »

I'd like to have a function that creates and returns a new context from an existing source context:

Code: Select all

(context 'hash)
(setq context-name "hash"    ## name of context
	  object-name  "[none]"  ## name of object
	  lst '()                ## list - may be nested
	  )
(context 'MAIN)
(define (Hash! l oname)
  (local (newhash)
	(set 'newhash(new hash 'newhash))
	(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
	(if l (set 'newhash:lst l))
	(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
	(if oname (set 'newhash:object-name oname))
	(println "newhash = ["  newhash:object-name "]") ;; DEBUG newhash
	newhash))
I evaluate the following with result:

Code: Select all

> (set 'y (Hash! '((name "tim")) "myhash"))
newhash = [()]
newhash = [((name "tim"))]
newhash = [myhash]
newhash
And then evaluate 'y with (obviously) not the results I'm looking for.

Code: Select all

> y:lst
?
> y:object-name
?
Help and examples are welcome. I haven't been able to find any
examples of this specific topic in documentation.
Thanks
Tim

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Not sure I understand what you want to do, but making a context from a source context is just one statement using 'new':

Code: Select all

(context 'Source)
	(set 'varA 123)
	(set 'varB "hello")
(context MAIN)

(new Source 'Target)

; now Target is a copy of Source

> Target:varA
123
> Target:varB
"hello"
> 
If you want to put this into a function, you would do:

Code: Select all

(define (copy from to)
	(new from to))

; now use the new function

> (set 'y (new Source 'Target)) 
Target
> y:varA
123
> y:varB
"hello"

; y contains the context Target
> (symbols y)
(Target:varA Target:varB)
> 
The problem in you code is having 'newhash' as a local variable, the contents of which will be deleted when exiting the 'local' statement. Don't put the name of a context as a local variable. This would imply a context beeing anonymous. A context always must be bound to a symbol in MAIN.

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post by Tim Johnson »

Hi Lutz:
Since I sent the previous, I find that the following:

Code: Select all

(define (Hash! l oname)
    (new hash 'newhash)
	(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
	(if l (set 'newhash:lst l))
	(println "newhash = ["  newhash:lst "]") ;; DEBUG newhash
	(if oname (set 'newhash:object-name oname))
	(println "newhash = ["  newhash:object-name "]") ;; DEBUG newhash
	newhash)
gives me results closer to what I'm looking for - at least with preliminary testing,
and the changes are consistent with your advice about not making
the variable local.
To clarify what I am after:
I'd like to be able to instantiate any number of "objects" which might be
multi-level associative lists. This is consistent with much that I now do in other languages
and consistent with RAD tools that I've developed for python and rebol
code generation.

Python has the feature of object intializers - so I'm trying to replicate that
here with the Hash! function. At a top level I might have something like this:

Code: Select all

(set 'lst '(
    (id001 (name "Anne") (addr (country "USA") (city "New York")))
    (id002 (name "Jean") (addr (country "France") (city "Paris")))
))
(set 'records (Hash! lst "records"))
;; here we have a default function
(define (hash:hash)
  (let((res lst))
	(dolist (i (args) (not res))
			(set 'res (assoc i res)))))
;; and a function call like
(records 'id001 'addr 'country)
Such an instantiation might have a 'strict parameter that, if
set might result in an error thrown if for instance, a non-nil
search result - and such an error would
contain the name of the context ('context-name) and the name
of the instantiation ('object-name).
Are my intentions any clearer now? :-)
Thanks again
tim

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

We posted at the same time, I added to my previous post, while you where posting ;-)

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post by Tim Johnson »

Lutz wrote:We posted at the same time, I added to my previous post, while you where posting ;-)
"""
Great Minds Run in the Same Gutter -
Even in different time zones.
""" - Alaskus Curmudgeous

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Have you also looked into this?

http://newlisp.org/download/development ... .html#hash

available in current development version 9.3.11

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Post by Tim Johnson »

Lutz wrote:Have you also looked into this?

http://newlisp.org/download/development ... .html#hash

available in current development version 9.3.11
Very interesting!
Looking forward to it in the stable distro ....
thanks
tim

Locked