Functions & Natural Language

All function call styles: traditional, natural language, nested, and mixed.

From testdata/eval/success/features/functions.cm, testdata/eval/success/features/natural_language.cm, and testdata/eval/success/features/growth_functions.cm.

Traditional Function Syntax #

avg(10, 20, 30)
avg(1, 2, 3, 4, 5)
sqrt(16)
sqrt(2)
Results
avg(10, 20, 30)20
avg(1, 2, 3, 4, 5)3
sqrt(16)4
sqrt(2)1.414214

Natural Language Function Syntax #

average of 10, 20, 30
average of 1, 2, 3, 4, 5
square root of 16
square root of 2
Results
average of 10, 20, 3020
average of 1, 2, 3, 4, 53
square root of 164
square root of 21.414214

read…from / compress…using / transfer…across #

read 100 MB from ssd
read 1 GB from nvme
read 500 MB from hdd
read 10 GB from pcie_ssd

compress 1 GB using gzip
compress 500 MB using lz4
compress 2 GB using zstd
compress 100 MB using snappy

transfer 1 GB across regional gigabit
transfer 500 MB across global wifi
transfer 100 MB across local ten_gig
transfer 10 GB across continental hundred_gig
Results
read 100 MB from ssd0.1818 second
read 1 GB from nvme0.2926 second
read 500 MB from hdd3.33 second
read 10 GB from pcie_ssd1.46 second
compress 1 GB using gzip341 MB
compress 500 MB using lz4250 MB
compress 2 GB using zstd585 MB
compress 100 MB using snappy40 MB
transfer 1 GB across regional gigabit8.2 second
transfer 500 MB across global wifi40.2 second
transfer 100 MB across local ten_gig0.0805 second
transfer 10 GB across continental hundred_gig0.8692 second

NL Functions with Variable References #

NL functions accept variables in place of literal quantities:

data = 1 GB
compress data using gzip
read data from ssd
transfer data across regional gigabit
Results
data = 1 GB1 GB
compress data using gzip341 MB
read data from ssd1.86 second
transfer data across regional gigabit8.2 second

Growth Functions: compound…by…over / grow…by…over / depreciate…by…over #

# Compound growth - functional
compound($1000, 5%, 10)
compound(500 customers, 20%, 12)
compound($1000, 5%, 10 years, monthly)
compound($1000, 5%, 10 years, quarterly)

# Compound growth - natural language
compound $1000 by 5% over 10 years
compound $1000 by 5% monthly over 10 years
compound $1000 by 5% per month over 12 months

# Linear growth - functional
grow($500, $100, 36)
grow(100, 20, 5)

# Linear growth - natural language
grow 100 by 20 over 5 months

# Depreciation - functional
depreciate($50000, 15%, 5)
depreciate($50000, 15%, 20, $5000)

# Depreciation - natural language
depreciate $50000 by 15% over 5 years
depreciate $50000 by 15% over 5 years to $5000
Results
compound($1000, 5%, 10)$1,628.89
compound(500 customers, 20%, 12)4.46K customers
compound($1000, 5%, 10 years, monthly)$1,647.01
compound($1000, 5%, 10 years, quarterly)$1,643.62
compound $1000 by 5% over 10 years$1,628.89
compound $1000 by 5% monthly over 10 years$1,647.01
compound $1000 by 5% per month over 12 months$1,795.86
grow($500, $100, 36)$4,100.00
grow(100, 20, 5)200
grow 100 by 20 over 5 months200
depreciate($50000, 15%, 5)$22.19K
depreciate($50000, 15%, 20, $5000)$5,000.00
depreciate $50000 by 15% over 5 years$22.19K
depreciate $50000 by 15% over 5 years to $5000$22.19K

Functions with Expressions #

x = 10
y = 20
z = 30
a = 5
b = 4

avg(x, y, z)
avg(10 + 5, 20 * 2, 30 - 10)
sqrt(x + y)
square root of (a + b)
Results
x = 1010
y = 2020
z = 3030
a = 55
b = 44
avg(x, y, z)20
avg(10 + 5, 20 * 2, 30 - 10)25
sqrt(x + y)5.477226
square root of (a + b)3

Nested Functions #

avg(sqrt(16), sqrt(25))
sqrt(avg(1, 2, 3))
average of square root of 4, square root of 9
Results
avg(sqrt(16), sqrt(25))4.5
sqrt(avg(1, 2, 3))1.414214
average of square root of 4, square root of 92.5

Functions in Assignments #

mean = avg(10, 20, 30)
root = sqrt(16)
calculated = average of 100, 200, 300
side = square root of 25
Results
mean = avg(10, 20, 30)20
root = sqrt(16)4
calculated = average of 100, 200, 300200
side = square root of 255

Mixed Syntax in Same Document #

Traditional syntax:
total1 = avg(1, 2, 3)

Natural language syntax:
total2 = average of 1, 2, 3

Both should produce same result:
same = total1 == total2
Results
total1 = avg(1, 2, 3)2
total2 = average of 1, 2, 32
same = total1 == total2true

What This Demonstrates #

  • Traditional fn(args) and natural language name of args syntax
  • Multi-argument read...from, compress...using, transfer...across patterns (with literals or variables)
  • Growth functions: compound...by...over, grow...by...over, depreciate...by...over
  • Expressions and variables as function arguments
  • Nested function calls
  • Assignment from function results
  • Both syntaxes produce identical results