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
2
4
syntaxes.
3
5
4
6
## Supported Literals
5
7
6
8
The package supports:
7
9
8
10
* ** 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"} `
12
14
* ** booleans** - ` true ` and ` false `
13
15
* ** nil** - ` nil `
14
16
@@ -19,28 +21,27 @@ access properties.
19
21
20
22
Public properties on structs can be accessed by using the ` . ` syntax:
21
23
22
- ```
24
+ ``` coffeescript
23
25
Foo .Bar .Baz
24
26
```
25
27
26
28
## Working with Functions
27
29
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.
29
31
30
- ```
32
+ ``` coffeescript
31
33
Upper (" text" )
32
34
```
33
35
34
36
Result will be set to ` HELLO ` .
35
37
36
- Note: ` Upper ` function doesn't present in package by default.
37
-
38
+ > Note: ` Upper ` function doesn't present in package by default.
38
39
39
40
## Working with Arrays
40
41
41
- If you pass an array into an expression, use the ` [] ` syntax to access values:
42
+ Use the ` [] ` syntax to access values:
42
43
43
- ```
44
+ ``` coffeescript
44
45
array[2 ]
45
46
```
46
47
@@ -59,17 +60,9 @@ The package comes with a lot of operators:
59
60
60
61
Example:
61
62
62
- ```
63
+ ``` coffeescript
63
64
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
+ ```
73
66
74
67
### Comparison Operators
75
68
@@ -79,15 +72,6 @@ life + universe + everything
79
72
* ` > ` (greater than)
80
73
* ` <= ` (less than or equal to)
81
74
* ` >= ` (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 ` .
91
75
92
76
### Logical Operators
93
77
@@ -103,11 +87,21 @@ life < universe or life < everything
103
87
104
88
### String Operators
105
89
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 ` .
107
101
108
102
Example:
109
103
110
- ``` go
104
+ ``` coffeescript
111
105
' Arthur' ~ ' ' ~ ' Dent'
112
106
```
113
107
@@ -120,11 +114,11 @@ Result will be set to `Arthur Dent`.
120
114
121
115
Example:
122
116
123
- ```
117
+ ``` coffeescript
124
118
User .Group in [" human_resources" , " marketing" ]
125
119
```
126
120
127
- ```
121
+ ``` coffeescript
128
122
" foo" in {foo : 1 , bar : 2 }
129
123
```
130
124
@@ -134,29 +128,49 @@ User.Group in ["human_resources", "marketing"]
134
128
135
129
Example:
136
130
137
- ```
131
+ ``` coffeescript
138
132
User .Age in 18 ..45
139
133
```
140
134
141
135
The range is inclusive:
142
136
143
- ```
137
+ ``` coffeescript
144
138
1 ..3 == [1 , 2 , 3 ]
145
139
```
146
140
147
141
### Ternary Operators
148
142
149
143
* ` foo ? 'yes' : 'no' `
150
- * ` foo ?: 'no' ` (equal to ` foo ? foo : 'no' ` )
151
144
152
145
## Builtin functions
153
146
154
147
* ` 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)
155
154
156
155
Example:
157
156
157
+ ``` coffeescript
158
+ // Ensure all tweets are less than 140 chars.
159
+ all (Tweets, {.Size < 140 })
158
160
```
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 })
160
170
```
161
171
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