Data flows left to right.
A pipe passes the value on its left as the first argument on its right. A block with x is a lambda.
A pipe passes the value on its left as the first argument on its right. A block with x is a lambda.
let result = [5, 2, 8, 1]
| Array.filter({ x > 2 })
| Array.map({ x * 10 })
| Array.sort()
println(result)[50, 80]A function that can fail returns a Result. try hands an error up to the caller. Nothing is thrown.
func load(path: string): Result[Json.Value, Error] {
let text = try File.read(path)
Json.parse(text)
}
let config = load("config.json")
if config is Err { println(config.error.message) }open config.json: no such file or directoryGoroutines are cheap, and any function can start one. Parallel.map runs a function over a list on goroutines, at most limit at once, and returns when all of them are done.
func slowSquare(n: int, ctx: Context): int {
Time.sleep(100ms, ctx)
n * n
}
let squares = Parallel.map([1, 2, 3, 4], slowSquare, limit: 3)
println(squares)[1, 4, 9, 16]Markup is a value of type Html, checked like any other expression. Text interpolated into it is escaped, so a page is safe by default.
type Post = { title: string, url: string }
func link(post: Post): Html {
<a href={post.url}>{post.title}</a>
}
let post = Post{ title: "Hello <world>", url: "/hello" }
println(<p>{link(post)}</p>)<p><a href="/hello">Hello <world></a></p>A union lists the types a value can have. is tells which one it holds, and inside the branch its fields are there to use.
type Card = { last4: string }
type Cash = { change: int }
type Payment = Card | Cash
func receipt(p: Payment): string {
if p is Card { "card ending ${p.last4}" }
else { "cash, ${p.change} back" }
}
println(receipt(Card{ last4: "4242" }))
println(receipt(Cash{ change: 5 }))card ending 4242
cash, 5 backOne binary checks, formats, tests, and documents Ant, and teaches your editor all of it.
ant check
A sound type checker: if ant check passes, the types hold when it runs. When it fails, it points at the exact characters and suggests a fix.
$ ant check greet.ant
greet.ant:2:13: nmae is not defined [undefined]
│
2 │ "Hello, ${nmae}"
│ ^^^^
= help: did you mean name?
1 errorant check
Some mistakes are perfectly valid code. The checker catches those too, says what goes wrong, and offers the fix.
$ ant check save.ant
save.ant:2:1: warning: the Result of File.write is discarded, so its Err is lost [unused-result]
│
2 │ File.write("report.txt", report)
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: propagate it with try, or discard it on purpose with _ = ...
= fix: _ = ...
1 warningant fmt
One style, with no options. Every editor formats on save.
func area(w:int,h :int):int{
w*h }func area(w: int, h: int): int {
w * h
}ant test
Tests sit beside the code they test. A failed assert prints both sides.
$ ant test
--- FAIL: area_test.ant: area of a strip (0.00s)
assertion failed: area(1, 4) == 5 (4 == 5)
at test "area of a strip" (area_test.ant:10:9)
FAIL 1 failed, 1 passed (0.00s)ant lsp · ant dap
The language server brings completion, hover, go to definition, rename, and diagnostics to VS Code, IntelliJ, and Neovim. The debugger sets breakpoints and shows values.
ant code
What the checker knows, on the command line: types, definitions, and references, for scripts and agents.
$ ant code type geo.ant:7:5
let origin: Point
$ ant code def geo.ant:8:9
geo.ant:3:6: func norm(p: Point): int
$ ant code refs geo.ant:3:6
geo.ant:8:9: println(norm(origin))
1 reference in 1 fileant
Run ant on its own to try an idea, with highlighting, completion, and history.
$ ant
> let a = [3, 1, 2] | Array.sort()
> a
[1, 2, 3]
> "total: ${Array.sum(a)}"
total: 6ant doc
Docs for any name, in the terminal, from the standard library or your own code.
$ ant doc String.split
func String.split(s: string, sep: string): Array[string]
String.split returns the pieces of s between each occurrence of sep. It panics if sep is empty.
String.split("a,b,,c", ",") // ["a", "b", "", "c"]
String.split("", ",") // [""]