Skip to content

Commit 68c3c2e

Browse files
committed
Rework bool.guard
1 parent fc751bf commit 68c3c2e

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

src/gleam/bool.gleam

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,33 +326,63 @@ pub fn to_string(bool: Bool) -> String {
326326

327327
/// Run a callback function if the given bool is `True`, otherwise return a
328328
/// default value.
329+
///
330+
/// With a `use` expression this function can simulate the early-return pattern
331+
/// found in some other programming languages.
329332
///
330-
/// This function is suitable for `use` expressions.
333+
/// In a procedural language:
331334
///
332-
/// ## Examples
335+
/// ```js
336+
/// if (predicate) return value;
337+
/// // ...
338+
/// ```
339+
///
340+
/// In Gleam with a `use` expression:
333341
///
334342
/// ```gleam
335-
/// > let name = "Kamaka"
336-
/// > use <- guard(name != "", or: "Welcome!")
337-
/// > "Hello, " <> name
338-
/// "Hello, Kamaka"
343+
/// use <- guard(when: predicate, return: value)
344+
/// // ...
339345
/// ```
340346
///
347+
/// Like everything in Gleam `use` is an expression, so it short circuits the
348+
/// current block, not the entire function. As a result you can assign the value
349+
/// to a variable:
350+
///
351+
/// ```gleam
352+
/// let x = {
353+
/// use <- guard(when: predicate, return: value)
354+
/// // ...
355+
/// }
356+
/// ```
357+
///
358+
/// Note that unlike in procedural languages the `return` value is evaluated
359+
/// even when the predicate is `False`, so it is advisable not to perform
360+
/// expensive computation there.
361+
///
362+
///
363+
/// ## Examples
364+
///
341365
/// ```gleam
342366
/// > let name = ""
343-
/// > use <- guard(name != "", or: "Welcome!")
367+
/// > use <- guard(when: name == "", return: "Welcome!")
344368
/// > "Hello, " <> name
345369
/// "Welcome!"
346370
/// ```
347371
///
372+
/// ```gleam
373+
/// > let name = "Kamaka"
374+
/// > use <- guard(when: name == "", return: "Welcome!")
375+
/// > "Hello, " <> name
376+
/// "Hello, Kamaka"
377+
/// ```
348378
///
349379
pub fn guard(
350-
requirement: Bool,
351-
or alternative: t,
352-
then consequence: fn() -> t,
380+
when requirement: Bool,
381+
return consequence: t,
382+
otherwise alternative: fn() -> t,
353383
) -> t {
354384
case requirement {
355-
True -> consequence()
356-
False -> alternative
385+
True -> consequence
386+
False -> alternative()
357387
}
358388
}

test/gleam/bool_test.gleam

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,13 @@ pub fn to_string_test() {
157157
}
158158

159159
pub fn guard_test() {
160-
assert 1 = {
161-
let x = 1
162-
use <- bool.guard(True, or: 2)
163-
x
160+
assert 2 = {
161+
use <- bool.guard(when: True, return: 2)
162+
1
164163
}
165164

166-
assert 2 = {
167-
let x = 1
168-
use <- bool.guard(False, or: 2)
169-
x
165+
assert 1 = {
166+
use <- bool.guard(when: False, return: 2)
167+
1
170168
}
171169
}

0 commit comments

Comments
 (0)