Compare commits

...

2 Commits

Author SHA1 Message Date
32b54c402f Fix example; Write README 2023-11-28 11:38:40 -05:00
69d7b4b1db Fix example 2023-11-28 11:05:54 -05:00
3 changed files with 37 additions and 46 deletions

View File

@ -28,7 +28,17 @@ if (random_boolean) {
}
```
Dust is an interpreted, strictly typed language with first class functions. It emphasises concurrency by allowing any group of statements to be executed in parallel. Dust includes built-in tooling to import and export data in a variety of formats, including JSON, TOML, YAML and CSV.
Dust enforces strict type checking to make sure your code is correct. Dust does *not* have a null type.
```dust
fib = |i <int>| <int> {
if i <= 1 {
1
} else {
(fib i - 1) + (fib i - 2)
}
}
```
<!--toc:start-->
- [Dust](#dust)
@ -42,9 +52,9 @@ Dust is an interpreted, strictly typed language with first class functions. It e
- [Lists](#lists)
- [Maps](#maps)
- [Loops](#loops)
- [Tables](#tables)
- [Functions](#functions)
- [Concurrency](#concurrency)
- [Acknowledgements](#acknowledgements)
<!--toc:end-->
## Features
@ -140,7 +150,6 @@ Variables have two parts: a key and a value. The key is always a string. The val
- boolean
- list
- map
- table
- function
Here are some examples of variables in dust.
@ -207,56 +216,31 @@ for number in list {
}
```
### Tables
Tables are strict collections, each row must have a value for each column. If a value is "missing" it should be set to an appropriate value for that type. For example, a string can be empty and a number can be set to zero. Dust table declarations consist of a list of column names, which are identifiers enclosed in pointed braces, followed by a list of rows.
An **async for** loop will run the loop operations in parallel using a thread pool.
```dust
animals = table <name species age> [
["rover" "cat" 14]
["spot" "snake" 9]
["bob" "giraffe" 2]
]
```
Querying a table is similar to SQL.
```dust
names = select name from animals
youngins = select species from animals {
age <= 10
async for i in [1 2 3 4 5 6 7 8 9 0] {
(output i)
}
```
The keywords `table` and `insert` make sure that all of the memory used to hold the rows is allocated at once, so it is good practice to group your rows together instead of using a call for each row.
```dust
insert into animals [
["eliza" "ostrich" 4]
["pat" "white rhino" 7]
["jim" "walrus" 9]
]
(assert_equal 6 (length animals))
```
### Functions
Functions are first-class values in dust, so they are assigned to variables like any other value.
```dust
# This simple function has no arguments.
say_hi = || => {
# This simple function has no arguments and no return type.
say_hi = || {
(output "hi")
}
# This function has one argument and will return a value.
add_one = |number| => {
# This function has one argument and will return an integer.
add_one = |number| <int> {
number + 1
}
(say_hi)
(assert_equal (add_one 3), 4)
(assert_equal 4 (add_one 3))
```
You don't need commas when listing arguments and you don't need to add whitespace inside the function body but doing so may make your code easier to read.
@ -274,6 +258,8 @@ async {
}
```
If the final statement in an async block creates a value, the block will return that value just like in a normal block.
```dust
data = async {
(output "Reading a file...")
@ -281,7 +267,7 @@ data = async {
}
```
### Acknowledgements
## Acknowledgements
Dust began as a fork of [evalexpr]. Some of the original code is still in place but the project has dramatically changed and no longer uses any of its parsing or interpreting.

View File

@ -1,6 +1,8 @@
async {
cast = (download "https://api.sampleapis.com/futurama/cast")
characters = (download "https://api.sampleapis.com/futurama/characters")
episodes = (download "https://api.sampleapis.com/futurama/episodes")
}
cast_len = (length (from_json cast))
characters_len = (length (from_json characters))

View File

@ -1,20 +1,23 @@
find = |list function| => {
for i in list {
find = |items <list> function <fn>| <any> {
for i in items {
if (function i) {
return i
}
}
}
map = |list function| => {
map = |items <list> function <fn>| <list> {
new_list = []
for i in list {
for i in items {
new_list += (function i)
}
new_list
}
[0 1 2] -> (map |i| => { i - 1})
-> (find |i| => { i == 1 })
foobar <int> = [0 1 2]
-> (map |i <int>| <int> { i - 1 })
-> (find |i <int>| <bool> { i == -1 })
foobar