News for hackers. Stuff that matters.
Re:The Universal Design Pattern by silentbicycle on Monday October 20, 2008 @01:12PM (#338062)
Ditto with Lisp, actually. Maybe something good could be done with read macros, but |(table 'key) -> (lookup table 'key) just doesn't do it for me. Any ideas? Lisp has alists, but (as noted elsewhere) they search in linear time.

Python, Lua, and JS all have syntactic sugar for maps, and I think it makes a really big difference. They're quite versatile. Lua's table.key -> table["key"] sugar for all string keys is particularly nice.

Re:The Universal Design Pattern by jimbokun on Monday October 20, 2008 @01:42PM (#338096)
Clojure has squiggly brace syntax for map literals, and multiple shortcuts for map lookups. E.g. both (map :key) or (:key map) return the value for :key in map (I think).

Arc has similar syntax shortcuts for hashes (but sticks with parentheses):

http://ycombinator.com/arc/tut.txt

    arc> (let h (obj x 1 y 2)
           (h 'y))
    2
Re:The Universal Design Pattern by gcv on Monday October 20, 2008 @02:52PM (#338186)
Don't know if this will do what you want, but these two reader macros add syntactic sugar for Common Lisp's aref and gethash forms. You should put these definitions in an eval-when. Disclaimer: I haven't used these in a while (not programming in Lisp at the moment, alas), so they may have some limitations which I have forgotten.

  (set-macro-character
    #\] (get-macro-character #\)))

  ;; Array syntax: [array-name array-references] is equivalent to
  ;; (aref array-name array-references)
  (set-macro-character
    #\[
    #'(lambda (stream char)
        (declare (ignore char))
        (destructuring-bind (array-name &rest array-references)
            (read-delimited-list #\] stream t)
          `(aref ,array-name ,@array-references))))

  ;; Hash table syntax: #[hash-table hash-key &optional default] is
  ;; equivalent to (gethash hash-key hash-table &optional default)
  (set-dispatch-macro-character
    #\# #\[ #'(lambda (stream subchar arg)
                (declare (ignore subchar arg))
                (destructuring-bind (hash-name hash-key &optional (default nil))
                    (read-delimited-list #\] stream t)
                  `(gethash ,hash-key ,hash-name ,default))))
Re:The Universal Design Pattern by silentbicycle on Monday October 20, 2008 @03:18PM (#338212)
Actually, would there be any real problem with keeping the map in a closure, and passing around a function that gets (or, with a second optional argument, sets) the value for a given key?

  (h 'key) and (h 'key 'new-val)
That's not too bad syntax-wise. Or, taking a cue from actor languages:

  (h 'get 'key) and (h 'set 'key 'new-val)
Re:The Universal Design Pattern by amix on Monday October 20, 2008 @03:29PM (#338224)
JavaScript also supports obj.id == obj["id"]. Python on the other hand does not support this (which is a shame, because obj.id is more readeable than obj['id']). But it's possible to hack this on dicts ( http://pastie.org/296506 ).

Syntactic sugar in languages is quite convenient, especially for things that are used a lot. Thought a lot of syntactic sugar (like in Perl) can ruin a language and it's readability.

Re:The Universal Design Pattern by silentbicycle on Monday October 20, 2008 @04:33PM (#338291)
I think syntactic sugar can make some things so convenient that people find novel uses for them. The ranged slicing operations in Python are really nice, for example; so is templating Lisp code with ` , ,@ once you get used to it.

Adding SS also adds to what you need to remember to use the language effectively (and to read other peoples' code), but a little bit of syntactic sugar can go a really long way.