Skip to content

Commit a3d54d6

Browse files
committed
docs(145): update new set shortcut syntax
1 parent 0adaf0e commit a3d54d6

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

content/documentation/basic-types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ A list will be interpreted as a function call, a macro call or a special form by
111111
A vector is a sequence of whitespace-separated values surrounded by brackets.
112112

113113
```phel
114-
[1 2 3]
114+
[1 2 3] # same as (vector 1 2 3)
115115
```
116116

117117
A vector in Phel is an indexed data structure. In contrast to PHP arrays, Phel vectors cannot be used as maps, hashtables or dictionaries.
@@ -121,7 +121,7 @@ A vector in Phel is an indexed data structure. In contrast to PHP arrays, Phel v
121121
A map is a sequence of whitespace-separated key/value pairs surrounded by curly braces, wherein the key and value of each key/value pair are separated by whitespace. There must be an even number of items between curly braces or the parser will signal a parse error. The sequence is defined as key1, value1, key2, value2, etc.
122122

123123
```phel
124-
{}
124+
{} # same as (hash-map)
125125
{:key1 "value1" :key2 "value2"}
126126
{'(1 2 3) '(4 5 6)}
127127
{[] []}
@@ -135,7 +135,7 @@ In contrast to PHP associative arrays, Phel maps can have any types of keys.
135135
A set is a sequence of whitespace-separated values prefixed by the function `set` and the whole being surrounded by parentheses.
136136

137137
```phel
138-
(set 1 2 3)
138+
#{1 2 3} # same as (set 1 2 3)
139139
```
140140

141141
## Comments

content/documentation/data-structures.md

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ A persistent list is simple a linked list. Access or modifications on the first
1212
To create a list surround the white space separated values with parentheses or use the `list` function.
1313

1414
```phel
15-
(do 1 2 3) # list with 4 entries
15+
(do 1 2 3) # list with 4 entries
1616
(list 1 2 3) # use the list function to create a new list
17-
'(1 2 3) # use a quote to create a list
17+
'(1 2 3) # use a quote to create a list
1818
```
1919

2020
To access values in a list the functions `get`, `first`, `second`, `next`, `rest` and `peek` can be used.
2121

2222
```phel
23-
(get (list 1 2 3) 0) # Evaluates to 1
24-
(first (list 1 2 3)) # Evaluates to 1
23+
(get (list 1 2 3) 0) # Evaluates to 1
24+
(first (list 1 2 3)) # Evaluates to 1
2525
(second (list 1 2 3)) # Evaluates to 2
26-
(peek (list 1 2 3)) # Evaluates to 3
27-
(next (list 1 2 3)) # Evaluates to (2 3)
28-
(next (list)) # Evaluates to nil
29-
(rest (list 1 2 3)) # Evaluates to (2 3)
30-
(rest (list)) # Evaluates to ()
26+
(peek (list 1 2 3)) # Evaluates to 3
27+
(next (list 1 2 3)) # Evaluates to (2 3)
28+
(next (list)) # Evaluates to nil
29+
(rest (list 1 2 3)) # Evaluates to (2 3)
30+
(rest (list)) # Evaluates to ()
3131
```
3232

3333
New values can only be added to the front of the list with the `cons` function.
3434

3535
```phel
36-
(cons 1 (list)) # Evaluates to (1)
36+
(cons 1 (list)) # Evaluates to (1)
3737
(cons 3 (list 1 2)) # Evaluates to (3 1 2)
3838
```
3939

4040
To get the length of the list the `count` function can be used
4141

4242
```phel
43-
(count (list)) # Evaluates to 0
43+
(count (list)) # Evaluates to 0
4444
(count (list 1 2 3)) # Evaluates to 3
4545
```
4646

@@ -51,17 +51,17 @@ Vectors are an indexed, sequential data structure. They offer efficient random a
5151
To create a vector wrap the white space seperated values with brackets or use the `vector` function.
5252

5353
```phel
54-
[1 2 3] # Creates a new vector with three values
55-
(vector 1 2 3) # Creates a new vector with three values
54+
[1 2 3] # Creates a new vector with three values
55+
(vector 1 2) # Creates a new vector with two values
5656
```
5757

5858
To get a value by its index use the `get` function. Similar to list you can use the `first`, `second` and `peek` function to access the first, second and last values of the vector.
5959

6060
```phel
61-
(get [1 2 3] 0) # Evaluates to 1
62-
(first [1 2 3]) # Evaluates to 1
61+
(get [1 2 3] 0) # Evaluates to 1
62+
(first [1 2 3]) # Evaluates to 1
6363
(second [1 2 3]) # Evaluates to 2
64-
(peek [1 2 3]) # Evaluates to 3
64+
(peek [1 2 3]) # Evaluates to 3
6565
```
6666

6767
New values can be appended by using the `push` function.
@@ -80,7 +80,7 @@ To change an existing value use the `put` function
8080
A vector can be counted using the `count` function.
8181

8282
```phel
83-
(count []) # Evaluates to 0
83+
(count []) # Evaluates to 0
8484
(count [1 2 3]) # Evaluates to 3
8585
```
8686

@@ -91,8 +91,8 @@ A Map contains key-value-pairs in random order. Each possible key appears at mos
9191
To create a map wrap the key and values in curly brackets or use the `hash-map` function.
9292

9393
```phel
94-
{:key1 value1 :key2 value2} # Create a new map with two key-value-pairs
95-
(hash-map :key1 value1 :key2 value2) # Create a new map using the hash-map function
94+
{:key1 value1 :key2 value2} # A new has-map using shortcut syntax
95+
(hash-map :key1 value1 :key2 value2) # A new has-map using the function
9696
```
9797

9898
Use the `get` function to access a value by its key
@@ -106,7 +106,7 @@ Use the `get` function to access a value by its key
106106
To add or update a key-value pair in the map use the `put` function
107107

108108
```phel
109-
(put {} :a "hello") # Evaluates to {:a "hello"}
109+
(put {} :a "hello") # Evaluates to {:a "hello"}
110110
(put {:a "foo"} :a "bar") # Evaluates to {:a "bar"}
111111
```
112112

@@ -119,20 +119,20 @@ A value in a map can be removed with the `unset` function
119119
As in the other data structures, the `count` function can be used to count the key-value-pairs.
120120

121121
```phel
122-
(count {}) # Evaluates to 0
122+
(count {}) # Evaluates to 0
123123
(count {:a "foo"}) # Evaluates to 1
124124
```
125125

126126
## Structs
127127

128-
A Struct is a special kind of Map. It only supports a predefined number of keys and is associated to a global name. The Struct not only defines itself but also a predicate function.
128+
A Struct is a special kind of Map. It only supports a predefined number of keys and is associated with a global name. The Struct not only defines itself but also a predicate function.
129129

130130
```phel
131131
(defstruct my-struct [a b c]) # Defines the struct
132-
(let [x (my-struct 1 2 3)] # Create a new struct
133-
(my-struct? x) # Evaluates to true
134-
(get x :a) # Evaluates to 1
135-
(put x :a 12)) # Evaluates to (my-struct 12 2 3)
132+
(let [x (my-struct 1 2 3)] # Create a new struct
133+
(my-struct? x) # Evaluates to true
134+
(get x :a) # Evaluates to 1
135+
(put x :a 12)) # Evaluates to (my-struct 12 2 3)
136136
```
137137

138138
Internally, Phel Structs are PHP classes where each key correspondence to an object property. Therefore, Structs can be faster than Maps.
@@ -141,65 +141,68 @@ Internally, Phel Structs are PHP classes where each key correspondence to an obj
141141

142142
A Set contains unique values in random order. All types of values are allowed that implement the `HashableInterface` and the `EqualsInterface`.
143143

144-
A new set can be created by using the `set` function
144+
A new set can be created by using the `set` function or shortcut syntax `#{}`
145145

