Arc Forumnew | comments | leaders | submitlogin
Proposal: concise function definition
1 point by rincewind 5849 days ago | 3 comments
This is currently impossible to implement with defset, but it seems be worth a try or a discussion:

  make            (= (f x) (expr x))
  expand into     (= f (fn (x) (expr x)))
when f has no setter.


1 point by almkglor 5848 days ago | link

  (= f (table))
  (= k 1)

  (= (f k) (+ k 1))
More:

  (def set-the-table (k)
    (= (tb k) (+ k 1)))

  (= tb (table))
-------

file1.arc:

  ; throwaway file I'm playing around with

  ; define some temporary function
  (= (foo x)
     (+ x 1))
file2.arc:

  ; throwaway file I'm playing around with
  (= foo (table))
  (for i 0 3
    (= (foo i) i))
What I do on the command line:

  (load "file2.arc")
  (load "file1.arc")

-----

1 point by stefano 5848 days ago | link

For global functions, I find the usual 'def to be more concise. As of for local functions, it could be quite handy, but possibilities of collision with names defined by 'defset seem quite high to me. I would prefer something similar to CL's 'labels:

  (flet (f (x) (expr x))
    whatever...)
and

  (fwith ((f (x) (expr x))
          (g (x) (f x)))
    whatever...)

-----

1 point by cchooper 5848 days ago | link

Should probably expand to:

  (= f (rfn 'f (x) (expr x)))
I like it because it's consistent with setting variables, but I also agree with Stefano. If only there were a way of distinguishing the two cases.

-----