Skip to content

Commit ec99baf

Browse files
committed
add extra docs with php+clojure examples
1 parent 6c4d2c5 commit ec99baf

File tree

6 files changed

+520
-40
lines changed

6 files changed

+520
-40
lines changed

content/documentation/basic-types.md

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,38 @@ weight = 2
55

66
## Nil, True, False
77

8-
Nil, true and false are literal constants. In Phel, `nil` is the same as `null` in PHP. Phel's `true` and `false` are the same as PHP's `true` and `false`.
8+
Nil, true and false are literal constants.
99

1010
```phel
1111
nil
1212
true
1313
false
1414
```
1515

16+
In Phel, only `false` and `nil` are falsy. Everything else is truthy—including `0`, `""`, and `[]`.
17+
18+
```phel
19+
# Truthiness examples
20+
(if nil "yes" "no") # => "no" (nil is falsy)
21+
(if false "yes" "no") # => "no" (false is falsy)
22+
(if 0 "yes" "no") # => "yes" (0 is truthy!)
23+
(if "" "yes" "no") # => "yes" (empty string is truthy!)
24+
(if [] "yes" "no") # => "yes" (empty vector is truthy!)
25+
```
26+
27+
{% php_note() %}
28+
In PHP, `nil` is the same as `null`, and `true`/`false` are the same. However, truthiness works differently:
29+
30+
**PHP**: `0`, `""`, `[]`, `null`, and `false` are all falsy
31+
**Phel**: Only `false` and `nil` are falsy
32+
33+
This means `if (0)` in PHP is false, but `(if 0 ...)` in Phel is true!
34+
{% end %}
35+
36+
{% clojure_note() %}
37+
Truthiness is the same as Clojure—only `false` and `nil` are falsy.
38+
{% end %}
39+
1640
## Symbol
1741

1842
Symbols are used to name functions and variables in Phel.
@@ -26,7 +50,7 @@ my-module/my-function
2650

2751
## Keywords
2852

29-
A keyword is like a symbol that begins with a colon character. However, it is used as a constant rather than a name for something.
53+
A keyword is like a symbol that begins with a colon character. However, it is used as a constant rather than a name for something. Keywords are interned and fast for equality checks.
3054

3155
```phel
3256
:keyword
@@ -36,6 +60,39 @@ A keyword is like a symbol that begins with a colon character. However, it is us
3660
::
3761
```
3862

63+
Keywords are commonly used as map keys and function options:
64+
65+
```phel
66+
# Map keys
67+
{:name "Alice" :email "[email protected]"}
68+
69+
# Function arguments for options
70+
(defn greet [name & {:keys [formal?]}]
71+
(if formal?
72+
(str "Good day, " name)
73+
(str "Hey, " name)))
74+
75+
(greet "Alice" :formal? true) # => "Good day, Alice"
76+
```
77+
78+
{% php_note() %}
79+
Keywords are like string constants, but more efficient for map keys. Use keywords instead of strings for map keys:
80+
81+
```phel
82+
# Less idiomatic:
83+
{"name" "Alice" "age" 30}
84+
85+
# Idiomatic:
86+
{:name "Alice" :age 30}
87+
```
88+
89+
Keywords are interned (only one instance exists in memory), making equality checks very fast.
90+
{% end %}
91+
92+
{% clojure_note() %}
93+
Keywords work exactly like in Clojure—they're interned, fast for equality checks, and self-evaluate.
94+
{% end %}
95+
3996
## Numbers
4097

4198
Phel supports integers and floating-point numbers. Both use the underlying PHP implementation. Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2) notations. Binary, octal and hexadecimal formats may contain underscores (`_`) between digits for better readability.
@@ -69,9 +126,7 @@ Phel supports integers and floating-point numbers. Both use the underlying PHP i
69126

70127
## Strings
71128

72-
Strings are surrounded by double quotes. They almost work the same as PHP double-quoted strings. One difference is that the dollar sign (`$`) must not be escaped. Internally, Phel strings are represented by PHP strings. Therefore, every PHP string function can be used to operate on the string.
73-
74-
Strings can be written over multiple lines. The line break character is then ignored by the reader.
129+
Strings are surrounded by double quotes. The dollar sign (`$`) does not need to be escaped.
75130

76131
```phel
77132
"hello world"
@@ -89,9 +144,28 @@ string."
89144
90145
"Hexadecimal notation is supported: \x41"
91146
92-
"Unicodes can be encoded as in PHP: \u{1000}"
147+
"Unicodes can be encoded: \u{1000}"
148+
```
149+
150+
String concatenation and conversion using `str`:
151+
152+
```phel
153+
(str "Hello" " " "World") # => "Hello World"
154+
(str "The answer is " 42) # => "The answer is 42"
155+
```
156+
157+
{% php_note() %}
158+
Phel strings are PHP strings internally, so you can use all PHP string functions:
159+
160+
```phel
161+
(php/strlen "hello") # => 5
162+
(php/strtoupper "hello") # => "HELLO"
163+
(php/str_replace "o" "0" "hello") # => "hell0"
93164
```
94165