146146
```phel
147-
(set 1 2 3) # Creates a new set with three values
147+
#{1 2 3} # A new set using shortcut syntax
148+
(set 1 2 3) # A new set using the function
148149
```
149150

150151
The `push` function can be used to add a new value to the Set.
151152

152153
```phel
153-
(push (set 1 2 3) 4) # Evaluates to (set 1 2 3 4)
154-
(push (set 1 2 3) 2) # Evaluates to (set 1 2 3)
154+
(push #{1 2 3} 4) # Evaluates to #{1 2 3 4}
155+
(push #{1 2 3} 2) # Evaluates to #{1 2 3}
155156
```
156157

157158
Similar to the Map the `unset` function can be used to remove a value from the list
158159

159160
```phel
160-
(unset (set 1 2 3) 2) # Evaluates to (set 1 3)
161+
(unset #{1 2 3} 2) # Evaluates to #{1 3}
161162
```
162163

163164
Again the `count` function can be used to count the elements in the set
164165

165166
```phel
166-
(count (set)) # Evaluates to 0
167-
(count (set 2)) # Evaluates to 1
167+
(count #{}) # Evaluates to 0
168+
(count #{2}) # Evaluates to 1
168169
```
169170

170171
Additionally, the union of a collection of sets is the set of all elements in the collection.
171172

