Arc Forumnew | comments | leaders | submitlogin
1 point by thaddeus 5838 days ago | link | parent

no worries, it's not a real problem I have to solve. I'm just trying to figure out what options/techniques are available.

Thanks. T.



3 points by conanite 5838 days ago | link

app* is a nested structure something like this:

  app*
    yardwork
      budget
      forecast
    house
      budget
        roof = 1996.0
        windows = 1000.0
      forecast
        roof = 1996.0
        windows = 1800.0
And you want a clean way to access, for example, (((app* 'house) 'budget) 'roof), given 'house and 'roof as parameters.

Am I right so far?

An alternative definition of area-situation might look like this:

  (def area-situation (area sub-area)
    (with (budget   ((app* area) 'budget)
           forecast ((app* area) 'forecast))
      (if (is (- (budget sub-area) (forecast sub-area)) 0)
        (= (budget sub-area) 200))
      (prn "forecast " (forecast sub-area) " budget " (budget sub-area))))
You can use 'with to create variables pointing just to the two tables you need and then there's less digging to do when you need to look up or alter values.

You might get better mileage by changing the data structure though if that's possible:

  app*
    yardwork ...
    house
      roof
        budget = 1996.0
        forecast = 1996.0
      windows
        budget = 1000.0
        forecast = 1800.0
Then, area-situation becomes even simpler:

  (def area-situation (area sub-area)
    (let situation ((app* area) sub-area)
      (if (is (- (situation 'budget) (situation 'forecast)) 0)
        (= (situation 'budget) 200))
      (prn "forecast " (situation 'forecast) " budget " (situation 'budget))))
Arc doesn't really have "pointers" (at least not in the c/c++ sense) - you either have global variables, or local (lexically-scoped) variables. So you can create a local variable pointing to the innermost table using 'let or 'with. Oh, I said "pointing" - well, they're kinda pointers.

Does that point you in the right direction, or did I miss the point entirely?

btw with the hot new special-syntax you can write app.area.sub-area instead of ((app area) sub-area) - see http://arclanguage.org/item?id=9220

-----

1 point by thaddeus 5838 days ago | link

wow!

i didn't have an appreciation for the 'hot new special-syntax' until I compared my original hack against your last function with the infixing....

    (def area-situation (area sub-area)
        (let situation app*.area.sub-area
          (if (is (- situation.budget situation.forecast) 0.0)
            (= situation.budget 200)
          (prn "forecast " situation.forecast " budget " situation.budget))))
This is awesome!

T.

-----

1 point by thaddeus 5838 days ago | link

    *Am I right so far?*
yep.

I have to spend more time using 'with'; it's not a well well used tool in my tool shed as it should be.

and your last solution looks great to me.

thanks. T.

-----

2 points by fallintothis 5838 days ago | link

Accepting the hypothetical that there's not a clear-cut way to simplify the data structures (hey, it happens), I can't think of much to do. I know what it is you want to do, as the problem does crop up from time to time -- trying to make use of the expression instead of the value of an expression. An obvious way to solve this would be to introduce a macro, e.g.

  (= app* (table))
  
  (deftem cost-sheet  ;or whatever.  probably a bad name.
    budget   (table)
    forecast (table))
  
  (= (app* 'house) (inst 'cost-sheet 'budget   (obj roof 1996.0 windows 1000.0)
                                     'forecast (obj roof 1996.0 windows 1800.0)))
  
  (mac budget (area sub-area)
    `(((app* ,area) 'budget) ,sub-area))
  
  (def area-situation (area sub-area)
    (withs (area-budget   (((app* area) 'budget)   sub-area)
            area-forecast (((app* area) 'forecast) sub-area)
            delta         (- area-forecast area-budget))
      (when (is delta 0.0)
        (= (budget area sub-area) 200))
      (prn "forecast " area-forecast "budget " area-budget)))
But this isn't a very palatable solution -- you'd need to define macros for each field, and even then they're only shortcuts, really. It'd be easier if there were a shorter way to do nested indexing (which there will be, come arc3: http://arclanguage.org/item?id=9163).

Upon writing this and finding someone else replying, I notice that conanite's post http://arclanguage.org/item?id=9326 contains probably the best solutions, though.

-----

1 point by thaddeus 5838 days ago | link

I like the mac idea too. if the concepts like "budget" and "forecast" are the main data concepts of the program; then I can see being able to re-use the macro a lot. Also I have to read up on 'deftem' and 'inst'.

thanks, T.

-----