Skip to content

Commit 2c6cb0e

Browse files
committed
Update The-Expression-Syntax.md
1 parent e7ea534 commit 2c6cb0e

File tree

1 file changed

+53
-39
lines changed

1 file changed

+53
-39
lines changed

docs/The-Expression-Syntax.md

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
The Expr package uses a specific syntax. In this document, you can find all supported
1+
# The Expression Syntax
2+
3+
**Expr** package uses a specific syntax. In this document, you can find all supported
24
syntaxes.
35

46
## Supported Literals
57

68
The package supports:
79

810
* **strings** - single and double quotes (e.g. `"hello"`, `'hello'`)
9-
* **numbers** - e.g. `103`, `2.5`
10-
* **arrays** - using JSON-like notation (e.g. `[1, 2]`)
11-
* **maps** - using JSON-like notation (e.g. `{ foo: "bar" }`)
11+
* **numbers** - e.g. `103`, `2.5`, `1e+6`
12+
* **arrays** - e.g. `[1, 2]`
13+
* **maps** - e.g. `{foo: "bar"}`
1214
* **booleans** - `true` and `false`
1315
* **nil** - `nil`
1416

@@ -19,28 +21,27 @@ access properties.
1921

2022
Public properties on structs can be accessed by using the `.` syntax:
2123

22-
```
24+
```coffeescript
2325
Foo.Bar.Baz
2426
```
2527

2628
## Working with Functions
2729

28-
You can also use pass functions into the expression and call them using C syntax.
30+
You can also use functions by calling them using C syntax.
2931

30-
```
32+
```coffeescript
3133
Upper("text")
3234
```
3335

3436
Result will be set to `HELLO`.
3537

36-
Note: `Upper` function doesn't present in package by default.
37-
38+
> Note: `Upper` function doesn't present in package by default.
3839
3940
## Working with Arrays
4041

41-
If you pass an array into an expression, use the `[]` syntax to access values:
42+
Use the `[]` syntax to access values:
4243

43-
```
44+
```coffeescript
4445
array[2]
4546
```
4647

@@ -59,17 +60,9 @@ The package comes with a lot of operators:
5960

6061
Example:
6162

62-
```
63+
```coffeescript
6364
life + universe + everything
64-
```
65-
66-
> Note what result will be `float64` as all binary operators what works with numbers, cast to `float64` automatically.
67-
68-
### Bitwise Operators
69-
70-
* `&` (and)
71-
* `|` (or)
72-
* `^` (xor)
65+
```
7366

7467
### Comparison Operators
7568

@@ -79,15 +72,6 @@ life + universe + everything
7972
* `>` (greater than)
8073
* `<=` (less than or equal to)
8174
* `>=` (greater than or equal to)
82-
* `matches` (regex match)
83-
84-
To test if a string does *not* match a regex, use the logical `not` operator in combination with the `matches` operator:
85-
86-
```
87-
not ("foo" matches "bar")
88-
```
89-
90-
You must use parenthesis because the unary operator `not` has precedence over the binary operator `matches`.
9175

9276
### Logical Operators
9377

@@ -103,11 +87,21 @@ life < universe or life < everything
10387

10488
### String Operators
10589

106-
* `~` (concatenation)
90+
* `+` (concatenation)
91+
* `matches` (regex match)
92+
* `contains` (string contains)
93+
94+
To test if a string does *not* match a regex, use the logical `not` operator in combination with the `matches` operator:
95+
96+
```coffeescript
97+
not ("foo" matches "^bar")
98+
```
99+
100+
You must use parenthesis because the unary operator `not` has precedence over the binary operator `matches`.
107101

108102
Example:
109103

110-
```go
104+
```coffeescript
111105
'Arthur' ~ ' ' ~ 'Dent'
112106
```
113107

@@ -120,11 +114,11 @@ Result will be set to `Arthur Dent`.
120114

121115
Example:
122116

123-
```
117+
```coffeescript
124118
User.Group in ["human_resources", "marketing"]
125119
```
126120

127-
```
121+
```coffeescript
128122
"foo" in {foo: 1, bar: 2}
129123
```
130124

@@ -134,29 +128,49 @@ User.Group in ["human_resources", "marketing"]
134128

135129
Example:
136130

137-
```
131+
```coffeescript
138132
User.Age in 18..45
139133
```
140134

141135
The range is inclusive:
142136

143-
```
137+
```coffeescript
144138
1..3 == [1, 2, 3]
145139
```
146140

147141
### Ternary Operators
148142

149143
* `foo ? 'yes' : 'no'`
150-
* `foo ?: 'no'` (equal to `foo ? foo : 'no'`)
151144

152145
## Builtin functions
153146

154147
* `len` (length of array or string)
148+
* `all` (will return `true` if all element satisfies the predicate)
149+
* `none` (will return `true` if all element does NOT satisfies the predicate)
150+
* `any` (will return `true` if any element satisfies the predicate)
151+
* `one` (will return `true` if exactly ONE element satisfies the predicate)
152+
* `filter` (filter array by the predicate)
153+
* `map` (map all items with the closure)
155154

156155
Example:
157156

157+
```coffeescript
158+
// Ensure all tweets are less than 140 chars.
159+
all(Tweets, {.Size < 140})
158160
```
159-
len([1, 2, 3]) == len("foo")
161+
162+
## Closure
163+
164+
* `{...}` (closure)
165+
166+
**Expr** support closures with builtin functions. To access current item use `#` symbol.
167+
168+
```go
169+
map(0..9, {# + 1})
160170
```
161171

162-
Result will be set to `true`.
172+
If the item of array is struct, it's possible to access fields of struct with omitted `#` symbol (`#.Value` becomes `.Value`).
173+
174+
```go
175+
filter(Tweets, {.Size > 140})
176+
```

0 commit comments

Comments
 (0)