Is there one way to define one formula to be used in other places?

I want to something like define in the normal programming language so that if we do (define test1 (lambda (x) (* x x))) (using Scheme syntax because I am learning that with SICP as one programming beginner), then we can do something like WithName(thisRow.val1, x, test1).

I tried to put the formula like Switch(x, forbidden, List("test1","test2"), List()) in one text Control with Name “test1”, but that won’t recognize x as one variable (similar for forbidden which may be one row in one table). Is there one workaround?

1 Like

alas, no. it has been a request from this community for a long time.

but it is possible to define named procedures for use in Action Formulas (ie. buttons and automations).

we can do this by creating a named button that executes the computations we require, and then invoke that button from within other action formulas.

let me illustrate that with an example; a procedure that computes the length of a 2d line.

the formula we want to define is…
D=squareroot( square( x2-x1 ) + square( y2-y1 )

lets assume that the parameters x1, x2, y1, y2, and the result LENGTH are columns in a table called LINES (its the most convenient way)

then we create a button called DISTANCE which has the following code

LINES.last().modifyrows(
    LENGTH, squareroot( power( x2 - x1 ,2) + power( y2 - y1, 2) )
)

then anywhere we want to invoke this procedure we write the following code

runactions(
    LINES.addrow(  // set the parameters for the line
        x1, pointA.x,
        y1, pointA.y,
        x2, pointB.x,
        x2, pointB.x,
        y2, pointB.y
    ),
DISTANCE  // computes the LENGTH
)

this contrived example shows one way to create and use a ‘named procedure’ with parameters in CFL. for this trivial example, it may seem like more trouble than its worth.

but for more complicated business logic, it is a great way to abstract and encapsulate the logic inside a single procedure where it can be maintained easily and invoked from many places.

(btw: scheme is a GREAT language to learn and gives you a deep insight into how the Coda Formula Language (CFL) works - they are very similar in their ways internally.)

respect
max

2 Likes

Thanks. I read your long answer briefly. IMHO here the difference is that “named procedures” here can only refer to determined variables, i.e. x1 in this row etc, since it only makes sense in the table. But the requested procedure can have x depending on the usage env.

1 Like

well, there is an alternative a way to define a named procedure which might be closer to what you suggest…

you can name a procedure in an action formula using the let() function.
it is not evaluated when you are defining it.
but it is evaluated whenever you refer to the named valiable.

i feel it might be possible to extend this to also support the passing of parameters in the way you would like…

lets see how it looks.

// define proc P1  
RunActions(
    text2.SetControlValue(text2+1)
).let(P1,

// use P1 in a loop  
RunActions(
    text2.SetControlValue(0), //reset counter
    Sequence(1,1000).ForEach(  // loop 1000 times
        P1  // invoke proc P1
    ) // next loop
  )
)

so this defines a trivial procedure P1
that increments a the counter TEXT2

then we have a loop for 1000 iterations
that invokes the P1 procedure by name

it works great. but P1 is only available inside the one action formula and cannot be invoked from elsewhere.

but… it may be possible to extend this technique to pass arguments in a way that matches what you are looking for … i just have not found it yet

Max

Yes.
What I actually wants is just as Scheme language does, i.e. having one formula as one single object which can be used anywhere. Thanks anyway.

1 Like

well, i have built a Visual Basic compiler that creates actions from VBA script, and i have made versions for javascript and python.

i do have an incomplete Clojure compiler (which is similar to Scheme and LISP) but the client ghosted me before it was finished.

because they are compilers, they ARE able to support syntactical and semantical operations not available in CFL… they just build the necessary stacks and control variables needed to implement things in a more basic way.

so, if you are interested, we could work on a project to try to get this working in coda; but the technique requires the use of the hidden formulas - so the resulting documents will not be supported by coda support.

now, it is always possible to get around this issue by generating the text for the CFL code and manually pasting it into your CFL editor.

but the better solution is likely to support Scheme using a pack.
and thats a much more challenging and complicated project!

1 Like

Thanks. I haven’t learnt about implementation of compiler systematically (scheduled to do that in later self learning). I just learnt some about assembly language which is related with compiler when learning about computer architecture.


I assumed that you mean pack by both quotes. That is based on “JavaScript or TypeScript” as What are Packs? - Coda Pack SDK says probably due to it’s initially developed for web and then the rest platforms. That is similar to VSCode based on Electron. I may try doing that when I have learnt necessary programming knowledge.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.