I am kind of a minimalist, and one thing I'm trying to do is define a cheat sheet for basic syntax and types of Kopi. Given that a person knows JS and the basics of Haskell/ML, would you understand how to write a small program in Kopi with this?
Syntax
1 + 2 * 3 Basic math (== 7)
s = "Hello" Basic assignment
[a, b] = "ab" Pattern matching
f (x) = x * x Function definition
(x) => x * x Anonymous function
print "Hello" Function application
5 | toFixed 2 Invoking a method
'(toFixed 2) 5 Alternate invocation
(1, 2).0 Tuple index access
(x: 1, y: 2).x Tuple field access
array.(3) Array index access
array.(1..5) Array slice access
"ab" ++ "cd" Concatenation operator
x < 0 ? -x : x Conditional expression
{ Multiple statements
x = 5
print x + 1
}
Some of these are not currently supported in the newest Kopi, but will be soon (array access, conditional, array pattern).
Types
1 -2.5 3.14 Number + – * / % == != < > <= >= succ
"Hello, world" String ++ == != < > <= >= .() size succ
true false Boolean == != !
'ident '(+ 1) ASTree apply
(x) => x * x Function apply
1..5 "a".."z" Range .from .to .by toArray apply
(1, '2, "abc") Tuple .N map
(x: 1, y: 2) Record .field map
[1, 2, 3, 4] Array .(N) ++ size
Elements can also be separated by newline, and tuples/record syntax mixed. "ASTree" is not my favorite type name, but it is an "expression tree" in C# parlance.
Functionslet (n = 5) => { Looping
print n
loop (n - 1)
}
match n ( Matching
0 => 1
n => n * n
)
Other things to write about:
Pattern matching in depth
Method pipelining
100% Asynchronous
Operator precedence
Dictionary type
More Examples
My previous version based on the previous version of Kopi: