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 2Results
| average of 10, 20, 30 | → | 20 |
| average of 1, 2, 3, 4, 5 | → | 3 |
| square root of 16 | → | 4 |
| square root of 2 | → | 1.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_gigResults
| read 100 MB from ssd | → | 0.1818 second |
| read 1 GB from nvme | → | 0.2926 second |
| read 500 MB from hdd | → | 3.33 second |
| read 10 GB from pcie_ssd | → | 1.46 second |
| compress 1 GB using gzip | → | 341 MB |
| compress 500 MB using lz4 | → | 250 MB |
| compress 2 GB using zstd | → | 585 MB |
| compress 100 MB using snappy | → | 40 MB |
| transfer 1 GB across regional gigabit | → | 8.2 second |
| transfer 500 MB across global wifi | → | 40.2 second |
| transfer 100 MB across local ten_gig | → | 0.0805 second |
| transfer 10 GB across continental hundred_gig | → | 0.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 gigabitResults
| data = 1 GB | → | 1 GB |
| compress data using gzip | → | 341 MB |
| read data from ssd | → | 1.86 second |
| transfer data across regional gigabit | → | 8.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 $5000Results
| 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 months | → | 200 |
| 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 = 10 | → | 10 |
| y = 20 | → | 20 |
| z = 30 | → | 30 |
| a = 5 | → | 5 |
| b = 4 | → | 4 |
| 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 9Results
| avg(sqrt(16), sqrt(25)) | → | 4.5 |
| sqrt(avg(1, 2, 3)) | → | 1.414214 |
| average of square root of 4, square root of 9 | → | 2.5 |
Functions in Assignments #
mean = avg(10, 20, 30)
root = sqrt(16)
calculated = average of 100, 200, 300
side = square root of 25Results
| mean = avg(10, 20, 30) | → | 20 |
| root = sqrt(16) | → | 4 |
| calculated = average of 100, 200, 300 | → | 200 |
| side = square root of 25 | → | 5 |
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 == total2Results
| total1 = avg(1, 2, 3) | → | 2 |
| total2 = average of 1, 2, 3 | → | 2 |
| same = total1 == total2 | → | true |
What This Demonstrates #
- Traditional
fn(args)and natural languagename of argssyntax - Multi-argument
read...from,compress...using,transfer...acrosspatterns (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