Skip to content

Commit 4d4f9e8

Browse files
committed
improve docs with examples
1 parent ec99baf commit 4d4f9e8

File tree

12 files changed

+1029
-61
lines changed

12 files changed

+1029
-61
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ node_modules/
99
npm-debug.log*
1010
yarn-debug.log*
1111
yarn-error.log*
12+
phel-error.log
1213

1314
.DS_Store
1415
Thumbs.db

content/documentation/arithmetic.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ All arithmetic operators are entered in prefix notation.
1212
(+ 1 (* 2 2) (/ 10 5) 3 4 (- 5 6)) # Evaluates to 13
1313
```
1414

15+
{% php_note() %}
16+
Phel uses prefix notation (operator comes first) instead of PHP's infix notation:
17+
18+
```php
19+
// PHP - infix notation
20+
1 + (2 * 2) + (10 / 5) + 3 + 4 + (5 - 6);
21+
22+
// Phel - prefix notation
23+
(+ 1 (* 2 2) (/ 10 5) 3 4 (- 5 6))
24+
```
25+
26+
This allows operators to accept any number of arguments and eliminates operator precedence concerns.
27+
{% end %}
28+
29+
{% clojure_note() %}
30+
Arithmetic works exactly like Clojure—prefix notation with variadic support for most operators.
31+
{% end %}
32+
1533
Some operators support zero, one or multiple arguments.
1634

1735
```phel
@@ -34,6 +52,27 @@ Some operators support zero, one or multiple arguments.
3452
(/ 24 4 2) #Evaluates to 3
3553
```
3654

55+
{% php_note() %}
56+
Phel's variadic operators are more flexible than PHP's:
57+
58+
```php
59+
// PHP - requires at least two operands
60+
1 + 2 + 3 + 4 + 5;
61+
// Can't do this: +(); <- syntax error
62+
63+
// Phel - supports 0, 1, or many operands
64+
(+) # 0 (identity)
65+
(+ 1) # 1 (identity)
66+
(+ 1 2 3 4 5) # 15 (sum of all)
67+
```
68+
69+
**Useful patterns:**
70+
- `(+)` returns the additive identity (0)
71+
- `(*)` returns the multiplicative identity (1)
72+
- `(- x)` negates a number
73+
- `(/ x)` computes the reciprocal
74+
{% end %}
75+
3776
Further numeric operations are `%` to compute the remainder of two values and `**` to raise a number to the power. All numeric operations can be found in the API documentation.
3877

3978
Some numeric operations can result in an undefined or unrepresentable value. These values are called _Not a Number_ (NaN). Phel represents these values by the constant `NAN`. You can check if a result is NaN by using the `nan?` function.
@@ -44,6 +83,24 @@ Some numeric operations can result in an undefined or unrepresentable value. The
4483
(nan? NAN) # true
4584
```
4685

86+
{% php_note() %}
87+
NaN handling is similar to PHP:
88+
89+
```php
90+
// PHP
91+
is_nan(1); // false
92+
is_nan(log(-1)); // true
93+
is_nan(NAN); // true
94+
95+
// Phel
96+
(nan? 1) # false
97+
(nan? (php/log -1)) # true
98+
(nan? NAN) # true
99+
```
100+
101+
The `%` operator for remainder and `**` for exponentiation work like PHP's `%` and `**` operators.
102+
{% end %}
103+
47104
## Bitwise Operators
48105
49106
Phel allows the evaluation and manipulation of specific bits within an integer.
@@ -80,3 +137,31 @@ Phel allows the evaluation and manipulation of specific bits within an integer.
80137
(bit-test 0b1011 0) # Evaluates to true
81138
(bit-test 0b1011 2) # Evaluates to false
82139
```
140+
141+
{% php_note() %}
142+
Phel provides named functions for bitwise operations instead of PHP's operators:
143+
144+
```php
145+
// PHP bitwise operators
146+
0b1100 & 0b1001; // AND
147+
0b1100 | 0b1001; // OR
148+
0b1100 ^ 0b1001; // XOR
149+
~0b0111; // NOT
150+
0b1101 << 1; // Left shift
151+
0b1101 >> 1; // Right shift
152+
153+
// Phel named functions
154+
(bit-and 0b1100 0b1001)
155+
(bit-or 0b1100 0b1001)
156+
(bit-xor 0b1100 0b1001)
157+
(bit-not 0b0111)
158+
(bit-shift-left 0b1101 1)
159+
(bit-shift-right 0b1101 1)
160+
```
161+
162+
Phel also provides additional bit manipulation functions not available in PHP: `bit-set`, `bit-clear`, `bit-flip`, and `bit-test`.
163+
{% end %}
164+
165+
{% clojure_note() %}
166+
Bitwise operators work exactly like Clojure's—same function names and behavior for bit manipulation.
167+
{% end %}

content/documentation/basic-types.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,15 @@ A keyword is like a symbol that begins with a colon character. However, it is us
6060
::
6161
```
6262

63-
Keywords are commonly used as map keys and function options:
63+
Keywords are commonly used as map keys:
6464

6565
```phel
66-
# Map keys
66+
# Map with keyword keys
6767
{:name "Alice" :email "[email protected]"}
6868
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"
69+
# Accessing map values with keywords
70+
(get {:name "Alice" :age 30} :name) # => "Alice"
71+
(:name {:name "Alice" :age 30}) # => "Alice" (keywords are functions!)
7672
```
7773

7874
{% php_note() %}

0 commit comments

Comments
 (0)