Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Clojure hash table accessors in CL
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 54 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Leslie P. Polzer  
View profile  
 More options Dec 14 2008, 6:01 am
Newsgroups: comp.lang.lisp
From: "Leslie P. Polzer" <leslie.pol...@gmx.net>
Date: Sun, 14 Dec 2008 03:01:04 -0800 (PST)
Local: Sun, Dec 14 2008 6:01 am
Subject: Clojure hash table accessors in CL
Clojure, from what I've gathered, lets one access hash tables just by
referring to them in the CAR of an evaluated form:

(defparameter ht (make-hash-table))

(ht 'foo) == (gethash 'foo ht)
(setf (ht 'foo) t) == (setf (gethash 'foo ht) t)

It should be easy to emulate the hash table reader at least in
function context, by defining a custom DEFUN-wrapping macro that
dispatches either to FUNCALL or to GETHASH depending on the data type.

Is this also possible for the SETF expansion? I think it should be,
but I hardly ever use the DEFSETF facilities so I'm not sure how.

  Leslie


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Dec 14 2008, 6:15 am
Newsgroups: comp.lang.lisp
From: Rainer Joswig <jos...@lisp.de>
Date: Sun, 14 Dec 2008 12:15:27 +0100
Local: Sun, Dec 14 2008 6:15 am
Subject: Re: Clojure hash table accessors in CL
In article
<47f1f8ae-bf53-4224-9564-28496de98...@r37g2000prr.googlegroups.com>,
 "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:

> Clojure, from what I've gathered, lets one access hash tables just by
> referring to them in the CAR of an evaluated form:

> (defparameter ht (make-hash-table))

> (ht 'foo) == (gethash 'foo ht)
> (setf (ht 'foo) t) == (setf (gethash 'foo ht) t)

Compare that with Lisp Machine Lisp from 70s/80s:

Command: ("abc" 1)
#\b

That's an old Lisp feature to make some data objects
having a functional interpretation. In LML for examples
arrays in functional position were evaluated as doing
AREF.

> It should be easy to emulate the hash table reader at least in
> function context, by defining a custom DEFUN-wrapping macro that
> dispatches either to FUNCALL or to GETHASH depending on the data type.

I don't think it is that easy...

> Is this also possible for the SETF expansion? I think it should be,
> but I hardly ever use the DEFSETF facilities so I'm not sure how.

>   Leslie

--
http://lispm.dyndns.org/

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 14 2008, 7:37 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Sun, 14 Dec 2008 04:37:36 -0800 (PST)
Local: Sun, Dec 14 2008 7:37 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 12:01 pm, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:
> Clojure, from what I've gathered, lets one access hash tables just by
> referring to them in the CAR of an evaluated form:

> (defparameter ht (make-hash-table))

> (ht 'foo) == (gethash 'foo ht)

