fork/numbat

git clone https://git.julia.blue/fork/numbat

David PeterRename speed => velocitydc1eeeb

v1.8.0
3.6 KiB107 linesraw
1# This is a line comment. It can span over
2# multiple lines
3
4# 1. Imports
5
6use prelude        # This is not necessary. The 'prelude'
7                   # module will always be loaded upon startup
8
9use units::stoney  # Load a specific module
10
11# 2. Numbers
12
1312345       # integer notation
1412_345      # optional decimal separators
15
160.234       # floating point notation
17.234        # without the leading zero
18
191.234e15    # scientific notation
201.234e+15
211e-9
221.0e-9
23
240x2A        # hexadecimal
250o52        # octal
260b101010    # binary
27
28# 3. Simple expressions
29
303 + (4 - 3)       # Addition and subtraction
31
321920 / 16 * 9     # Multiplication, division
331920 ÷ 16 × 9     # Unicode-style, '·' is also multiplication
342 pi              # Whitespace is implicit multiplication
35meter per second  # 'per' keyword can be used for division
36
372^3               # Exponentiation
382**3              # Python-style
392³                # Unicode exponents
402^-3              # Negative exponents
41
42mod(17, 4)        # Modulo
43
443 in -> cm        # Unit conversion, can also be → or ➞
453 in to cm        # Unit conversion with the 'to' keyword
46
47cos(pi/3 + pi)    # Call mathematical functions
48pi/3 + pi // cos  # Same, 'arg // f' is equivalent to 'f(arg)'
49                  # The '//' operator has the lowest precedence
50                  # which makes it very useful for interactive
51                  # terminals (press up-arrow, and add '// f')
52
53# 4. Constants
54
55let n = 4                          # Simple numerical constant
56let q1 = 2 m/s                     # Right hand side can be any expression
57let q2: Velocity = 2 m/s           # With optional type annotation
58let q3: Length / Time = 2 m/s      # more complex type annotation
59
60# 5. Function definitions
61
62fn foo(z: Scalar) -> Scalar = 2 * z + 3                   # A simple function
63fn speed(len: Length, dur: Time) -> Velocity = len / dur  # Two parameters
64fn my_sqrt<T>(q: T^2) -> T = q^(1/2)                      # A generic function
65fn is_non_negative(x: Scalar) -> Bool = x ≥ 0             # Returns a bool
66
67# 6. Dimension definitions
68
69dimension Fame                            # A new base dimension
70dimension Deceleration = Length / Time^2  # A new derived dimension
71
72# 7. Unit definitions
73
74@aliases(quorks)                 # Optional aliases-decorator
75unit quork = 0.35 meter          # A new derived unit
76
77@metric_prefixes                 # Optional decorator to allow 'milliclonk', etc.
78@aliases(ck: short)              # short aliases can be used with short prefixes (mck)
79unit clonk: Time = 0.2 seconds   # Optional type annotation
80
81@metric_prefixes
82@aliases(wh: short)
83unit warhol: Fame                # New base unit for the "Fame" dimension
84
85unit thing                       # New base unit with automatically generated
86                                 # base dimension "Thing"
87
88# 8. Conditionals
89
90fn step(x: Scalar) -> Scalar =   # The construct 'if <cond> then <expr> else <expr>'
91  if x < 0                       # is an expression, not a statement. It can span
92    then 0                       # multiple lines.
93    else 1
94
95# 9. Procedures
96
97print(2 kilowarhol)              # Print the value of an expression
98print("hello world")             # Print a message
99print("value of pi = {pi}")      # String interpolation
100print("sqrt(10) = {sqrt(10)}")   # Expressions in string interpolation
101
102assert(1 yard < 1 meter)         # Assertion
103
104assert_eq(1 ft, 12 in)           # Assert that two quantities are equal
105assert_eq(1 yd, 1 m, 10 cm)      # Assert that two quantities are equal, up to
106                                 # the given precision
107type(2 m/s)                      # Print the type of an expression