numericValue vs simpleFunction

Computing values in CL requires a bit more than entering simple arithmetic (e.g. 1+1 won’t get you anywhere). For computations in CL, we primarily use numericValue and simpleFunction. Here’s what each does:

  • numericValue takes a string and computes it’s numeric value
  • simpleFunction creates a function using any number of variables that can be evaluated at different values

When do I use numericValue vs simpleFunction?

The difference in use between the two functions boils down to a few simple points. Here’s when to use each:

numericValue

  • The computation only needs to be done once.
  • You either know exactly what numbers are being computed, or if inputs are variables, you know all of the possible inputs.

simpleFunction

  • The computation is repeated several times.
  • The inputs are highly variable.

For simple or non repeated calculations, numericValue is much simpler to write:

numericValue(`1+2`)
simpleFunction(`x+y`,`x`,`y`).evaluateAt(1,2)

The more you repeat the calculation, especially with lengthly computations, simpleFunction becomes more and more useful:

fn = simpleFunction(`x^{2}+6x+9`,`x`)
evaluate1= fn.evaluatAt(1)
evaluate2= fn.evaluatAt(10)
evaluate3= fn.evaluatAt(100)
evaluate4= fn.evaluatAt(1000)
evaluate5= fn.evaluatAt(10000)

Additionally, numericValue can break mysteriously when given really big or really small numbers:

One more thing…

If you want to take it one step further, check out evaluationFrame! This function allows you to make changes or combine parts of a calculation in a different way without rewriting the whole thing.

Step 1: Add your functions

frame = evaluationFrame()
  .define(`f`,simpleFunction(`x+1`,`x`))
  .define(`g`,simpleFunction(`2x`,`x`))
  .define(`h`,simpleFunction(`x^{2}`,`x`))

Step 2: evaluate your functions

Code Function Output
.evaluate(`f(g(h(3)`) 2(3)^{2}+1 19
.evaluate(`g(h(f(3)`) 2(3+1)^{2} 32
.evaluate(`h(g(f(3)`) (2(3+1))^{2} 64
.evaluate(`f(h(g(3)`) (2\cdot3)^{2}+1 37
1 Like