How to set a local 'Contexts Hash'

Started by ssqq, November 03, 2014, 10:48:05 PM

Previous topic - Next topic

ssqq

When default functor used as a pseudo hash function, How to set a local 'Hash'? Because Context is global.


(define (check-key x)
    ;; I don't know whether environment have defined this symbol
    (define aHash:aHash)
    (aHash 'key-1 1)
    (aHash 'key-2 2)
    (if (aHash x) true nil))

Lutz

#1
Instead of (define hash:hash) use (new Tree 'hash) it does the same but also makes the default functor hash:hash a constant (containing nil)



(context 'Foo)

(new Tree 'MAIN:hash) ; must qualify with MAIN, symbol trees are global

(hash "one" 1)
(hash "two" 2)

(println (hash "one")) ; prints 1

(context MAIN)

(println (hash)) ; hash is global


But as a matter of good style, I would pre-define all hashes and pre-create other contexts in a MAIN context module like all other globally used symbols. In bigger newLISP projects or when working with different programmers on the same project clashes/problems are avoided this way.

ssqq

#2
In bigger project, Context as hash in function maybe pollute global symbol tree.



(define (make-local-hash)
  (let (local-hash (new Tree 'a-hash))
    (a-hash "one" 1)
    (a-hash "two" 2)
))

(set 'a-hash 2)
(println a-hash) ; => 2
(make-local-hash)
(println a-hash) ; => a-hash

(exit)

Lutz

#3
We call context dictionaries often "hashes", but there is no hash function at the base of it. "Hash" is just a convenient name as most other languages implement lookup functionality using hash functions.



In newLISP dictionaries are built as separate red-black balanced binary trees and only the root of it - the name of the context - is part of the main symbol space. So no namespace "pollution" is taking place.

zhanglong

#4
context is value, so we can use (uuid) to generate unique symbol in MAIN, then make it a context. But there's no gc, you have to delete the context yourself.





(set 'a (new Tree (sym (string "_" (uuid)) MAIN)))





delete the context like this



(set 'name (sym (term a) MAIN)) ; find the context's name in MAIN

(delete name) ; delete the context

(delete name) ; delete the symbol in MAIN

ssqq

#5
thanks, it is a good idea.

abaddon1234

#6
it is a good idea.

http://9gclub.blogspot.com/2015/11/gclub.html">gclub เข้าไม่ได้