Re: JESS: Good grief

"Wolfgang Laun" <[email protected]>
Newsgroups gmane.comp.java.jess
Message-ID <14630_1281958915_o7GBbsqc014679_AANLkTimrH5gCG-46u9Rihf5H296XOuFOFw+RFp22smUk@mail.gmail.com>
On 16 August 2010 05:37, Donald Winston <[email protected]> wrote:

> I guess you got to use toArray and as-list all the time for any kind of
> array.
>
> Not quite. And the remaining todo's can be nicely abstracted into a couple
of deffunctions for doing the puts and gets:

(deffunction putIntoMap (?map ?key $?val)
  (if (= 1 (length$ $?val))
   then (call ?map put ?key (nth$ 1 $?val))
   else (call ?map put ?key $?val)))

(deffunction getFromMap (?map ?key)
  (bind ?val (call ?map get ?key))
  (bind ?strep (call String valueOf ?val))
  (if (and (eq ?strep "null") (= ?val nil))
   then (return nil)
   else (bind ?clname ((call ?val getClass) getName))
        (if (= "[" (call ?clname substring 0 1))
         then (return (as-list ?val))
         else (return ?val))))

(bind ?myMap (new java.util.HashMap))

(bind ?string "string-of-char")
(bind ?strings (list "un" "deux" "trois"))
(bind ?number 42)
(bind ?numbers (list 1001 64 3.14159))

(putIntoMap ?myMap "string"  ?string)
(putIntoMap ?myMap "strings" ?strings)
(putIntoMap ?myMap "nephews" "Huey" "Dewey" "Louie")
(putIntoMap ?myMap "number"  ?number)
(putIntoMap ?myMap "numbers" ?numbers)
(putIntoMap ?myMap "primes" 2 3 5 7 11 13)

(foreach ?key (?myMap keySet)
;(list "string" "strings" "nephews" "number" "numbers" "primes" "foo")
  (bind ?val (getFromMap ?myMap ?key))
  (if (listp ?val)
   then (printout t ?key " (list): " ?val crlf)
   else (printout t ?key " (scalar): " ?val crlf)))

This is the output:

numbers (list): (1001 64 3.14159)
string (scalar): string-of-char
number (scalar): 42
strings (list): ("un" "deux" "trois")
primes (list): (2 3 5 7 11 13)
nephews (list): ("Huey" "Dewey" "Louie")

(IMHO, Java Arrays aren't first choice in an application such as the one
you've described. YMMV.)
-W
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.