Arc Forumnew | comments | leaders | submitlogin
4 points by almkglor 5924 days ago | link | parent

This gets exceedingly complicated if we want to implement, say, 2-d matrices:

  (= matrix
    (let matdata '((1 0)
                   (0 1))
      (fn (v (o j nil)) ; ugly hack
          (if (is v settersym) (fn (val i j) (= ((matdata j) i) val))
              ((matdata j) v)))))
> Attaching data to functions is something I've wanted to do for a long time, so I support the general idea, but I enjoy the challenge of doing it with macros :)

Yes, but then it becomes non-standard. That's why I said "Request PG", hopefully it should be standardized into = forms.



1 point by jivestgarden 5923 days ago | link

It is actually not complicated at all if one just defines a function (let us call it matrix) that returns settable functions. Then one could write code like this

  (let m (matrix '((1 0) (0 1)))
     (= (m 0 1) 42))
Similar things could (and in my opinion: should) also be done for each, len, and other basic functions and macros to enable iterations through, and manipulation of, stateful functions.

-----

1 point by almkglor 5923 days ago | link

Someone still has to write the function 'matrix, and it still has to look like that.

-----

2 points by jivestgarden 5922 days ago | link

It´s not so difficult as it looks: is is just a function that returns a function that returns a function. If you are used to object orientation, think of matrix as instansiation, m as an object, and the returned setter as a method. With the arc abbreviations, you could also write (m.settersym val 0 0) rather than having to modify =, but personally I would like to have some setter macro to make the syntax more natural (By the way: this is exactly how you would do a matrix implmentation in C++, only with templates in stead of macros)

-----

2 points by almkglor 5922 days ago | link

I am concerned about how it looks, which is my main objection to this.

-----

1 point by nex3 5922 days ago | link

You could always define a macro to abstract out the nastiness...

-----

1 point by almkglor 5922 days ago | link

^^ Again, somebody has to write the nastiness ^^. The main difficulty really is the fact that you have a polymorphic function that dispatches based on number of parameters as well as their contents.

Although I suppose you could actually use --warning-blatant-self-proclamation-- my p-m: macro:

  (p-m:def matrix
    (,(s (is s settersym)))
      writerfunction
    (i j)
      (readerfunction i j)
    x (err "argument error"))

-----