Actually I feel that as too verbose:
(ht 'foo)
->foo-val
would be even better.
also I want below too for multiple keys selection:
(ht '(foo bar baz))
-> (foo-val bar-val baz-val)
and possibly even this:
(ht (lambda (key) (like (princ-to-string key) "*-bar"))))

bobi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joswig@corporate-world.li sp.de  
View profile  
 More options Dec 14 2008, 8:01 am
Newsgroups: comp.lang.lisp
From: "jos...@corporate-world.lisp.de" <jos...@corporate-world.lisp.de>
Date: Sun, 14 Dec 2008 05:01:59 -0800 (PST)
Local: Sun, Dec 14 2008 8:01 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 1:37 pm, Slobodan Blazeski <slobodan.blaze...@gmail.com>
wrote:

If you want to go that route, why not make $ a default hash-table
variable and $foo accesses it.
Also write $ht.foo to access hash table ht. Then make $h.f!3 set the
hash table h, key f to 3.
Not that it would look like Lisp, but it sure saves keystrokes.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 14 2008, 9:00 am
Newsgroups: comp.lang.lisp
From: "Alex Mizrahi" <udode...@users.sourceforge.net>
Date: Sun, 14 Dec 2008 16:00:44 +0200
Local: Sun, Dec 14 2008 9:00 am
Subject: Re: Clojure hash table accessors in CL
 SB> also I want below too for multiple keys selection:
 SB> (ht '(foo bar baz))
 SB> -> (foo-val bar-val baz-val)

this is a delirium. what if you actually want to lookup key '(foo bar baz)?
i have keys that are lists all the time


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jochen Schmidt  
View profile  
 More options Dec 14 2008, 9:07 am
Newsgroups: comp.lang.lisp
From: Jochen Schmidt <j...@crispylogics.com>
Date: Sun, 14 Dec 2008 06:07:59 -0800 (PST)
Local: Sun, Dec 14 2008 9:07 am
Subject: Re: Clojure hash table accessors in CL
On 14 Dez., 12:01, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:

It's not that easy. The problem is, that there is no way to override
the syntax of a cl funtion call. You can either have a symbol denoting
a function or a lambda expression. You can extend the function call
protocol though: By implementing an hashtable or array as a
funcallable-instance. You could then use FUNCALL on the hashtable:

(funcall *ht* 1)

The missing part could be a macro which enables writing single-
namespace lisp code:

(defmacro with-single-namespace ((&rest fns) &body forms)
   `(macrolet (,@(mapcar (lambda (fn) `(,fn (&rest args)
                                       `(funcall ,',fn ,@,'args)))
fns))
                  ,@forms))

(with-single-namespace (*ht*)
  (+ (*ht* 1) (*ht* 2)))

-> (+ (funcall *ht* 1) (funcall *ht* 2))

Another solution could be:

(defmacro with-hashtables ((&rest hts) &body forms)
  `(macrolet (,@(mapcar (lambda (ht) `(,ht (key)
                                        `(gethash ,,'key ,',ht)))
hts))
     ,@forms))

(defmacro with-arrays ((&rest as) &body forms)
   `(macrolet (,@(mapcar (lambda (a) `(,a (&rest args)
                                       `(aref ,',a ,@,'args))) as))
                  ,@forms))

Or as generalization of this:

(defmacro with-collections ((&rest cs) &body forms)
   `(macrolet (,@(mapcar (lambda (c) `(,c (&rest args)
                                       `(element ,',c ,@,'args)))
cs))
                  ,@forms))

(defmethod element ((ht hash-table) &rest keys)
  (if (rest keys)
    (apply #'element (gethash (first keys) ht) (rest keys))
  (gethash (first keys) ht)))

(defmethod element ((a array) &rest keys)
  (apply #'aref a keys))

(defun alist->ht (alist &key (test 'eql))
  (loop with ht = (make-hash-table :test test)
        for (key . value) in alist do (setf (gethash key ht) value)
        finally (return ht)))

(defun plist->ht (plist &key (test 'eql))
  (loop with ht = (make-hash-table :test test)
        for (key value) on plist by #'cddr do (setf (gethash key ht)
value)
        finally (return ht)))

(let ((ht (alist->ht `((a . ,(alist->ht '((1 . "Foo")(2 . "Bar")(3 .
"Baz"))))
                       (b . ,(alist->ht '((1 . "FOO")(2 . "BAR")(3 .
"BAZ")))))))
      (a #(a b c d e f))
      (mix (plist->ht `(a #(p q r) b #(x y z)))))
  (with-collections (ht a mix) (values (ht 'b 3) (a 2) (mix 'b 1))))

==>
"BAZ"
C
Y

--
Jochen Schmidt
CRISPYLOGICS
Uhlandstr. 9, 90408 Nuremberg

Fon +49 (0)911 517 999 82
Fax +49 (0)911 517 999 83

mailto:(format nil "~(~36r@~36r.~36r~)" 870180 1680085828711918828
16438) http://www.crispylogics.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 14 2008, 9:32 am
Newsgroups: comp.lang.lisp
From: "Alex Mizrahi" <udode...@users.sourceforge.net>
Date: Sun, 14 Dec 2008 16:32:29 +0200
Local: Sun, Dec 14 2008 9:32 am
Subject: Re: Clojure hash table accessors in CL
 LPP> Clojure, from what I've gathered, lets one access hash tables just by
 LPP> referring to them in the CAR of an evaluated form:

 LPP> (defparameter ht (make-hash-table))

 LPP> (ht 'foo) == (gethash 'foo ht)
 LPP> (setf (ht 'foo) t) == (setf (gethash 'foo ht) t)

 LPP> It should be easy to emulate the hash table reader at least in
 LPP> function context, by defining a custom DEFUN-wrapping macro that
 LPP> dispatches either to FUNCALL or to GETHASH depending on the data type.

 LPP> Is this also possible for the SETF expansion? I think it should be,
 LPP> but I hardly ever use the DEFSETF facilities so I'm not sure how.

it is fairly easy to do this for custom binding contstructs. something like
this:

(defmacro defhash (name value)
 `(progn (defparameter ,name ,value)
            (defun ,name (key) (gethash key ,name))
            (defun (setf ,name) (value key) (setf (gethash key ,name)
value))))

(defmacro lethash ((name value) &body body)
 `(let ((,name ,value))
   (macrolet ((,name (key) `(gethash ,key ,,name)))
    ,@body)))

if you want to use normal lisp binding constructs, that becomes more tricky.
you can do this for special variables one way or another. but for lexical
bindings
that becomes really hard -- basically you need to modify all binding
constructs
and inject macros via macrolet that will deal with the case where variable
name
is used like a function. handling setf wil be even more problematic..
but there is actually have little sense -- essentially that will be not a
Common Lisp
but some new Lisp-1 dialect [*]. perhaps then just port Clojure (or Arc or
whatever) to
 Common Lisp..

*: consider such case:

 (defun foo (x)
   (let ((foo (make-hash-table)))
       (foo x)))

does (foo x) mean a function call or ht lookup? the only way to solve
ambiguity is
to have single namespace.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
André Thieme  
View profile  
 More options Dec 14 2008, 11:25 am
Newsgroups: comp.lang.lisp
From: André Thieme <address.good.until.2009.may...@justmail.de>
Date: Sun, 14 Dec 2008 17:25:49 +0100
Local: Sun, Dec 14 2008 11:25 am
Subject: Re: Clojure hash table accessors in CL
Slobodan Blazeski schrieb:

> On Dec 14, 12:01 pm, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:
>> Clojure, from what I've gathered, lets one access hash tables just by
>> referring to them in the CAR of an evaluated form:

>> (defparameter ht (make-hash-table))

>> (ht 'foo) == (gethash 'foo ht)
> Actually I feel that as too verbose:
> (ht 'foo)
> ->foo-val
> would be even better.

Let’s say we have two hashmaps:
(def h1 {:name "Slobodan" :foo 100 :language "Lisp"})
(def h2 {:name "Slodoban" :foo 200 :language "Perl"})

(print :foo-val)  ==> will it print 100 or 200?
(print (:foo h1)) ==> prints 100 and is not dramatically much verbose.

> also I want below too for multiple keys selection:
> (ht '(foo bar baz))
> -> (foo-val bar-val baz-val)

Something similar is available in Clojure:
user> (let [{:keys [foo name]} h2]
         [(inc foo) name])
==>
[201 "Slodoban"]

or
user> [(:foo h2) (h2 :name)]
[200 "Slodoban"]

When keywords (symbols with a : in the front) were used then these
keywords act as getter function. But one can in general also have the
collection object first and then the key:
([10 20 30] 1) ==> 20

But your suggestion of (ht '(foo bar baz)) is really not good, as Alex
already mentioned. People want to have the list (foo bar baz) as the key
for their hash map.

> and possibly even this:
> (ht (lambda (key) (like (princ-to-string key) "*-bar"))))

This would not be very orthogonal to what is already available and thus
is not a good idea.
And how could we decide if it will map over the keys or the values or
the key/value pairs?

Why not just (map #(like (str %) "*-bar") (keys ht)) to get all keys
that end with "-bar"?
Or instead of map you could
(first (filter #(like (str %) "*-bar") (keys ht)))

André
--
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leslie P. Polzer  
View profile  
 More options Dec 14 2008, 1:12 pm
Newsgroups: comp.lang.lisp
From: "Leslie P. Polzer" <leslie.pol...@gmx.net>
Date: Sun, 14 Dec 2008 10:12:06 -0800 (PST)
Local: Sun, Dec 14 2008 1:12 pm
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 3:07 pm, Jochen Schmidt <j...@crispylogics.com> wrote:

> It's not that easy. The problem is, that there is no way to override
> the syntax of a cl funtion call. You can either have a symbol denoting
> a function or a lambda expression.

Why not?

Consider a syntax transformation via code walker that does something
like

  (fn-or-ht arg)

  =>

  (if (typep fn-or-ht 'hash-table)
     (gethash fn-or-ht arg)
     (funcall fn-or-ht arg))


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rainer Joswig  
View profile  
 More options Dec 14 2008, 1:21 pm
Newsgroups: comp.lang.lisp
From: Rainer Joswig <jos...@lisp.de>
Date: Sun, 14 Dec 2008 19:21:23 +0100
Local: Sun, Dec 14 2008 1:21 pm
Subject: Re: Clojure hash table accessors in CL
In article
<79727957-cc4c-40b0-842d-8a07646c6...@x16g2000prn.googlegroups.com>,
 "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:

CL-USER 4 > (flet ((fn-or-ht (foo) foo))
  (if (typep fn-or-ht 'hash-table)
      (gethash fn-or-ht arg)
    (funcall fn-or-ht arg)))

Error: The variable FN-OR-HT is unbound.
  1 (continue) Try evaluating FN-OR-HT again.
  2 Specify a value to use this time instead of evaluating FN-OR-HT.
  3 Specify a value to set FN-OR-HT to.
  4 (abort) Return to level 0.
  5 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 5 : 1 > :top

CL-USER 6 > (flet ((fn-or-ht (foo) foo))
  (if (typep fn-or-ht 'hash-table)
      (gethash fn-or-ht arg)
    (funcall (function fn-or-ht) arg)))

Error: The variable FN-OR-HT is unbound.
  1 (continue) Try evaluating FN-OR-HT again.
  2 Specify a value to use this time instead of evaluating FN-OR-HT.
  3 Specify a value to set FN-OR-HT to.
  4 (abort) Return to level 0.
  5 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 7 : 1 >

--
http://lispm.dyndns.org/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Mizrahi  
View profile  
 More options Dec 14 2008, 2:06 pm
Newsgroups: comp.lang.lisp
From: "Alex Mizrahi" <udode...@users.sourceforge.net>
Date: Sun, 14 Dec 2008 21:06:36 +0200
Local: Sun, Dec 14 2008 2:06 pm
Subject: Re: Clojure hash table accessors in CL
 ??>> It's not that easy. The problem is, that there is no way to override
 ??>> the syntax of a cl funtion call. You can either have a symbol denoting
 ??>> a function or a lambda expression.

 LPP> Why not?

because it is not Scheme.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jerryboe...@mac.com  
View profile  
 More options Dec 14 2008, 2:48 pm
Newsgroups: comp.lang.lisp
From: jerryboe...@mac.com
Date: Sun, 14 Dec 2008 11:48:00 -0800 (PST)
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 9:32 am, "Alex Mizrahi" <udode...@users.sourceforge.net>
wrote:

> *: consider such case:

>  (defun foo (x)
>    (let ((foo (make-hash-table)))
>        (foo x)))

> does (foo x) mean a function call or ht lookup? the only way to solve
> ambiguity is
> to have single namespace.

Yes, and what about this...

(setq car (make-hash-table))

Treating as a symbol is fine
  (setf (gethash 'a car) 'quux)

(gethash 'a car) => quux

But what happens with

(car '(foo or quux)) ?

It clashes with the CL:CAR function. Is the compiler supposed to
understand that the value of the symbol is a hashtable hence
effectively producing (gethash ...)? Is the value of CAR (value is
changeable) now the hash table? Yes, probably. And what is the value
of the function slot? (car '(a b c)) => a or does it now have
#'gethash. But what happens if it finds (funcall car 'a).

IMHO, I think this 'extension' is cute, useful in certain
circumstances, and not well thought-out. Better to make a macro as
noted above. If the generated function (embedded gethash) closes over
the hash table, then it's not dependent on the value of the symbol.
But allowing DEFPARAMETER or even SETQ strikes me as confusing and not
worth the time.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Taylor  
View profile  
 More options Dec 14 2008, 2:59 pm
Newsgroups: comp.lang.lisp
From: Joshua Taylor <joshuaaa...@gmail.com>
Date: Sun, 14 Dec 2008 11:59:00 -0800 (PST)
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 6:01 am, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:

I don't know if it would be considered "easy", but you might be
interested at a blog post by Geoff Wozniak on Exploring Lisp. It's
about funcallable objects in general.

http://exploring-lisp.blogspot.com/2007/05/objects-as-functions.html

This doesn't provide an entire solution, since you'd still need to
write a hash table implementation. In your own package, though, you
could shadow the CL hash table functions and provide the same
interface though your own symbols (make-hash-table, gethash, …).

There are still some issue with the idea though, since CL is a Lisp-2,
you end up having to do things such as those in Geoff's post, such as:

(defun f () nil)
(setf (symbol-function 'f)
      (make-instance 'setl-map :pairs '((1 . 100) (2 . 200))))

so that (f …) will be a call to the correct object, but you still
can't do things like:

(let ((f (make-funcallable-hash-table …)))
  (f …))

and be calling the local funcallable object. Instead you'd have to:

(let ((f (make-funcallable-hash-table …)))
  (funcall f …))

but (funcall f …) isn't really any more concise than (gethash f …), so
we're right back where we started.

//JT


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vsevolod  
View profile  
 More options Dec 14 2008, 3:02 pm
Newsgroups: comp.lang.lisp
From: Vsevolod <vselo...@gmail.com>
Date: Sun, 14 Dec 2008 12:02:49 -0800 (PST)
Local: Sun, Dec 14 2008 3:02 pm
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 1:01 pm, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:

You can achieve it with a sort of such simple macro as the following
(a naive implementation without handling of parameters of make-hash-
table):
(defmacro def-hash-table (name)
  `(progn (defparameter ,name (make-hash-table))
               (defun ,name (key &optional default)
                  (gethash key ,name default))))
which uses Lisp's separation of namespaces.

PS. But, in my opinion, Lisp-n is better, because you virtually always
know, what you are dealing with -- a var or a function.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
William James  
View profile  
(2 users)  More options Dec 14 2008, 6:27 pm
Newsgroups: comp.lang.lisp
From: "William James" <w_a_x_...@yahoo.com>
Date: 14 Dec 2008 23:27:16 GMT
Local: Sun, Dec 14 2008 6:27 pm
Subject: Re: Clojure hash table accessors in CL

Slobodan Blazeski wrote:
> On Dec 14, 12:01 pm, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:
> > Clojure, from what I've gathered, lets one access hash tables just
> > by referring to them in the CAR of an evaluated form:

> > (defparameter ht (make-hash-table))

> > (ht 'foo) == (gethash 'foo ht)
> Actually I feel that as too verbose:
> (ht 'foo)
> ->foo-val
> would be even better.

Still too verbose.

JavaScript:

js> h = {foo: 77, bar: 88}
[object Object]
js> h.foo
77
js> h.bar
88

> also I want below too for multiple keys selection:
> (ht '(foo bar baz))
> -> (foo-val bar-val baz-val)

Ruby:

h = {:foo, "foo_val", :bar, "bar_val", :baz, "baz_val"}
    ==>{:baz=>"baz_val", :foo=>"foo_val", :bar=>"bar_val"}
h.values_at( :foo, :bar, :baz )
    ==>["foo_val", "bar_val", "baz_val"]

> and possibly even this:
> (ht (lambda (key) (like (princ-to-string key) "*-bar"))))

h.keys.map{|x| x.to_s}.grep(/^ba/)
    ==>["baz", "bar"]
h.select{|key,val| key.to_s.match(/^ba/) }
    ==>[[:baz, "baz_val"], [:bar, "bar_val"]]
h.values.grep( /^ba/ )
    ==>["baz_val", "bar_val"]

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Dec 14 2008, 7:14 pm
Newsgroups: comp.lang.lisp
From: Kaz Kylheku <kkylh...@gmail.com>
Date: Mon, 15 Dec 2008 00:14:35 +0000 (UTC)
Local: Sun, Dec 14 2008 7:14 pm
Subject: Re: Clojure hash table accessors in CL
On 2008-12-14, William James <w_a_x_...@yahoo.com> wrote:

> Still too verbose.

Like you.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal J. Bourguignon  
View profile  
 More options Dec 14 2008, 7:45 pm
Newsgroups: comp.lang.lisp
From: p...@informatimago.com (Pascal J. Bourguignon)
Date: Mon, 15 Dec 2008 01:45:19 +0100
Local: Sun, Dec 14 2008 7:45 pm
Subject: Re: Clojure hash table accessors in CL

André Thieme <address.good.until.2009.may...@justmail.de> writes:
> Slobodan Blazeski schrieb:
>> On Dec 14, 12:01 pm, "Leslie P. Polzer" <leslie.pol...@gmx.net> wrote:
>>> Clojure, from what I've gathered, lets one access hash tables just by
>>> referring to them in the CAR of an evaluated form:

>>> (defparameter ht (make-hash-table))

>>> (ht 'foo) == (gethash 'foo ht)
>> Actually I feel that as too verbose:
>> (ht 'foo)
>> ->foo-val
>> would be even better.

> Let’s say we have two hashmaps:

It's quite incredible.  Why people aren't trying to add syntaxes to
languages such as pascal or C++ to be able to access maps or vectors
as if was a function call,  but periodically we get asked this kind of
crapiness here?

Please, just ignore these questions, until we see them asked in clc++ or
similarly in other cl* groups:

    What can be done to be able to write:

        int f(){
           std::map<std::string,int> m;
           m("toto")=42;
           return m("toto");
        }

    in C++?

--
__Pascal Bourguignon__


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
D Herring  
View profile  
 More options Dec 14 2008, 10:06 pm
Newsgroups: comp.lang.lisp
From: D Herring <dherr...@at.tentpost.dot.com>
Date: Sun, 14 Dec 2008 22:06:58 -0500
Local: Sun, Dec 14 2008 10:06 pm
Subject: Re: Clojure hash table accessors in CL

Pascal J. Bourguignon wrote:
>     What can be done to be able to write:

>         int f(){
>            std::map<std::string,int> m;
>            m("toto")=42;
>            return m("toto");
>         }

This is generally written as

int f(){
    std::map<std::string,int> m;
    m["toto"]=42;
    return m["toto"];

}

This works because of the following prototype:
mapped_type & operator[](const key_type &);

Although they could have chosen to define it as
mapped_type & operator()(const key_type &);

- Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vsevolod  
View profile  
 More options Dec 15 2008, 3:03 am
Newsgroups: comp.lang.lisp
From: Vsevolod <vselo...@gmail.com>
Date: Mon, 15 Dec 2008 00:03:18 -0800 (PST)
Local: Mon, Dec 15 2008 3:03 am
Subject: Re: Clojure hash table accessors in CL
On Dec 15, 5:06 am, D Herring <dherr...@at.tentpost.dot.com> wrote:

Well, is it possible to extend such syntax -- [] -- to handle, for
example, multiple keys access at once (as mentioned above in the
thread) or default value? AFAIK with [] -- not, because it's hardwired
to take only 1 argument. Although, you can do it with (). But once
again, can this accessor be extended to handle variable number of
keys?..

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 15 2008, 3:13 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Mon, 15 Dec 2008 00:13:54 -0800 (PST)
Local: Mon, Dec 15 2008 3:13 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 3:00 pm, "Alex Mizrahi" <udode...@users.sourceforge.net>
wrote:

>  SB> also I want below too for multiple keys selection:
>  SB> (ht '(foo bar baz))
>  SB> -> (foo-val bar-val baz-val)

> this is a delirium. what if you actually want to lookup key '(foo bar baz)?
> i have keys that are lists all the time

Excellent question Alex. It could be easily solved with adding more
syntax or no agregate keys. I will have to think about it.

bobi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 15 2008, 3:22 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Mon, 15 Dec 2008 00:22:37 -0800 (PST)
Local: Mon, Dec 15 2008 3:22 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 2:01 pm, "jos...@corporate-world.lisp.de" <jos...@corporate-

Because I need a general solution. Every instance container is in the
same time a function that takes a key, suit of keys or a predicate.
(setq lst (list 1 2 3))
(setq arr (array 1 2 3))

(lst 1)
2
(arr 1)
2
(lst 0 2)
(1 3)
(arr 0 2)
(1 3)

bobi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 15 2008, 3:43 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Mon, 15 Dec 2008 00:43:36 -0800 (PST)
Local: Mon, Dec 15 2008 3:43 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 5:25 pm, André Thieme <address.good.until.

Braces, no way. That's too much syntax already I'm thinking of using
() for point-free definitions but that already looks ugly.

Good question. If I have a list :
(setq lst (list 1 2 3 4 5))
and I do:
(lst #'oddp)
what should the result be?
(1 3 5)
So the predicate loops over container values. For looping over keys
there should be something else.

> Why not just (map #(like (str %) "*-bar") (keys ht)) to get all keys
> that end with "-bar"?
> Or instead of map you could

 I don't understand below example
> (first (filter #(like (str %) "*-bar") (keys ht)))

1st we agreed it's over values.   But let's *PRETEND* it's over keys
Whats' more understandable:
(map #'predicate (keys ht)) or
(ht #'predicate)

2nd map is generalized operator it could operate over multiple
containers:
(map #'> '(1 2 3) '(2 3 4))
while  (container key(s)) or (container predicate) is a current
container accessor.

bobi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 15 2008, 3:50 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Mon, 15 Dec 2008 00:50:05 -0800 (PST)
Local: Mon, Dec 15 2008 3:50 am
Subject: Re: Clojure hash table accessors in CL
On Dec 15, 9:13 am, Slobodan Blazeski <slobodan.blaze...@gmail.com>
wrote:

> On Dec 14, 3:00 pm, "Alex Mizrahi" <udode...@users.sourceforge.net>
> wrote:

> >  SB> also I want below too for multiple keys selection:
> >  SB> (ht '(foo bar baz))
> >  SB> -> (foo-val bar-val baz-val)

> > this is a delirium. what if you actually want to lookup key '(foo bar baz)?
> > i have keys that are lists all the time

> Excellent question Alex. It could be easily solved with adding more
> syntax or no agregate keys. I will have to think about it.

> bobi

Solved  it, &rest is my friend :)
(ht  '(foo bar baz)  1 'foo)
->(foo-bar-baz-value 1-value foo-value)

I love design by newsgroup.

bobi
P.S.
This is all for my language, edi. I don't  want this added to common
lisp.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slobodan Blazeski  
View profile  
 More options Dec 15 2008, 4:20 am
Newsgroups: comp.lang.lisp
From: Slobodan Blazeski <slobodan.blaze...@gmail.com>
Date: Mon, 15 Dec 2008 01:20:59 -0800 (PST)
Local: Mon, Dec 15 2008 4:20 am
Subject: Re: Clojure hash table accessors in CL
I don't like replying to trolls, but since this question is of much
interest to me I'll. Thank you Kaz for making this reply visible to
me.
On Dec 15, 12:27 am, "William James" <w_a_x_...@yahoo.com> wrote:

h.bar  - token count 2

That's slightly better for accessing  a single element, token count is
the same 2 but you don't have to use braces instead have a dot, but
how would you access more elements or use a predicate WITHOUT
breaking the consistency NOR adding new tokens?

> > also I want below too for multiple keys selection:
> > (ht '(foo bar baz))
> > -> (foo-val bar-val baz-val)

> Ruby:

> h = {:foo, "foo_val", :bar, "bar_val", :baz, "baz_val"}
>     ==>{:baz=>"baz_val", :foo=>"foo_val", :bar=>"bar_val"}
> h.values_at( :foo, :bar, :baz )
>     ==>["foo_val", "bar_val", "baz_val"]

This is where your solutions flops. You're unable to access multiple
elements with the dot syntax  so you're forced into adding a function.
h.values_at(...) - 2 tokens just for syntax plus 2 braces ignoring the
keys
(h ...)          - only one token plus 2 braces,ignoring the keys
I win.

> > and possibly even this:
> > (ht (lambda (key) (like (princ-to-string key) "*-bar"))))

> h.keys.map{|x| x.to_s}.grep(/^ba/)

Lets's see:
h.*VALUES?*.map{predicate}- 3 tokens  plus 2 braces ignoring the
predicate
(h predicate) - 1 token plus 2 braces ignoring the predicate

See your cheap ruby tricks doesn't scale well. You already losing 3:1
Now the real challenge?
Given ht : keys 1 2 3 '(1 2)   :values 1-val 2-val 3-val 1-2-val
(h 1 2 '(1 2) (lambda (x) (and (numberp x) (oddp x))))
-> (1-val 2-val 1-2-val (1-val 3-val))

Your move.

bobi


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leslie P. Polzer  
View profile  
 More options Dec 15 2008, 4:43 am
Newsgroups: comp.lang.lisp
From: "Leslie P. Polzer" <leslie.pol...@gmx.net>
Date: Mon, 15 Dec 2008 01:43:52 -0800 (PST)
Local: Mon, Dec 15 2008 4:43 am
Subject: Re: Clojure hash table accessors in CL
On Dec 14, 7:21 pm, Rainer Joswig <jos...@lisp.de> wrote:

> Error: The variable FN-OR-HT is unbound.

CL-USER(36): (defmacro with-hts (form)
  `(if (and (boundp ',(car form)) (typep ,(car form) 'hash-table))
     (gethash ,(cadr form) (symbol-value (find-symbol ,(princ-to-
string (car form)) *package*)))
     (funcall ',(car form) ,(cadr form))))

WITH-HTS
CL-USER(37): (defparameter ht (make-hash-table))

HT
CL-USER(39): (setf (gethash 2 ht) t)

T
CL-USER(40): (with-hts (ht 2))

T
T
CL-USER(41): (with-hts (ht 1))

NIL
NIL
CL-USER(42): (with-hts (identity 1))

1

This is what I had in mind.

I know it doesn't work with hash tables from other packages, but it's
just a demo.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 54   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google