Skip to content

Commit 65a7980

Browse files
committed
Update docs for in operator and negative ranges
1 parent 6411b5a commit 65a7980

File tree

6 files changed

+273
-9
lines changed

6 files changed

+273
-9
lines changed

docs/ios/expressions.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ for i in loops {
197197
}
198198
```
199199

200+
You can also use the `in` operator to check if a range contains a particular value:
201+
202+
```swift
203+
define range 1 to 5
204+
205+
if 2.5 in range {
206+
print "range contains 2.5"
207+
}
208+
```
209+
200210
**Note:** Ranges are inclusive of both the start and end values, so a loop from `0 to 5` would loop *6* times and not 5 as you might expect.
201211

202212
Range values can be fractional and/or negative:
@@ -245,6 +255,17 @@ for i in 5 to 1 step -1 {
245255
}
246256
```
247257

258+
For stepped ranges, `in` will only return true for values that align with the steps:
259+
260+
```swift
261+
define range 1 to 5 step 2
262+
263+
print 1 in range // prints true
264+
print 2 in range // prints false
265+
print 2.5 in range // prints false
266+
print 3 in range // prints true
267+
```
268+
248269
## Members
249270

250271
Compound values like [vectors and tuples](literals.md#vectors-and-tuples) and [objects](literals.md#objects) can be decomposed by using the *dot* operator to access individual components or *members*:
@@ -349,7 +370,25 @@ for i in 0 to foo.count - 1 {
349370
}
350371
```
351372

352-
Trying to access elements outside the range `0 to count - 1` will result in an error.
373+
To access elements relative to the end of the tuple, you can use negative indices. An index of `-1` is shorthand for `count - 1`:
374+
375+
376+
```swift
377+
define foo 1 2 3 4
378+
379+
print foo[-1] // prints 4
380+
print foo[-2] // prints 3
381+
```
382+
383+
Trying to access tuple elements outside the range `-count to count - 1` will result in an error, as will attempting to access a named member that does not exist. To avoid the error you can check if a given member exists by using the `in` operator, as follows:
384+
385+
```swift
386+
define purple 0.6 0 1
387+
388+
if "red" in purple {
389+
print "the red value of purple is " purple["red"] // prints 0.6
390+
}
391+
```
353392

354393
---
355394
[Index](index.md) | Next: [Functions](functions.md)

docs/ios/literals.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,35 @@ define size 1 2 3
7070
define myTuple2 "hello" 5.3 size
7171
```
7272

73+
Tuple values can be accessed by index using [ordinal members](expressions.md#members) or [subscripting](expressions.md#subscripting):
74+
75+
```swift
76+
define size 1 2 3
77+
78+
print size.second // prints 2
79+
print size[0] // prints 1
80+
```
81+
82+
To check if a tuple contains a particular value, you can use the `in` operator:
83+
84+
```swift
85+
define values 1 2 3
86+
87+
if 2 in values {
88+
print "values includes the number 2"
89+
}
90+
```
91+
92+
You can also enumerate the values in a tuple using a [for loop](control-flow.md#looping-over-values):
93+
94+
```swift
95+
define values 1 2 3
96+
97+
for value in values {
98+
print value // prints 1 2 3
99+
}
100+
```
101+
73102
## Structured Data
74103

75104
As well as simple values like numbers and text, it can sometimes be useful to group together sets of related data. Tuples can be nested arbitrarily to create complex data structures:
@@ -213,11 +242,16 @@ define data object {
213242
height 10
214243
depth 15
215244
}
245+
```
246+
247+
To access the members of an object you can either use the [dot operator](expressions.md#members) or [subscripting](expressions.md#subscripting):
216248

249+
```swift
217250
print data.height // prints 10
251+
print data["height"] // also prints 10
218252
```
219253

220-
Objects can also be nested:
254+
Objects can be nested inside each other:
221255

222256
```swift
223257
define data object {
@@ -231,6 +265,21 @@ define data object {
231265
}
232266

233267
print data.dimensions.height // prints 10
268+
print data["dimensions"]["height"] // also prints 10
269+
```
270+
271+
Attempting to access a non-existent member of an object will cause an error. To check if an object contains a particular member before you try to access it, you can use the `in` operator:
272+
273+
```swift
274+
define axes object {
275+
x (1 0 0)
276+
y (0 1 0)
277+
z (0 0 1)
278+
}
279+
280+
if "x" in axes {
281+
print "axes object contains an x component"
282+
}
234283
```
235284

236285
To enumerate all the members of an object, you can use a [for loop](control-flow.md#looping-over-values). Each member is returned as a tuple of the key (member name) and value:
@@ -241,7 +290,7 @@ for row in data {
241290
}
242291
```
243292

244-
**Note:** object members are *unordered*, meaning that the order in which they are defined has no special significance. When looping through members of an object, the order will be alphabetical rather than the order in which the members were defined.
293+
**Note:** object members are *unordered*, meaning that the order in which they are defined has no special significance. When looping through members of an object, the order will be alphabetical rather than reflecting the order in which the members were defined.
245294

246295
---
247296
[Index](index.md) | Next: [Symbols](symbols.md)

docs/mac/expressions.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ for i in loops {
197197
}
198198
```
199199

200+
You can also use the `in` operator to check if a range contains a particular value:
201+
202+
```swift
203+
define range 1 to 5
204+
205+
if 2.5 in range {
206+
print "range contains 2.5"
207+
}
208+
```
209+
200210
**Note:** Ranges are inclusive of both the start and end values, so a loop from `0 to 5` would loop *6* times and not 5 as you might expect.
201211

202212
Range values can be fractional and/or negative:
@@ -245,6 +255,17 @@ for i in 5 to 1 step -1 {
245255
}
246256
```
247257

258+
For stepped ranges, `in` will only return true for values that align with the steps:
259+
260+
```swift
261+
define range 1 to 5 step 2
262+
263+
print 1 in range // prints true
264+
print 2 in range // prints false
265+
print 2.5 in range // prints false
266+
print 3 in range // prints true
267+
```
268+
248269
## Members
249270

250271
Compound values like [vectors and tuples](literals.md#vectors-and-tuples) and [objects](literals.md#objects) can be decomposed by using the *dot* operator to access individual components or *members*:
@@ -349,7 +370,25 @@ for i in 0 to foo.count - 1 {
349370
}
350371
```
351372

352-
Trying to access elements outside the range `0 to count - 1` will result in an error.
373+
To access elements relative to the end of the tuple, you can use negative indices. An index of `-1` is shorthand for `count - 1`:
374+
375+
376+
```swift
377+
define foo 1 2 3 4
378+
379+
print foo[-1] // prints 4
380+
print foo[-2] // prints 3
381+
```
382+
383+
Trying to access tuple elements outside the range `-count to count - 1` will result in an error, as will attempting to access a named member that does not exist. To avoid the error you can check if a given member exists by using the `in` operator, as follows:
384+
385+
```swift
386+
define purple 0.6 0 1
387+
388+
if "red" in purple {
389+
print "the red value of purple is " purple["red"] // prints 0.6
390+
}
391+
```
353392

354393
---
355394
[Index](index.md) | Next: [Functions](functions.md)

docs/mac/literals.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,35 @@ define size 1 2 3
7070
define myTuple2 "hello" 5.3 size
7171
```
7272

73+
Tuple values can be accessed by index using [ordinal members](expressions.md#members) or [subscripting](expressions.md#subscripting):
74+
75+
```swift
76+
define size 1 2 3
77+
78+
print size.second // prints 2
79+
print size[0] // prints 1
80+
```
81+
82+
To check if a tuple contains a particular value, you can use the `in` operator:
83+
84+
```swift
85+
define values 1 2 3
86+
87+
if 2 in values {
88+
print "values includes the number 2"
89+
}
90+
```
91+
92+
You can also enumerate the values in a tuple using a [for loop](control-flow.md#looping-over-values):
93+
94+
```swift
95+
define values 1 2 3
96+
97+
for value in values {
98+
print value // prints 1 2 3
99+
}
100+
```
101+
73102
## Structured Data
74103

75104
As well as simple values like numbers and text, it can sometimes be useful to group together sets of related data. Tuples can be nested arbitrarily to create complex data structures:
@@ -213,11 +242,16 @@ define data object {
213242
height 10
214243
depth 15
215244
}
245+
```
246+
247+
To access the members of an object you can either use the [dot operator](expressions.md#members) or [subscripting](expressions.md#subscripting):
216248

249+
```swift
217250
print data.height // prints 10
251+
print data["height"] // also prints 10
218252
```
219253

220-
Objects can also be nested:
254+
Objects can be nested inside each other:
221255

222256
```swift
223257
define data object {
@@ -231,6 +265,21 @@ define data object {
231265
}
232266

233267
print data.dimensions.height // prints 10
268+
print data["dimensions"]["height"] // also prints 10
269+
```
270+
271+
Attempting to access a non-existent member of an object will cause an error. To check if an object contains a particular member before you try to access it, you can use the `in` operator:
272+
273+
```swift
274+
define axes object {
275+
x (1 0 0)
276+
y (0 1 0)
277+
z (0 0 1)
278+
}
279+
280+
if "x" in axes {
281+
print "axes object contains an x component"
282+
}
234283
```
235284

236285
To enumerate all the members of an object, you can use a [for loop](control-flow.md#looping-over-values). Each member is returned as a tuple of the key (member name) and value:
@@ -241,7 +290,7 @@ for row in data {
241290
}
242291
```
243292

244-
**Note:** object members are *unordered*, meaning that the order in which they are defined has no special significance. When looping through members of an object, the order will be alphabetical rather than the order in which the members were defined.
293+
**Note:** object members are *unordered*, meaning that the order in which they are defined has no special significance. When looping through members of an object, the order will be alphabetical rather than reflecting the order in which the members were defined.
245294

246295
---
247296
[Index](index.md) | Next: [Symbols](symbols.md)

docs/src/expressions.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ for i in loops {
197197
}
198198
```
199199

200+
You can also use the `in` operator to check if a range contains a particular value:
201+
202+
```swift
203+
define range 1 to 5
204+
205+
if 2.5 in range {
206+
print "range contains 2.5"
207+
}
208+
```
209+
200210
**Note:** Ranges are inclusive of both the start and end values, so a loop from `0 to 5` would loop *6* times and not 5 as you might expect.
201211

202212
Range values can be fractional and/or negative:
@@ -245,6 +255,17 @@ for i in 5 to 1 step -1 {
245255
}
246256
```
247257

258+
For stepped ranges, `in` will only return true for values that align with the steps:
259+
260+
```swift
261+
define range 1 to 5 step 2
262+
263+
print 1 in range // prints true
264+
print 2 in range // prints false
265+
print 2.5 in range // prints false
266+
print 3 in range // prints true
267+
```
268+
248269
## Members
249270

250271
Compound values like [vectors and tuples](literals.md#vectors-and-tuples) and [objects](literals.md#objects) can be decomposed by using the *dot* operator to access individual components or *members*:
@@ -349,7 +370,25 @@ for i in 0 to foo.count - 1 {
349370
}
350371
```
351372

352-
Trying to access elements outside the range `0 to count - 1` will result in an error.
373+
To access elements relative to the end of the tuple, you can use negative indices. An index of `-1` is shorthand for `count - 1`:
374+
375+
376+
```swift
377+
define foo 1 2 3 4
378+
379+
print foo[-1] // prints 4
380+
print foo[-2] // prints 3
381+
```
382+
383+
Trying to access tuple elements outside the range `-count to count - 1` will result in an error, as will attempting to access a named member that does not exist. To avoid the error you can check if a given member exists by using the `in` operator, as follows:
384+
385+
```swift
386+
define purple 0.6 0 1
387+
388+
if "red" in purple {
389+
print "the red value of purple is " purple["red"] // prints 0.6
390+
}
391+
```
353392

354393
---
355394
[Index](index.md) | Next: [Functions](functions.md)

0 commit comments

Comments
 (0)