166+
Strings work almost the same as PHP double-quoted strings, with one difference: the dollar sign (`$`) doesn't need escaping.
167+
{% end %}
168+
95169
## Lists
96170

97171
A list is a sequence of whitespace-separated values surrounded by parentheses.
@@ -118,17 +192,42 @@ A vector in Phel is an indexed data structure. In contrast to PHP arrays, Phel v
118192

119193
## Maps
120194

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.
195+
A map is a sequence of whitespace-separated key/value pairs surrounded by curly braces. The sequence is defined as key1, value1, key2, value2, etc. There must be an even number of items.
122196

123197
```phel
124198
{} # same as (hash-map)
125199
{:key1 "value1" :key2 "value2"}
126-
{'(1 2 3) '(4 5 6)}
127-
{[] []}
128-
{1 2 3 4 5 6}
200+
201+
# Any type can be a key
202+
{'(1 2 3) '(4 5 6)} # Lists as keys
203+
{[] []} # Vectors as keys
204+
{1 2 3 4 5 6} # Numbers as keys
205+
206+
# Common pattern: keywords as keys
207+
{:name "Alice" :age 30 :email "[email protected]"}
208+
```
209+
210+
{% php_note() %}
211+
Unlike PHP associative arrays, Phel maps:
212+
- Can have **any type** as keys (not just strings/integers): vectors, lists, or even other maps
213+
- Are **immutable**: operations return new maps without modifying the original
214+
- Are **not** PHP arrays internally—they're their own data structure
215+
216+
```phel
217+
# PHP:
218+
$map = ['name' => 'Alice'];
219+
$map['name'] = 'Bob'; // Mutates in place
220+
221+
# Phel:
222+
(def map {:name "Alice"})
223+
(def new-map (assoc map :name "Bob")) # Returns new map
224+
# map is still {:name "Alice"}
129225
```
226+
{% end %}
130227

131-
In contrast to PHP associative arrays, Phel maps can have any types of keys.
228+
{% clojure_note() %}
229+
Maps work exactly like Clojure maps, including support for any hashable type as keys.
230+
{% end %}
132231

133232
## Sets
134233

content/documentation/control-flow.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ The _test_ evaluates to `false` if its value is `false` or equal to `nil`. Every
1717
(if true 10) # Evaluates to 10
1818
(if false 10) # Evaluates to nil
1919
(if true (print 1) (print 2)) # Prints 1 but not 2
20-
(if 0 (print 1) (print 2)) # Prints 2
20+
(if 0 (print 1) (print 2)) # Prints 1
2121
(if nil (print 1) (print 2)) # Prints 2
22-
(if [] (print 1) (print 2)) # Prints 2
22+
(if [] (print 1) (print 2)) # Prints 1
2323
```
2424

2525
## Case
@@ -145,11 +145,11 @@ have the form `:modifier argument`. The following modifiers are supported:
145145
(for [[k v] :pairs {:a 1 :b 2 :c 3}] [v k]) # Evaluates to [[1 :a] [2 :b] [3 :c]]
146146
(for [[k v] :pairs [1 2 3]] [k v]) # Evaluates to [[0 1] [1 2] [2 3]]
147147
(for [[k v] :pairs {:a 1 :b 2 :c 3} :reduce [m {}]]
148-
(put m k (inc v))) # Evaluates to {:a 2 :b 3 :c 4}
148+
(assoc m k (inc v))) # Evaluates to {:a 2 :b 3 :c 4}
149149
(for [[k v] :pairs {:a 1 :b 2 :c 3} :reduce [m {}] :let [x (inc v)]]
150-
(put m k x)) # Evaluates to {:a 2 :b 3 :c 4}
150+
(assoc m k x)) # Evaluates to {:a 2 :b 3 :c 4}
151151
(for [[k v] :pairs {:a 1 :b 2 :c 3} :when (contains-value? [:a :c] k) :reduce [acc {}]]
152-
(put acc k v)) # Evaluates to {:a 1 :c 3}
152+
(assoc acc k v)) # Evaluates to {:a 1 :c 3}
153153
154154
(for [x :in [2 2 2 3 3 4 5 6 6] :while (even? x)] x) # Evaluates to [2 2 2]
155155
(for [x :in [2 2 2 3 3 4 5 6 6] :when (even? x)] x) # Evaluates to [2 2 2 4 6 6]

0 commit comments

Comments
 (0)