You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/documentation/basic-types.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,7 +111,7 @@ A list will be interpreted as a function call, a macro call or a special form by
111
111
A vector is a sequence of whitespace-separated values surrounded by brackets.
112
112
113
113
```phel
114
-
[1 2 3]
114
+
[1 2 3] # same as (vector 1 2 3)
115
115
```
116
116
117
117
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
121
121
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.
122
122
123
123
```phel
124
-
{}
124
+
{} # same as (hash-map)
125
125
{:key1 "value1" :key2 "value2"}
126
126
{'(1 2 3) '(4 5 6)}
127
127
{[] []}
@@ -135,7 +135,7 @@ In contrast to PHP associative arrays, Phel maps can have any types of keys.
135
135
A set is a sequence of whitespace-separated values prefixed by the function `set` and the whole being surrounded by parentheses.
Copy file name to clipboardExpand all lines: content/documentation/data-structures.md
+50-47Lines changed: 50 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,35 +12,35 @@ A persistent list is simple a linked list. Access or modifications on the first
12
12
To create a list surround the white space separated values with parentheses or use the `list` function.
13
13
14
14
```phel
15
-
(do 1 2 3) # list with 4 entries
15
+
(do 1 2 3) # list with 4 entries
16
16
(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
18
18
```
19
19
20
20
To access values in a list the functions `get`, `first`, `second`, `next`, `rest` and `peek` can be used.
21
21
22
22
```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
25
25
(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 ()
31
31
```
32
32
33
33
New values can only be added to the front of the list with the `cons` function.
34
34
35
35
```phel
36
-
(cons 1 (list)) # Evaluates to (1)
36
+
(cons 1 (list)) # Evaluates to (1)
37
37
(cons 3 (list 1 2)) # Evaluates to (3 1 2)
38
38
```
39
39
40
40
To get the length of the list the `count` function can be used
41
41
42
42
```phel
43
-
(count (list)) # Evaluates to 0
43
+
(count (list)) # Evaluates to 0
44
44
(count (list 1 2 3)) # Evaluates to 3
45
45
```
46
46
@@ -51,17 +51,17 @@ Vectors are an indexed, sequential data structure. They offer efficient random a
51
51
To create a vector wrap the white space seperated values with brackets or use the `vector` function.
52
52
53
53
```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
56
56
```
57
57
58
58
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.
59
59
60
60
```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
63
63
(second [1 2 3]) # Evaluates to 2
64
-
(peek [1 2 3]) # Evaluates to 3
64
+
(peek [1 2 3]) # Evaluates to 3
65
65
```
66
66
67
67
New values can be appended by using the `push` function.
@@ -80,7 +80,7 @@ To change an existing value use the `put` function
80
80
A vector can be counted using the `count` function.
81
81
82
82
```phel
83
-
(count []) # Evaluates to 0
83
+
(count []) # Evaluates to 0
84
84
(count [1 2 3]) # Evaluates to 3
85
85
```
86
86
@@ -91,8 +91,8 @@ A Map contains key-value-pairs in random order. Each possible key appears at mos
91
91
To create a map wrap the key and values in curly brackets or use the `hash-map` function.
92
92
93
93
```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
96
96
```
97
97
98
98
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
106
106
To add or update a key-value pair in the map use the `put` function
107
107
108
108
```phel
109
-
(put {} :a "hello") # Evaluates to {:a "hello"}
109
+
(put {} :a "hello") # Evaluates to {:a "hello"}
110
110
(put {:a "foo"} :a "bar") # Evaluates to {:a "bar"}
111
111
```
112
112
@@ -119,20 +119,20 @@ A value in a map can be removed with the `unset` function
119
119
As in the other data structures, the `count` function can be used to count the key-value-pairs.
120
120
121
121
```phel
122
-
(count {}) # Evaluates to 0
122
+
(count {}) # Evaluates to 0
123
123
(count {:a "foo"}) # Evaluates to 1
124
124
```
125
125
126
126
## Structs
127
127
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.
129
129
130
130
```phel
131
131
(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)
136
136
```
137
137
138
138
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
141
141
142
142
A Set contains unique values in random order. All types of values are allowed that implement the `HashableInterface` and the `EqualsInterface`.
143
143
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 `#{}`
145
145
146
146
```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
148
149
```
149
150
150
151
The `push` function can be used to add a new value to the Set.
151
152
152
153
```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}
155
156
```
156
157
157
158
Similar to the Map the `unset` function can be used to remove a value from the list
158
159
159
160
```phel
160
-
(unset (set 1 2 3) 2) # Evaluates to (set 1 3)
161
+
(unset #{1 2 3} 2) # Evaluates to #{1 3}
161
162
```
162
163
163
164
Again the `count` function can be used to count the elements in the set
164
165
165
166
```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
168
169
```
169
170
170
171
Additionally, the union of a collection of sets is the set of all elements in the collection.
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.
203
206
204
207
For example, if we want to convert a PHP Array to a persistent map. This function can be used:
205
208
@@ -209,7 +212,7 @@ For example, if we want to convert a PHP Array to a persistent map. This functio
209
212
[arr]
210
213
(let [res (transient {})] # Convert a persistent data to a transient
211
214
(foreach [k v arr]
212
-
(put res k v)) # Fill the transient map (mutable)
215
+
(put res k v)) # Fill the transient map (mutable)
213
216
(persistent res))) # Convert the transient map to a persistent map.
214
217
```
215
218
@@ -219,7 +222,7 @@ In Phel all data structures can also be used as functions.
0 commit comments