Hash-map e contexts

Q&A's, tips, howto's
Locked
cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Hash-map e contexts

Post by cameyo »

How to filter only the contexts that represent a hash-map?
Example:
(dolist (_el (symbols))
(if (context? (eval _el))
(println (eval _el) {} (length (eval _el)))))
; -> Class 2
; -> MAIN 0
; -> Tree 0
; -> demo 0
; -> myHash 0

(dolist (_el (symbols))
(if (and (context? (eval _el))
(not (= _el 'MAIN))
(not (= _el 'Tree))
(not (= _el 'Class)))
(println (eval _el) {} (eval-string (string "(" _el ")")))))
; -> demo ()
; -> myHash (("1" 1) ("20" 20) ("57" 57) ("59" 59) ("81" 81))

Is there a way that doesn't use "eval-string" to display / count the values of a context representing a hash-map? "

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Hash-map e contexts

Post by ralph.ronnquist »

1) a "hashmap" is a context without default functor, i.e.

Code: Select all

(and (context? S) (nil? (sym (term S) S nil))) 
2) using the symbol as functor results in its list if entries, i.e.

Code: Select all

(apply S)

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: Hash-map e contexts

Post by ralph.ronnquist »

tried to edit, to wrap the sym term into an eval, but the server doesn't let me...

cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Hash-map e contexts

Post by cameyo »

Thank you. I'll do some tests.

cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Hash-map e contexts

Post by cameyo »

More difficulties:
a context with functor and functions can be a hash-map too.

cameyo
Posts: 183
Joined: Sun Mar 27, 2011 3:07 pm
Location: Italy
Contact:

Re: Hash-map e contexts

Post by cameyo »

This works for me:

Code: Select all

(define (hash? hash)
  (and (context? (evals hash))
       (not (list? (evals (sym (term hash) hash nil))))))
With "eval" instead of "evals" (to avoid "Internal server error")

Locked