How to set a local 'Contexts Hash'

Featuring the Dragonfly web framework

How to set a local 'Contexts Hash'

Postby ssqq » Tue Nov 04, 2014 6:48 am

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

Code: Select all
(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))
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: How to set a local 'Contexts Hash'

Postby Lutz » Tue Nov 04, 2014 7:59 am

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)

Code: Select all
(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.
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: How to set a local 'Contexts Hash'

Postby ssqq » Wed Nov 05, 2014 6:56 am

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

Code: Select all
(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)
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: How to set a local 'Contexts Hash'

Postby Lutz » Wed Nov 05, 2014 7:29 am

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.
Lutz
 
Posts: 5258
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: How to set a local 'Contexts Hash'

Postby zhanglong » Wed Nov 26, 2014 11:36 am

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
zhanglong
 
Posts: 9
Joined: Thu Jun 27, 2013 3:58 pm

Re: How to set a local 'Contexts Hash'

Postby ssqq » Thu Jan 08, 2015 2:19 am

thanks, it is a good idea.
ssqq
 
Posts: 88
Joined: Sun May 04, 2014 12:49 pm

Re: How to set a local 'Contexts Hash'

Postby abaddon1234 » Thu Mar 24, 2016 4:59 am

abaddon1234
 
Posts: 21
Joined: Mon Sep 14, 2015 3:09 am


Return to So, what can you actually DO with newLISP?

Who is online

Users browsing this forum: No registered users and 2 guests