172173
```phel
173-
(union) # Evaluates to (set)
174-
(union (set 1 2)) # Evaluates to (set 1 2)
175-
(union (set 1 2) (set 0 3)) # Evaluates to (set 0 1 2 3)
174+
(union) # Evaluates to #{}
175+
(union #{1 2}) # Evaluates to #{1 2}
176+
(union #{1 2} #{0 3}) # Evaluates to #{0 1 2 3}
176177
```
177178

178179
The intersection of two sets or more is the set containing all elements shared between those sets.
179180

180181
```phel
181-
(intersection (set 1 2) (set 0 3)) # Evaluates to (set)
182-
(intersection (set 1 2) (set 0 1 2 3)) # Evaluates to (set 1 2)
182+
(intersection #{1 2} #{0 3}) # Evaluates to #{}
183+
(intersection #{1 2} #{0 1 2 3}) # Evaluates to #{1 2}
183184
```
184185

185186
The difference of two sets or more is the set containing all elements in the first set that aren't in the other sets.
186187

187188
```phel
188-
(difference (set 1 2) (set 0 3)) # Evaluates to (set 1 2)
189-
(difference (set 1 2) (set 0 1 2 3)) # Evaluates to (set)
190-
(difference (set 0 1 2 3) (set 1 2)) # Evaluates to (set 0 3)
189+
(difference #{1 2} #{0 3}) # Evaluates to #{1 2}
190+
(difference #{1 2} #{0 1 2 3}) # Evaluates to #{}
191+
(difference #{0 1 2 3} #{1 2}) # Evaluates to #{0 3}
191192
```
192193

193194
The symmetric difference of two sets or more is the set of elements which are in either of the sets and not in their intersection.
194195

195196
```phel
196-
(symmetric-difference (set 1 2) (set 0 3)) # Evaluates to (set 0 1 2 3)
197-
(symmetric-difference (set 1 2) (set 0 1 2 3)) # Evaluates to (set 0 3)
197+
(symmetric-difference #{1 2} #{0 3}) # Evaluates to #{0 1 2 3}
198+
(symmetric-difference #{1 2} #{0 1 2 3}) # Evaluates to #{0 3}
198199
```
199200

200201
## Transients
201202

202-
Nearly all persistent data structures have a transient version (except for Persistent List). The transient version of each persistent data structure is a mutable version of them. It stores the value in the same way as the persistent version but instead of returning a new persistent version with every modification it modifies the current version. Transient versions are a bit faster and can be used as builders for new persistent collections. Since transients use the same underlying storage it is very fast to convert a persistent data structure to a transient and back.
203+
Nearly all persistent data structures have a transient version (except for Persistent List). The transient version of each persistent data structure is a mutable version of them. It stores the value in the same way as the persistent version, but instead of returning a new persistent version with every modification, it modifies the current version.
204+
205+
Transient versions are faster and can be used as builders for new persistent collections. Since transients use the same underlying storage, it is rapid to convert a persistent data structure to a transient and back.
203206

204207
For example, if we want to convert a PHP Array to a persistent map. This function can be used:
205208

@@ -209,7 +212,7 @@ For example, if we want to convert a PHP Array to a persistent map. This functio
209212
[arr]
210213
(let [res (transient {})] # Convert a persistent data to a transient
211214
(foreach [k v arr]
212-
(put res k v)) # Fill the transient map (mutable)
215+
(put res k v)) # Fill the transient map (mutable)
213216
(persistent res))) # Convert the transient map to a persistent map.
214217
```
215218

@@ -219,7 +222,7 @@ In Phel all data structures can also be used as functions.
219222

220223
```phel
221224
((list 1 2 3) 0) # Same as (get (list 1 2 3) 0)
222-
([1 2 3] 0) # Same as (get [1 2 3] 0)
225+
([1 2 3] 0) # Same as (get [1 2 3] 0)
223226
({:a 1 :b 2} :a) # Same as (get {:a 1 :b 2} :a)
224-
((set 1 2 3) 1)
227+
(#{1 2 3} 1) # Same as (get #{1 2 3} 1)
225228
```

syntaxes/phel.sublime-syntax

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ contexts:
1818
#- include: symbol
1919

2020
comment:
21-
- match: (#|;).*$\n?
21+
- match: (;|#(?!\{)).*$
2222
scope: comment.line.hash-or-semicolon.phel
2323
captures:
2424
1: punctuation.definition.comment.phel

0 commit comments

Comments
 (0)