API

Tip: This documentation is also available in JSON format at /api.json.


base64#

base64/decode#

(decode s & [strict?])

Decodes a Base64 string. Optional strict? flag validates characters.

Example:

(decode "SGVsbG8=") ; => "Hello"

[View source]

base64/decode-url#

(decode-url s & [strict?])

Decodes a URL-safe Base64 string. Adds padding automatically.

Example:

(decode-url "SGVsbG8") ; => "Hello"

[View source]

base64/encode#

(encode s)

Encodes a string to Base64.

Example:

(encode "Hello") ; => "SGVsbG8="

[View source]

base64/encode-url#

(encode-url s)

Encodes a string to URL-safe Base64 (no padding).

Example:

(encode-url "Hello") ; => "SGVsbG8"

[View source]


core#

%#

(% dividend divisor)

Return the remainder of dividend / divisor. [View source]

*#

(* & xs)

Returns the product of all elements in xs. All elements in xs must be numbers. If xs is empty, return 1. [View source]

**#

(** a x)

Return a to the power of x. [View source]

*build-mode*#

Set to true when a file is being built/compiled, false otherwise.

*file*#

*file*

Returns the path of the current source file.

Example:

(println *file*) ; => "/path/to/current/file.phel"

*ns*#

*ns*

Returns the namespace in the current scope.

Example:

(println *ns*) ; => "my-app\core"

+#

(+ & xs)

Returns the sum of all elements in xs. All elements xs must be numbers. If xs is empty, return 0. [View source]

-#

(- & xs)

Returns the difference of all elements in xs. If xs is empty, return 0. If xs has one element, return the negative value of that element. [View source]

->#

(-> x & forms)

Threads the expr through the forms. Inserts x as the second item in the first form, making a list of it if it is not a list already. If there are more forms, insert the first form as the second item in the second form, etc. [View source]

->>#

(->> x & forms)

Threads the expr through the forms. Inserts x as the last item in the first form, making a list of it if it is not a list already. If there are more forms, insert the first form as the last item in the second form, etc. [View source]

/#

(/ & xs)

Returns the nominator divided by all the denominators. If xs is empty, returns 1. If xs has one value, returns the reciprocal of x. [View source]

<#

(< a & more)

Checks if each argument is strictly less than the following argument.

Example:

(< 1 2 3 4) ; => true

[View source]

<=#

(<= a & more)

Checks if each argument is less than or equal to the following argument. Returns a boolean. [View source]

<=>#

(<=> a b)

Alias for the spaceship PHP operator in ascending order. Returns an int. [View source]

=#

(= a & more)

Checks if all values are equal (value equality, not identity).

Example:

(= [1 2 3] [1 2 3]) ; => true

[View source]

>#

(> a & more)

Checks if each argument is strictly greater than the following argument.

Example:

(> 4 3 2 1) ; => true

[View source]

>=#

(>= a & more)

Checks if each argument is greater than or equal to the following argument. Returns a boolean. [View source]

>=<#

(>=< a b)

Alias for the spaceship PHP operator in descending order. Returns an int. [View source]

NAN#

Constant for Not a Number (NAN) values. [View source]

all?#

(all? pred coll)

Returns true if predicate is true for every element in collection, false otherwise.

Example:

(all? even? [2 4 6 8]) ; => true

[View source]

and#

(and & args)

Evaluates expressions left to right, returning the first falsy value or the last value.

Example:

(and true 1 "hello") ; => "hello"

[View source]

apply#

(apply f expr*)

Calls the function with the given arguments. The last argument must be a list of values, which are passed as separate arguments, rather than a single list. Apply returns the result of the calling function.

Example:

(apply + [1 2 3]) ; => 6

[Read more]

argv#

Vector of arguments passed to the script. [View source]

as->#

(as-> expr name & forms)

Binds name to expr, evaluates the first form in the lexical context of that binding, then binds name to that result, repeating for each successive form, returning the result of the last form. [View source]

assoc#

(assoc ds key value)

Associates a value with a key in a collection.

Example:

(assoc {:a 1} :b 2) ; => {:a 1 :b 2}

[View source]

assoc-in#

(assoc-in ds [k & ks] v)

Associates a value in a nested data structure at the given path.

Creates intermediate maps if they don't exist.

Example:

(assoc-in {:a {:b 1}} [:a :c] 2) ; => {:a {:b 1 :c 2}}

See also: get-in, update-in, dissoc-in [View source]

associative?#

(associative? x)

Returns true if x is an associative data structure, false otherwise.

Associative data structures include hash maps, structs, and associative PHP arrays.

Example:

(associative? {:a 1}) ; => true

[View source]

binding#

(binding bindings & body)

Temporary redefines definitions while executing the body. The value will be reset after the body was executed. [View source]

bit-and#

(bit-and x y & args)

Bitwise and. [View source]

bit-clear#

(bit-clear x n)

Clear bit an index n. [View source]

bit-flip#

(bit-flip x n)

Flip bit at index n. [View source]

bit-not#

(bit-not x)

Bitwise complement. [View source]

bit-or#

(bit-or x y & args)

Bitwise or. [View source]

bit-set#

(bit-set x n)

Set bit an index n. [View source]

bit-shift-left#

(bit-shift-left x n)

Bitwise shift left. [View source]

bit-shift-right#

(bit-shift-right x n)

Bitwise shift right. [View source]

bit-test#

(bit-test x n)

Test bit at index n. [View source]

bit-xor#

(bit-xor x y & args)

Bitwise xor. [View source]

boolean?#

(boolean? x)

Returns true if x is a boolean, false otherwise. [View source]

butlast#

(butlast coll)

Returns all but the last item in coll.

Example:

(butlast [1 2 3 4]) ; => [1 2 3]

See also: last, drop-last [View source]

case#

(case e & pairs)

Evaluates expression and matches it against constant test values, returning the associated result.

Example:

(case x 1 "one" 2 "two" "other") ; => "one" (when x is 1)

[View source]

catch#

(catch exception-type exception-name expr*)

Handle exceptions thrown in a try block by matching on the provided exception type. The caught exception is bound to exception-name while evaluating the expressions.

Example:

(try (throw (php/new Exception "error")) (catch Exception e (php/-> e (getMessage))))

[Read more]

coerce-in#

(coerce-in v min max)

Returns v if it is in the range, or min if v is less than min, or max if v is greater than max. [View source]

comment#

(comment &)

Ignores the body of the comment. [View source]

comp#

(comp & fs)

Takes a list of functions and returns a function that is the composition of those functions. [View source]

compact#

(compact coll & values)

Returns a lazy sequence with specified values removed from coll. If no values are specified, removes nil values by default.

Example:

(compact [1 nil 2 nil 3]) ; => (1 2 3)

[View source]

compare#

(compare x y)

Wrapper for PHP's spaceship operator (php/<=>). Returns an integer less than, equal to, or greater than zero when x is less than, equal to, or greater than y, respectively. [View source]

compile#

(compile form)

Returns the compiled PHP code string for the given form. [View source]

complement#

(complement f)

Returns a function that takes the same arguments as f and returns the opposite truth value. [View source]

concat#

(concat & colls)

Concatenates multiple collections into a lazy sequence.

Example:

(concat [1 2] [3 4]) ; => (1 2 3 4)

[View source]

cond#

(cond & pairs)

Evaluates test/expression pairs, returning the first matching expression.

Example:

(cond (< x 0) "negative" (> x 0) "positive" "zero") ; => "negative", "positive", or "zero" depending on x

[View source]

conj#

(conj)
(conj coll)
(conj coll value)
(conj coll value & more)

Returns a new collection with values added. Appends to vectors/sets, prepends to lists.

Example:

(conj [1 2] 3) ; => [1 2 3]

[Read more]

cons#

(cons x coll)

Prepends an element to the beginning of a collection.

Example:

(cons 0 [1 2 3]) ; => [0 1 2 3]

[View source]

constantly#

(constantly x)

Returns a function that always returns x and ignores any passed arguments. [View source]

contains-value?#

(contains-value? coll val)

Returns true if the value is present in the given collection, otherwise returns false.

Example:

(contains-value? {:a 1 :b 2} 2) ; => true

See also: contains?, some? [View source]

contains?#

(contains? coll key)

Returns true if key is present in collection (checks keys/indices, not values).

Example:

(contains? [10 20 30] 1) ; => true

[View source]

count#

(count coll)

Counts the number of elements in a sequence. Can be used on everything that implements the PHP Countable interface.

Works with lists, vectors, hash-maps, sets, strings, and PHP arrays. Returns 0 for nil.

Example:

(count [1 2 3]) ; => 3

See also: empty?, seq [View source]

csv-seq#

(csv-seq filename)
(csv-seq filename options)

Returns a lazy sequence of rows from a CSV file.

Example:

(take 10 (csv-seq "data.csv")) ; => [["col1" "col2"] ["val1" "val2"] ...]

[View source]

cycle#

(cycle coll)

Returns an infinite lazy sequence that cycles through the elements of collection.

Example:

(take 7 (cycle [1 2 3])) ; => (1 2 3 1 2 3 1)

See also: iterate, repeat [View source]

dec#

(dec x)

Decrements x by one. [View source]

declare#

Declare a global symbol before it is defined. [View source]

dedupe#

(dedupe coll)

Returns a lazy sequence with consecutive duplicate values removed in coll.

Example:

(dedupe [1 1 2 2 2 3 1 1]) ; => (1 2 3 1)

See also: distinct [View source]

deep-merge#

(deep-merge & args)

Recursively merges data structures. [View source]

def#

(def name meta? value)

This special form binds a value to a global symbol.

Example:

(def my-value 42)

[Read more]

def-#

Define a private value that will not be exported. [View source]

defexception#

(defexception name)

Define a new exception. [View source]

defexception*#

(defexception my-ex)

Define a new exception.

Example:

(defexception my-error)

[Read more]

definterface#

(definterface name & fns)

An interface in Phel defines an abstract set of functions. It is directly mapped to a PHP interface. An interface can be defined by using the definterface macro.

Example:

(definterface name & fns)

[View source]

definterface*#

(definterface name & fns)

An interface in Phel defines an abstract set of functions. It is directly mapped to a PHP interface. An interface can be defined by using the definterface macro.

Example:

(definterface Greeter (greet [name]))

[Read more]

defmacro#

Define a macro. [View source]

defmacro-#

(defmacro- name & fdecl)

Define a private macro that will not be exported. [View source]

defn#

Define a new global function. [View source]

defn-#

(defn- name & fdecl)

Define a private function that will not be exported. [View source]

defstruct#

(defstruct name keys & implementations)

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. [View source]

defstruct*#

(defstruct my-struct [a b c])

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.

Example:

(defstruct point [x y])

[Read more]

deref#

(deref variable)

Returns the current value inside the variable.

Example:

(def x (var 42))

See also: var, set!, swap! [View source]

difference#

(difference set & sets)

Difference between multiple sets into a new one. [View source]

dissoc#

(dissoc ds key)

Dissociates key from the datastructure ds. Returns ds without key.

Example:

(dissoc {:a 1 :b 2} :b) ; => {:a 1}

[View source]

dissoc-in#

(dissoc-in ds [k & ks])

Dissociates a value from a nested data structure at the given path.

Example:

(dissoc-in {:a {:b 1 :c 2}} [:a :b]) ; => {:a {:c 2}}

See also: dissoc, assoc-in, get-in [View source]

distinct#

(distinct coll)

Returns a lazy sequence with duplicated values removed in coll.

Example:

(distinct [1 2 1 3 2 4 3]) ; => (1 2 3 4)

See also: frequencies, set [View source]

do#

(do expr*)

Evaluates the expressions in order and returns the value of the last expression. If no expression is given, nil is returned.

Example:

(do (println "Hello") (+ 1 2)) ; prints "Hello", returns 3

[Read more]

doall#

(doall coll)

Forces realization of a lazy sequence and returns it as a vector.

Example:

(doall (map println [1 2 3])) ; => [nil nil nil]

[View source]

dofor#

(dofor head & body)

Repeatedly executes body for side effects with bindings and modifiers as provided by for. Returns nil. [View source]

dorun#

(dorun coll)

Forces realization of a lazy sequence for side effects, returns nil.

Example:

(dorun (map println [1 2 3])) ; => nil

[View source]

doseq#

(doseq seq-exprs & body)

Alias for dofor. [View source]

doto#

(doto x & forms)

Evaluates x then calls all of the methods and functions with the value of x supplied at the front of the given arguments. The forms are evaluated in order. Returns x. [View source]

drop#

(drop n coll)

Drops the first n elements of coll. Returns a lazy sequence.

Example:

(drop 2 [1 2 3 4 5]) ; => (3 4 5)

See also: take, drop-last [View source]

drop-last#

(drop-last n coll)

Drops the last n elements of coll.

Example:

(drop-last 2 [1 2 3 4 5]) ; => [1 2 3]

See also: drop, butlast [View source]

drop-while#

(drop-while pred coll)

Drops all elements at the front of coll where (pred x) is true. Returns a lazy sequence.

Example:

(drop-while |(< $ 5) [1 2 3 4 5 6 3 2 1]) ; => (5 6 3 2 1)

See also: take-while, drop [View source]

empty?#

(empty? x)

Returns true if x would be 0, "" or empty collection, false otherwise. [View source]

eval#

(eval form)

Evaluates a form and return the evaluated results. [View source]

even?#

(even? x)

Checks if x is even. [View source]

every?#

(every? pred coll)

Alias for all?. [View source]

extreme#

(extreme order args)

Returns the most extreme value in args based on the binary order function. [View source]

false?#

(false? x)

Checks if value is exactly false (not just falsy).

Example:

(false? nil) ; => false

[View source]

ffirst#

(ffirst coll)

Same as (first (first coll)). [View source]

file-seq#

(file-seq path)

Returns a lazy sequence of all files and directories in a directory tree.

Example:

(filter |(php/str_ends_with $ ".phel") (file-seq "src/")) ; => ["src/file1.phel" "src/file2.phel" ...]

[View source]

filter#

(filter pred coll)

Returns a lazy sequence of elements where predicate returns true.

Example:

(filter even? [1 2 3 4 5 6]) ; => (2 4 6)

[View source]

finally#

(finally expr*)

Evaluate expressions after the try body and all matching catches have completed. The finally block runs regardless of whether an exception was thrown.

Example:

(try (risky-operation) (catch Exception e nil) (finally (cleanup)))

[Read more]

find#

(find pred coll)

Returns the first item in coll where (pred item) evaluates to true.

Example:

(find |(> $ 5) [1 2 3 6 7 8]) ; => 6

See also: find-index, filter, some? [View source]

find-index#

(find-index pred coll)

Returns the index of the first item in coll where (pred item) evaluates to true.

Example:

(find-index |(> $ 5) [1 2 3 6 7 8]) ; => 3

See also: find, filter [View source]

first#

Returns the first element of a sequence, or nil if empty.

Example:

(first [1 2 3]) ; => 1

[View source]

flatten#

(flatten coll)

Flattens nested sequential structure into a lazy sequence of all leaf values.

Example:

(flatten [[1 2] [3 [4 5]] 6]) ; => (1 2 3 4 5 6)

[View source]

float?#

(float? x)

Returns true if x is float point number, false otherwise. [View source]

fn#

(fn [params*] expr*)

Defines a function. A function consists of a list of parameters and a list of expression. The value of the last expression is returned as the result of the function. All other expression are only evaluated for side effects. If no expression is given, the function returns nil.

Example:

(fn [x y] (+ x y))

[Read more]

for#

(for head & body)

List comprehension. The head of the loop is a vector that contains a sequence of bindings modifiers and options. A binding is a sequence of three values binding :verb expr. Where binding is a binding as in let and :verb is one of the following keywords:

  • :range loop over a range by using the range function.
  • :in loops over all values of a collection (including strings).
  • :keys loops over all keys/indexes of a collection.
  • :pairs loops over all key-value pairs of a collection.

After each loop binding, additional modifiers can be applied. Modifiers have the form :modifier argument. The following modifiers are supported:

  • :while breaks the loop if the expression is falsy.
  • :let defines additional bindings.
  • :when only evaluates the loop body if the condition is true.

Finally, additional options can be set:

  • :reduce [accumulator initial-value] Instead of returning a list, it reduces the values into accumulator. Initially accumulator is bound to initial-value.

Example:

(for [x :in [1 2 3]] (* x 2)) ; => [2 4 6]

[View source]

foreach#

(foreach [value valueExpr] expr*)
(foreach [key value valueExpr] expr*)

The foreach special form can be used to iterate over all kind of PHP datastructures. The return value of foreach is always nil. The loop special form should be preferred of the foreach special form whenever possible.

Example:

(foreach [x [1 2 3]] (println x))

[Read more]

format#

(format fmt & xs)

Returns a formatted string. See PHP's sprintf for more information. [View source]

frequencies#

(frequencies coll)

Returns a map from distinct items in coll to the number of times they appear.

Works with vectors, lists, sets, and strings.

Example:

(frequencies [:a :b :a :c :b :a]) ; => {:a 3 :b 2 :c 1}

[View source]

full-name#

(full-name x)

Return the namespace and name string of a string, keyword or symbol. [View source]

function?#

(function? x)

Returns true if x is a function, false otherwise. [View source]

gensym#

(gensym)

Generates a new unique symbol. [View source]

get#

(get ds k & [opt])

Gets the value at key in a collection. Returns default if not found.

Example:

(get {:a 1} :a) ; => 1

[View source]

get-in#

(get-in ds ks & [opt])

Accesses a value in a nested data structure via a sequence of keys.

Returns opt (default nil) if the path doesn't exist.

Example:

(get-in {:a {:b {:c 42}}} [:a :b :c]) ; => 42

See also: assoc-in, update-in [View source]

group-by#

(group-by f coll)

Returns a map of the elements of coll keyed by the result of f on each element.

Example:

(group-by count ["a" "bb" "c" "ddd" "ee"]) ; => {1 ["a" "c"] 2 ["bb" "ee"] 3 ["ddd"]}

See also: partition-by, frequencies [View source]

hash-map#

(hash-map :a 1 :b 2) ; => {:a 1 :b 2}

Creates a new hash map. If no argument is provided, an empty hash map is created. The number of parameters must be even. Shortcut: {}

Example:

(hash-map :name "Alice" :age 30) ; => {:name "Alice" :age 30}

[Read more]

hash-map?#

(hash-map? x)

Returns true if x is a hash map, false otherwise. [View source]

id#

(id a & more)

Checks if all values are identical. Same as a === b in PHP. [View source]

identity#

(identity x)

Returns its argument. [View source]

if#

(if test then else?)

A control flow structure. First evaluates test. If test evaluates to true, only the then form is evaluated and the result is returned. If test evaluates to false only the else form is evaluated and the result is returned. If no else form is given, nil will be returned.

The test evaluates to false if its value is false or equal to nil. Every other value evaluates to true. In sense of PHP this means (test != null && test !== false).

Example:

(if (> x 0) "positive" "non-positive")

[Read more]

if-let#

(if-let bindings then & [else])

If test is true, evaluates then with binding-form bound to the value of test, if not, yields else [View source]

if-not#

(if-not test then & [else])

Evaluates then if test is false, else otherwise.

Example:

(if-not (< 5 3) "not less" "less") ; => "not less"

[View source]

inc#

(inc x)

Increments x by one. [View source]

indexed?#

(indexed? x)

Returns true if x is an indexed sequence, false otherwise.

Indexed sequences include lists, vectors, and indexed PHP arrays.

Example:

(indexed? [1 2 3]) ; => true

[View source]

int?#

(int? x)

Returns true if x is an integer number, false otherwise. [View source]

interleave#

(interleave & colls)

Interleaves multiple collections. Returns a lazy sequence.

Returns elements by taking one from each collection in turn. Pads with nil when collections have different lengths. Works with infinite sequences.

Example:

(interleave [1 2 3] [:a :b :c]) ; => (1 :a 2 :b 3 :c)

[View source]

interpose#

(interpose sep coll)

Returns elements separated by a separator. Returns a lazy sequence.

Inserts sep between each element of the collection. Works with infinite sequences.

Example:

(interpose 0 [1 2 3]) ; => (1 0 2 0 3)

[View source]

intersection#

(intersection set & sets)

Intersect multiple sets into a new one. [View source]

into#

(into to & rest)

Returns to with all elements of from added.

When from is associative, it is treated as a sequence of key-value pairs. Supports persistent and transient collections.

Example:

(into [] '(1 2 3)) ; => [1 2 3]

See also: conj, concat [View source]

invert#

(invert map)

Returns a new map where the keys and values are swapped.

If map has duplicated values, some keys will be ignored.

Example:

(invert {:a 1 :b 2 :c 3}) ; => {1 :a 2 :b 3 :c}

[View source]

iterate#

(iterate f x)

Returns an infinite lazy sequence of x, (f x), (f (f x)), and so on.

Example:

(take 5 (iterate inc 0)) ; => (0 1 2 3 4)

See also: repeatedly, cycle [View source]

juxt#

(juxt & fs)

Takes a list of functions and returns a new function that is the juxtaposition of those functions.

Example:

((juxt a b c) x) => [(a x) (b x) (c x)]

[View source]

keep#

(keep pred coll)

Returns a lazy sequence of non-nil results of applying function to elements.

Example:

(keep |(when (even? $) (* $ $)) [1 2 3 4 5]) ; => (4 16)

[View source]

keep-indexed#

(keep-indexed pred coll)

Returns a lazy sequence of non-nil results of (pred i x).

Example:

(keep-indexed |(when (even? $1) $2) ["a" "b" "c" "d"]) ; => ("a" "c")

See also: keep, map-indexed [View source]

keys#

(keys coll)

Returns a sequence of all keys in a map.

Example:

(keys {:a 1 :b 2}) ; => (:a :b)

[View source]

keyword#

(keyword x)

Creates a new Keyword from a given string.

Example:

(keyword "name") ; => :name

[View source]

keyword?#

(keyword? x)

Returns true if x is a keyword, false otherwise. [View source]

kvs#

(kvs coll)

Returns a vector of key-value pairs like [k1 v1 k2 v2 k3 v3 ...].

Example:

(kvs {:a 1 :b 2}) ; => [:a 1 :b 2]

See also: pairs, keys, values [View source]

last#

(last coll)

Returns the last element of coll or nil if coll is empty or nil.

Example:

(last [1 2 3]) ; => 3

See also: first, peek, butlast [View source]

lazy-cat#

(lazy-cat & colls)

Concatenates collections into a lazy sequence (expands to concat).

Example:

(lazy-cat [1 2] [3 4]) ; => (1 2 3 4)

[View source]

lazy-seq#

(lazy-seq & body)

Creates a lazy sequence that evaluates the body only when accessed.

Example:

(lazy-seq (cons 1 (lazy-seq nil))) ; => (1)

[View source]

let#

(let [bindings*] expr*)

Creates a new lexical context with assignments defined in bindings. Afterwards the list of expressions is evaluated and the value of the last expression is returned. If no expression is given nil is returned.

Example:

(let [x 1 y 2] (+ x y)) ; => 3

[Read more]

line-seq#

(line-seq filename)

Returns a lazy sequence of lines from a file.

Example:

(take 10 (line-seq "large-file.txt")) ; => ["line1" "line2" ...]

[View source]

list#

(list 1 2 3) ; => '(1 2 3)

Creates a new list. If no argument is provided, an empty list is created. Shortcut: '()

Example:

(list 1 2 3) ; => '(1 2 3)

[Read more]

list?#

(list? x)

Returns true if x is a list, false otherwise. [View source]

loop#

(loop [bindings*] expr*)

Creates a new lexical context with variables defined in bindings and defines a recursion point at the top of the loop.

Example:

(loop [i 0] (if (< i 5) (do (println i) (recur (inc i)))))

[Read more]

macroexpand#

(macroexpand form)

Recursively expands the given form until it is no longer a macro call. [View source]

macroexpand-1#

(macroexpand-1 form)

Expands the given form once if it is a macro call. [View source]

map#

(map f & colls)

Returns a lazy sequence of the result of applying f to all of the first items in each coll, followed by applying f to all the second items in each coll until anyone of the colls is exhausted.

When given a single collection, applies the function to each element. With multiple collections, applies the function to corresponding elements from each collection, stopping when the shortest collection is exhausted.

Example:

(map inc [1 2 3]) ; => (2 3 4)

See also: filter, reduce, map-indexed, mapcat [View source]

map-indexed#

(map-indexed f coll)

Maps a function over a collection with index. Returns a lazy sequence.

Applies f to each element in xs. f is a two-argument function where the first argument is the index (0-based) and the second is the element itself. Works with infinite sequences.

Example:

(map-indexed vector [:a :b :c]) ; => ([0 :a] [1 :b] [2 :c])

[View source]

mapcat#

(mapcat f coll)

Maps a function over a collection and concatenates the results. Returns a lazy sequence.

Applies f to each element of the collection, where f should return a collection. All resulting collections are concatenated into a single lazy sequence. Works with infinite sequences.

Example:

(mapcat reverse [[1 2] [3 4]]) ; => (2 1 4 3)

[View source]

max#

(max & numbers)

Returns the numeric maximum of all numbers. [View source]

mean#

(mean xs)

Returns the mean of xs. [View source]

median#

(median xs)

Returns the median of xs. [View source]

memoize#

(memoize f)

Returns a memoized version of the function f. The memoized function caches the return value for each set of arguments.

Example:

(def f-memo (memoize f))

See also: memoize-lru [View source]

memoize-lru#

(memoize-lru f)
(memoize-lru f max-size)

Returns a memoized version of the function f with an LRU (Least Recently Used) cache limited to max-size entries. When the cache exceeds max-size, the least recently used entry is evicted. This prevents unbounded memory growth in long-running processes.

Without arguments, uses a default cache size of 128 entries.

Example:

(def fib-memo (memoize-lru fib 100))

See also: memoize [View source]

merge#

(merge & maps)

Merges multiple maps into one new map.

If a key appears in more than one collection, later values replace previous ones.

Example:

(merge {:a 1 :b 2} {:b 3 :c 4}) ; => {:a 1 :b 3 :c 4}

[View source]

merge-with#

(merge-with f & hash-maps)

Merges multiple maps into one new map. If a key appears in more than one collection, the result of (f current-val next-val) is used. [View source]

meta#

Gets the metadata of the given object or definition. [View source]

min#

(min & numbers)

Returns the numeric minimum of all numbers. [View source]

name#

(name x)

Returns the name string of a string, keyword or symbol. [View source]

namespace#

(namespace x)

Return the namespace string of a symbol or keyword. Nil if not present. [View source]

nan?#

(nan? x)

Checks if x is not a number. [View source]

neg?#

(neg? x)

Checks if x is smaller than zero. [View source]

next#

Returns the sequence after the first element, or nil if empty.

Example:

(next [1 2 3]) ; => [2 3]

[View source]

nfirst#

(nfirst coll)

Same as (next (first coll)). [View source]

nil?#

(nil? x)

Returns true if value is nil, false otherwise.

Example:

(nil? (get {:a 1} :b)) ; => true

[View source]

nnext#

(nnext coll)

Same as (next (next coll)). [View source]

not#

(not x)

Returns true if value is falsy (nil or false), false otherwise.

Example:

(not nil) ; => true

[View source]

not-any?#

(not-any? pred coll)

Returns true if (pred x) is logical false for every x in coll or if coll is empty. Otherwise returns false. [View source]

not-empty#

(not-empty coll)

Returns coll if it contains elements, otherwise nil. [View source]

not-every?#

(not-every? pred coll)

Returns false if (pred x) is logical true for every x in collection coll or if coll is empty. Otherwise returns true. [View source]

not=#

(not= a & more)

Checks if all values are unequal. Same as a != b in PHP. [View source]

ns#

(ns name imports*)

Defines the namespace for the current file and adds imports to the environment. Imports can either be uses or requires. The keyword :use is used to import PHP classes, the keyword :require is used to import Phel modules and the keyword :require-file is used to load php files.

Example:

(ns my-app\core (:require phel\str :as str))

[Read more]

number?#

(number? x)

Returns true if x is a number, false otherwise. [View source]

odd?#

(odd? x)

Checks if x is odd. [View source]

one?#

(one? x)

Checks if x is one. [View source]

or#

(or & args)

Evaluates expressions left to right, returning the first truthy value or the last value.

Example:

(or false nil 42 100) ; => 42

[View source]

pairs#

(pairs coll)

Gets the pairs of an associative data structure.

Example:

(pairs {:a 1 :b 2}) ; => ([:a 1] [:b 2])

See also: keys, values, kvs [View source]

partial#

(partial f & args)

Takes a function f and fewer than normal arguments of f and returns a function that a variable number of additional arguments. When call f will be called with args and the additional arguments. [View source]

partition#

(partition n coll)

Partitions collection into chunks of size n, dropping incomplete final partition.

Example:

(partition 3 [1 2 3 4 5 6 7]) ; => ([1 2 3] [4 5 6])

[View source]

partition-all#

(partition-all n coll)

Partitions collection into chunks of size n, including incomplete final partition.

Example:

(partition-all 3 [1 2 3 4 5 6 7]) ; => ([1 2 3] [4 5 6] [7])

[View source]

partition-by#

(partition-by f coll)

Returns a lazy sequence of partitions. Applies f to each value in coll, splitting them each time the return value changes.

Example:

(partition-by |(< $ 3) [1 2 3 4 5 1 2]) ; => [[1 2] [3 4 5] [1 2]]

See also: group-by, partition [View source]

peek#

(peek coll)

Returns the last element of a sequence.

Example:

(peek [1 2 3]) ; => 3

[View source]

persistent#

(persistent coll)

Converts a transient collection back to a persistent collection.

Example:

(def t (transient {}))

See also: transient [View source]

phel->php#

(phel->php x)

Recursively converts a Phel data structure to a PHP array.

Example:

(phel->php {:a [1 2 3] :b {:c 4}}) ; => (PHP array ["a" => [1, 2, 3], "b" => ["c" => 4]])

See also: php->phel [View source]

php->phel#

(php->phel x)

Recursively converts a PHP array to Phel data structures.

Indexed PHP arrays become vectors, associative PHP arrays become maps.

Example:

(php->phel (php-associative-array "a" 1 "b" 2)) ; => {"a" 1 "b" 2}

See also: phel->php [View source]

php-array-to-map#

(php-array-to-map arr)

Converts a PHP Array to a Phel map.

Example:

(php-array-to-map (php-associative-array "a" 1 "b" 2)) ; => {"a" 1 "b" 2}

See also: to-php-array, php->phel [View source]

php-array?#

(php-array? x)

Returns true if x is a PHP Array, false otherwise. [View source]

php-associative-array#

(php-associative-array & xs)

Creates a PHP associative array from key-value pairs.

Arguments: Key-value pairs (must be even number of arguments)

Example:

(php-associative-array "name" "Alice" "age" 30) ; => (PHP array ["name" => "Alice", "age" => 30])

[View source]

php-indexed-array#

(php-indexed-array & xs)

Creates a PHP indexed array from the given values.

Example:

(php-indexed-array 1 2 3) ; => (PHP array [1, 2, 3])

[View source]

php-object?#

(php-object? x)

Returns true if x is a PHP object, false otherwise. [View source]

php-resource?#

(php-resource? x)

Returns true if x is a PHP resource, false otherwise. [View source]

pop#

(pop coll)

Removes the last element of the array coll. If the array is empty returns nil. [View source]

pos?#

(pos? x)

Checks if x is greater than zero. [View source]

print#

(print & xs)

Prints the given values to the default output stream. Returns nil. [View source]

(print-str & xs)

Same as print. But instead of writing it to an output stream, the resulting string is returned. [View source]

printf#

(printf fmt & xs)

Output a formatted string. See PHP's printf for more information. [View source]

println#

(println & xs)

Same as print followed by a newline. [View source]

push#

Deprecated: 0.25.0 — Use conj instead

(push coll x)

Inserts x at the end of the sequence coll. [View source]

put#

Deprecated: 0.25.0 — Use assoc instead

(put ds key value)

Puts value mapped to key on the datastructure ds. Returns ds. [View source]

put-in#

Deprecated: 0.25.0 — Use assoc-in instead

(put-in ds ks v)

Puts a value into a nested data structure. [View source]

quote#

(quote form)

Returns the unevaluated form.

Example:

(quote (+ 1 2)) ; => '(+ 1 2)

[Read more]

rand#

(rand)

Returns a random number between 0 and 1. [View source]

rand-int#

(rand-int n)

Returns a random number between 0 and n. [View source]

rand-nth#

(rand-nth xs)

Returns a random item from xs. [View source]

range#

(range a & rest)

Creates a lazy sequence of numbers from start to end (exclusive).

Example:

(range 5) ; => (0 1 2 3 4)

[View source]

re-seq#

(re-seq re s)

Returns a sequence of successive matches of pattern in string. [View source]

read-file-lazy#

(read-file-lazy filename)
(read-file-lazy filename chunk-size)

Returns a lazy sequence of byte chunks from a file.

Example:

(take 5 (read-file-lazy "large-file.bin" 1024)) ; => ["chunk1" "chunk2" ...]

[View source]

read-string#

(read-string s)

Reads the first phel expression from the string s. [View source]

realized?#

(realized? coll)

Returns true if a lazy sequence has been realized, false otherwise.

Example:

(realized? (take 5 (iterate inc 1))) ; => false

[View source]

recur#

(recur expr*)

Internally recur is implemented as a PHP while loop and therefore prevents the Maximum function nesting level errors.

Example:

(loop [n 5 acc 1] (if (<= n 1) acc (recur (dec n) (* acc n))))

[Read more]

reduce#

(reduce f & args)

Reduces collection to a single value by repeatedly applying function to accumulator and elements.

Example:

(reduce + [1 2 3 4]) ; => 10

[View source]

remove#

(remove coll offset & [n])

Removes up to n elements from array coll starting at index offset.

Example:

(remove my-array 1 2)

[View source]

repeat#

(repeat a & rest)

Returns a vector of length n where every element is x.

With one argument returns an infinite lazy sequence of x.

Example:

(repeat 3 :a) ; => [:a :a :a]

See also: repeatedly, cycle [View source]

repeatedly#

(repeatedly a & rest)

Returns a vector of length n with values produced by repeatedly calling f.

With one argument returns an infinite lazy sequence of calls to f.

Example:

(repeatedly 3 rand) ; => [0.234 0.892 0.456] (random values)

See also: repeat, iterate [View source]

rest#

(rest coll)

Returns the sequence after the first element, or empty sequence if none.

Example:

(rest [1 2 3]) ; => [2 3]

[View source]

reverse#

(reverse coll)

Reverses the order of the elements in the given sequence.

Example:

(reverse [1 2 3 4]) ; => [4 3 2 1]

[View source]

second#

(second coll)

Returns the second element of a sequence, or nil if not present.

Example:

(second [1 2 3]) ; => 2

[View source]

select-keys#

(select-keys m ks)

Returns a new map including key value pairs from m selected with keys ks.

Example:

(select-keys {:a 1 :b 2 :c 3} [:a :c]) ; => {:a 1 :c 3}

See also: dissoc [View source]

seq#

(seq coll)

Returns a seq on the collection. Strings are converted to a vector of characters. Collections are unchanged. Returns nil if coll is empty or nil.

This function is useful for explicitly converting strings to sequences of characters, enabling sequence operations like map, filter, and frequencies.

Example:

(seq "hello") ; => ["h" "e" "l" "l" "o"]

See also: count [View source]

set#

(set & xs)

Creates a new Set from the given arguments. Shortcut: #{}

Example:

(set 1 2 3) ; => #{1 2 3}

[View source]

set!#

(set! variable value)

Sets a new value to the given variable.

Example:

(def x (var 10))

See also: var, deref, swap! [View source]

set-meta!#

Sets the metadata to a given object. [View source]

set-var#

(var value)

Variables provide a way to manage mutable state that can be updated with set! and swap!. Each variable contains a single value. To create a variable use the var function.

Example:

(def counter (var 0))

[Read more]

set?#

(set? x)

Returns true if x is a set, false otherwise. [View source]

shuffle#

(shuffle coll)

Returns a random permutation of coll.

Example:

(shuffle [1 2 3 4 5]) ; => [3 1 5 2 4] (random order)

[View source]

slice#

(slice coll & [offset & [length]])

Extracts a slice of coll starting at offset with optional length.

Example:

(slice [1 2 3 4 5] 1 3) ; => [2 3 4]

[View source]

slurp#

(slurp path & [opts])

Reads entire file or URL into a string.

Example:

(slurp "file.txt") ; => "file contents"

[View source]

some#

(some pred coll)

Returns the first truthy value of applying predicate to elements, or nil if none found.

Example:

(some |(when (> $ 10) $) [5 15 8]) ; => 15

[View source]

some->#

(some-> x & forms)

Threads x through the forms like -> but stops when a form returns nil. [View source]

some->>#

(some->> x & forms)

Threads x through the forms like ->> but stops when a form returns nil. [View source]

some?#

(some? pred coll)

Returns true if predicate is true for at least one element in collection, false otherwise.

Example:

(some? even? [1 3 5 6 7]) ; => true

[View source]

sort#

(sort coll & [comp])

Returns a sorted vector. If no comparator is supplied compare is used.

Example:

(sort [3 1 4 1 5 9 2 6]) ; => [1 1 2 3 4 5 6 9]

See also: sort-by, compare [View source]

sort-by#

(sort-by keyfn coll & [comp])

Returns a sorted vector where the sort order is determined by comparing (keyfn item).

If no comparator is supplied compare is used.

Example:

(sort-by count ["aaa" "c" "bb"]) ; => ["c" "bb" "aaa"]

See also: sort, compare [View source]

spit#

(spit filename data & [opts])

Writes data to file, returning number of bytest that were written or nil on failure. Accepts opts map for overriding default PHP file_put_contents arguments, as example to append to file use {:flags php/FILE_APPEND}.

See PHP's file_put_contents for more information. [View source]

split-at#

(split-at n coll)

Returns a vector of [(take n coll) (drop n coll)].

Example:

(split-at 2 [1 2 3 4 5]) ; => [[1 2] [3 4 5]]

See also: split-with, take, drop [View source]

split-with#

(split-with f coll)

Returns a vector of [(take-while pred coll) (drop-while pred coll)].

Example:

(split-with |(< $ 4) [1 2 3 4 5 6]) ; => [[1 2 3] [4 5 6]]

See also: split-at, take-while, drop-while [View source]

str#

(str & args)

Creates a string by concatenating values together. If no arguments are provided an empty string is returned. Nil and false are represented as an empty string. True is represented as 1. Otherwise, it tries to call __toString. This is PHP equivalent to $args[0] . $args[1] . $args[2] .... [View source]

str-contains?#

Deprecated: Use phel\str\contains?

(str-contains? str s)

Returns true if str contains s. [View source]

string?#

(string? x)

Returns true if x is a string, false otherwise. [View source]

struct?#

(struct? x)

Returns true if x is a struct, false otherwise. [View source]

sum#

(sum xs)

Returns the sum of all elements is xs. [View source]

swap!#

(swap! variable f & args)

Atomically swaps the value of the variable to (apply f current-value args).

Returns the new value after the swap.

Example:

(def counter (var 0))

See also: var, set!, deref [View source]

symbol#

(symbol name-or-ns & [name])

Returns a new symbol for given string with optional namespace.

With one argument, creates a symbol without namespace. With two arguments, creates a symbol in the given namespace.

Example:

(symbol "foo") ; => foo

[View source]

symbol?#

(symbol? x)

Returns true if x is a symbol, false otherwise. [View source]

symmetric-difference#

(symmetric-difference set & sets)

Symmetric difference between multiple sets into a new one. [View source]

take#

(take n coll)

Takes the first n elements of coll.

Note: Metadata preservation works with inline calls but may be lost when binding to variables. Use inline calls or force realization with doall if metadata needed. See local/investigate-metadata-binding-issue.md for details. [View source]

take-last#

(take-last n coll)

Takes the last n elements of coll.

Example:

(take-last 3 [1 2 3 4 5]) ; => [3 4 5]

See also: take, drop-last [View source]

take-nth#

(take-nth n coll)

Returns every nth item in coll. Returns a lazy sequence.

Example:

(take-nth 2 [0 1 2 3 4 5 6 7 8]) ; => (0 2 4 6 8)

See also: take, filter [View source]

take-while#

(take-while pred coll)

Takes all elements at the front of coll where (pred x) is true. Returns a lazy sequence.

Example:

(take-while |(< $ 5) [1 2 3 4 5 6 3 2 1]) ; => (1 2 3 4)

See also: drop-while, take [View source]

throw#

(throw exception)

Throw an exception.

Example:

(throw (php/new InvalidArgumentException "Invalid input"))

[Read more]

time#

(time expr)

Evaluates expr and prints the time it took. Returns the value of expr. [View source]

to-php-array#

Creates a PHP Array from a sequential data structure.

See also: php-array-to-map, phel->php [View source]

transient#

(transient coll)

Converts a persistent collection to a transient collection for efficient updates.

Transient collections provide faster performance for multiple sequential updates. Use persistent to convert back.

Example:

(def t (transient []))

See also: persistent [View source]

tree-seq#

(tree-seq branch? children root)

Returns a vector of the nodes in the tree, via a depth-first walk. branch? is a function with one argument that returns true if the given node has children. children must be a function with one argument that returns the children of the node. root the root node of the tree. [View source]

true?#

(true? x)

Checks if value is exactly true (not just truthy).

Example:

(true? 1) ; => false

[View source]

truthy?#

(truthy? x)

Checks if x is truthy. Same as x == true in PHP. [View source]

try#

(try expr* catch-clause* finally-clause?)

All expressions are evaluated and if no exception is thrown the value of the last expression is returned. If an exception occurs and a matching catch-clause is provided, its expression is evaluated and the value is returned. If no matching catch-clause can be found the exception is propagated out of the function. Before returning normally or abnormally the optionally finally-clause is evaluated.

Example:

(try (/ 1 0) (catch Exception e "error"))

[Read more]

type#

(type x)

Returns the type of x. The following types can be returned:

  • :vector
  • :list
  • :struct
  • :hash-map
  • :set
  • :keyword
  • :symbol
  • :var
  • :int
  • :float
  • :string
  • :nil
  • :boolean
  • :function
  • :php/array
  • :php/resource
  • :php/object
  • :unknown [View source]

union#

(union & sets)

Union multiple sets into a new one. [View source]

unquote#

(unquote my-sym) ; Evaluates to my-sym
,my-sym          ; Shorthand for (same as above)

Values that should be evaluated in a macro are marked with the unquote function. Shortcut: ,

Example:

`(+ 1 ,(+ 2 3)) ; => (+ 1 5)

[Read more]

unquote-splicing#

(unquote-splicing my-sym) ; Evaluates to my-sym
,@my-sym                  ; Shorthand for (same as above)

Values that should be evaluated in a macro are marked with the unquote function. Shortcut: ,@

Example:

`(+ ,@[1 2 3]) ; => (+ 1 2 3)

[Read more]

unset#

Deprecated: 0.25.0 — Use dissoc instead

(unset ds key)

Returns ds without key. [View source]

unset-in#

Deprecated: 0.25.0 — Use dissoc-in instead

(unset-in ds ks)

Removes a value from a nested data structure. [View source]

update#

(update ds k f & args)

Updates a value in a datastructure by applying f to the current value.

Example:

(update {:count 5} :count inc) ; => {:count 6}

See also: update-in, assoc [View source]

update-in#

(update-in ds [k & ks] f & args)

Updates a value in a nested data structure by applying f to the value at path.

Example:

(update-in {:a {:b 5}} [:a :b] inc) ; => {:a {:b 6}}

See also: get-in, assoc-in, update [View source]

values#

(values coll)

Returns a sequence of all values in a map.

Example:

(values {:a 1 :b 2}) ; => (1 2)

[View source]

var#

(var value)

Creates a new variable with the given value.

Variables provide a way to manage mutable state that can be updated with set! and swap!. Each variable contains a single value. To create a variable use the var function.

Example:

(def counter (var 0))

See also: set!, deref, swap! [View source]

var?#

(var? x)

Checks if the given value is a variable. [View source]

vector#

(vector 1 2 3) ; => [1 2 3]

Creates a new vector. If no argument is provided, an empty vector is created. Shortcut: []

Example:

(vector 1 2 3) ; => [1 2 3]

[Read more]

vector?#

(vector? x)

Returns true if x is a vector, false otherwise. [View source]

when#

(when test & body)

Evaluates body if test is true, otherwise returns nil.

Example:

(when (> 10 5) "greater") ; => "greater"

[View source]

when-let#

(when-let bindings & body)

When test is true, evaluates body with binding-form bound to the value of test [View source]

when-not#

(when-not test & body)

Evaluates body if test is false, otherwise returns nil.

Example:

(when-not (empty? [1 2 3]) "has items") ; => "has items"

[View source]

with-output-buffer#

(with-output-buffer & body)

Everything that is printed inside the body will be stored in a buffer. The result of the buffer is returned. [View source]

zero?#

(zero? x)

Checks if x is zero. [View source]

zipcoll#

(zipcoll a b)

Creates a map from two sequential data structures. Returns a new map.

Example:

(zipcoll [:a :b :c] [1 2 3]) ; => {:a 1 :b 2 :c 3}

See also: zipmap, interleave [View source]

zipmap#

(zipmap keys vals)

Returns a new map with the keys mapped to the corresponding values.

Stops when the shorter of keys or vals is exhausted.

Example:

(zipmap [:a :b :c] [1 2 3]) ; => {:a 1 :b 2 :c 3}

See also: zipcoll [View source]


debug#

debug/dbg#

(dbg expr)

Evaluates an expression and prints it with its result.

Example:

(dbg (+ 1 2))

[View source]

debug/dotrace#

(dotrace name f)

Wraps a function to print each call and result with indentation.

Example:

(def add (dotrace "add" +))

[View source]

debug/reset-trace-state!#

(reset-trace-state!)

Resets trace counters to initial values.

Example:

(reset-trace-state!)

[View source]

debug/set-trace-id-padding!#

(set-trace-id-padding! estimated-id-padding)

Sets the number of digits for trace ID padding.

Example:

(set-trace-id-padding! 3)

[View source]

debug/spy#

(spy expr)
(spy label expr)

Evaluates an expression and prints it with an optional label.

Example:

(spy (+ 1 2))

[View source]

debug/tap#

(tap value)
(tap value handler)

Prints a value and returns it unchanged. Useful in pipelines.

Example:

(-> 5 (tap) (* 2))

[View source]


html#

html/doctype#

(doctype type)

Returns an HTML doctype declaration.

Example:

(doctype :html5) ; => "<!DOCTYPE html>\n"

[View source]

html/escape-html#

(escape-html s)

Escapes HTML special characters to prevent XSS.

Example:

(escape-html "<div>") ; => "&lt;div&gt;"

[View source]

html/html#

(html & content)

Compiles Phel vectors to HTML strings.

Example:

(html [:div "Hello"]) ; => "<div>Hello</div>"

[View source]

html/raw-string#

(raw-string s)

Creates a new raw-string struct. [View source]

html/raw-string?#

(raw-string? x)

Checks if x is an instance of the raw-string struct. [View source]


http#

http/create-response-from-map#

Deprecated: Use response-from-map

[View source]

http/create-response-from-string#

Deprecated: Use response-from-string

[View source]

http/emit-response#

(emit-response response)

Emits the response by sending headers and outputting the body.

Example:

(emit-response (response-from-string "Hello World")) ; => nil

[View source]

http/files-from-globals#

(files-from-globals & [files])

Extracts the files from $_FILES and normalizes them to a map of "uploaded-file".

Example:

(files-from-globals) ; => {:avatar (uploaded-file "/tmp/phpYzdqkD" 1024 0 "photo.jpg" "image/jpeg")}

[View source]

http/headers-from-server#

(headers-from-server & [server])

Extracts all headers from the $_SERVER variable.

Example:

(headers-from-server) ; => {:host "example.com" :content-type "application/json"}

[View source]

http/request#

(request method uri headers parsed-body query-params cookie-params server-params uploaded-files version attributes)

Creates a new request struct. [View source]

http/request-from-globals#

(request-from-globals)

Extracts a request from $_SERVER, $_GET, $_POST, $_COOKIE and $_FILES.

Example:

(request-from-globals) ; => (request "GET" (uri ...) {...} nil {...} {...} {...} {...} "1.1" {})

[View source]

http/request-from-globals-args#

(request-from-globals-args server get-parameter post-parameter cookies files)

Extracts a request from args.

Example:

(request-from-globals-args php/$_SERVER php/$_GET php/$_POST php/$_COOKIE php/$_FILES) ; => (request "GET" (uri ...) {...} nil {...} {...} {...} {...} "1.1" {})

[View source]

http/request-from-map#

(request-from-map {:method method, :version version, :uri uri, :headers headers, :parsed-body parsed-body, :query-params query-params, :cookie-params cookie-params, :server-params server-params, :uploaded-files uploaded-files, :attributes attributes})

Creates a request struct from a map with optional keys :method, :uri, :headers, etc.

Example:

(request-from-map {:method "POST" :uri "https://api.example.com/users"}) ; => (request "POST" (uri ...) {} nil {} {} {} [] "1.1" {})

[View source]

http/request?#

(request? x)

Checks if x is an instance of the request struct. [View source]

http/response#

(response status headers body version reason)

Creates a new response struct. [View source]

http/response-from-map#

(response-from-map {:status status, :headers headers, :body body, :version version, :reason reason})

Creates a response struct from a map with optional keys :status, :headers, :body, :version, and :reason.

Example:

(response-from-map {:status 200 :body "Hello World"}) ; => (response 200 {} "Hello World" "1.1" "OK")

[View source]

http/response-from-string#

(response-from-string s)

Create a response from a string.

Example:

(response-from-string "Hello World") ; => (response 200 {} "Hello World" "1.1" "OK")

[View source]

http/response?#

(response? x)

Checks if x is an instance of the response struct. [View source]

http/uploaded-file#

(uploaded-file tmp-file size error-status client-filename client-media-type)

Creates a new uploaded-file struct. [View source]

http/uploaded-file?#

(uploaded-file? x)

Checks if x is an instance of the uploaded-file struct. [View source]

http/uri#

(uri scheme userinfo host port path query fragment)

Creates a new uri struct. [View source]

http/uri-from-globals#

(uri-from-globals & [server])

Extracts the URI from the $_SERVER variable.

Example:

(uri-from-globals) ; => (uri "https" nil "example.com" 443 "/path" "foo=bar" nil)

[View source]

http/uri-from-string#

(uri-from-string url)

Create a uri struct from a string.

Example:

(uri-from-string "https://example.com/path?foo=bar") ; => (uri "https" nil "example.com" nil "/path" "foo=bar" nil)

[View source]

http/uri?#

(uri? x)

Checks if x is an instance of the uri struct. [View source]


json#

json/decode#

(decode json & [{:flags flags, :depth depth}])

Decodes a JSON string to a Phel value.

Example:

(decode "{\"name\":\"Alice\"}") ; => {:name "Alice"}

[View source]

json/decode-value#

(decode-value x)

Converts a JSON value to Phel format.

Example:

(decode-value [1 2 3]) ; => [1 2 3]

[View source]

json/encode#

(encode value & [{:flags flags, :depth depth}])

Encodes a Phel value to a JSON string.

Example:

(encode {:name "Alice"}) ; => "{\"name\":\"Alice\"}"

[View source]

json/encode-value#

(encode-value x)

Converts a Phel value to JSON-compatible format.

Example:

(encode-value :name) ; => "name"

[View source]

json/valid-key?#

(valid-key? v)

Checks if a value can be used as a JSON key.

Example:

(valid-key? :name) ; => true

[View source]


mock#

mock/call-count#

(call-count mock-fn)

Returns the number of times the mock was called.

Example:

(def my-mock (mock :result))

[View source]

mock/called-once?#

(called-once? mock-fn)

Returns true if the mock was called exactly once.

Example:

(def my-mock (mock :result))

[View source]

mock/called-times?#

(called-times? mock-fn n)

Returns true if the mock was called exactly n times.

Example:

(def my-mock (mock :result))

[View source]

mock/called-with?#

(called-with? mock-fn & expected-args)

Returns true if the mock was called with the exact arguments.

Example:

(def my-mock (mock :result))

[View source]

mock/called?#

(called? mock-fn)

Returns true if the mock was called at least once.

Example:

(def my-mock (mock :result))

[View source]

mock/calls#

(calls mock-fn)

Returns a list of all argument lists the mock was called with.

Example:

(def my-mock (mock :result))

[View source]

mock/clear-all-mocks!#

(clear-all-mocks!)

Clears the entire mock registry. Useful for cleanup between test suites in long-running processes.

Example:

(clear-all-mocks!) ; All mocks removed from registry

[View source]

mock/first-call#

(first-call mock-fn)

Returns the arguments from the first call.

Example:

(def my-mock (mock :result))

[View source]

mock/last-call#

(last-call mock-fn)

Returns the arguments from the most recent call.

Example:

(def my-mock (mock :result))

[View source]

mock/mock#

(mock return-value)

Creates a mock function that returns a fixed value and tracks all calls.

Example:

(def my-mock (mock :return-value))

[View source]

mock/mock-fn#

(mock-fn f)

Creates a mock function with custom behavior that tracks all calls.

Example:

(def my-mock (mock-fn (fn [x] (* x 2))))

[View source]

mock/mock-returning#

(mock-returning values)

Creates a mock that returns different values for consecutive calls. After exhausting values, returns the last value.

Example:

(def my-mock (mock-returning [1 2 3]))

[View source]

mock/mock-throwing#

(mock-throwing exception)

Creates a mock that throws an exception when called.

Example:

(def my-mock (mock-throwing (php/new \RuntimeException "API unavailable")))

[View source]

mock/mock?#

(mock? f)

Returns true if the function is a mock. [View source]

mock/never-called?#

(never-called? mock-fn)

Returns true if the mock was never called.

Example:

(def my-mock (mock :result))

[View source]

mock/reset-mock!#

(reset-mock! mock-fn)

Resets the call history of a mock without removing it from the registry. The mock can continue to be used and track new calls.

Example:

(def my-mock (mock :result))

[View source]

mock/spy#

(spy f)

Wraps an existing function to track calls while preserving original behavior.

Example:

(def original-fn (fn [x] (* x 2)))

[View source]

mock/with-mock-wrapper#

(with-mock-wrapper bindings & body)

Like with-mocks but for wrapped mocks (interop scenarios). Automatically resets the underlying mock even when wrapped in an adapter function.

Usage:

(with-mock-wrapper [fn-symbol underlying-mock wrapper-fn]
body...)

Multiple wrappers:

(with-mock-wrapper [service-a mock-a (fn [x] (mock-a (inc x)))
service-b mock-b (fn [y] (mock-b (dec y)))]
...)

Example:

(with-mock-wrapper [http mock-http identity] (http "test"))

[View source]

mock/with-mocks#

(with-mocks bindings & body)

Temporarily replaces functions with mocks using binding. Automatically resets mocks after the body executes.

Works with inline mock creation:

(with-mocks [http-get (mock {:status 200})]
(http-get)
# Mock is automatically reset after this block)

Also works with pre-defined mocks:

(let [my-mock (mock :result)]
(with-mocks [some-fn my-mock]
(some-fn)))

If you need to wrap the mock in a function (e.g., to adapt arguments), you'll need to manually reset:

(with-mocks [some-fn (fn [& args] (my-mock (transform args)))]
(some-fn)
(reset-mock! my-mock))

[View source]


php#

php/->#

(php/-> object call*)
(php/:: class call*)

Access to an object property or result of chained calls.

Example:

(php/-> date (format "Y-m-d"))

[Read more]

php/::#

(php/:: class (method-name expr*))
(php/:: class call*)

Calls a static method or property from a PHP class. Both method-name and property must be symbols and cannot be an evaluated value.

Example:

(php/:: DateTime (createFromFormat "Y-m-d" "2024-01-01"))

[Read more]

php/aget#

(php/aget arr index)

Equivalent to PHP's arr[index] ?? null.

Example:

(php/aget (php/array "a" "b" "c") 1) ; => "b"

[Read more]

php/aget-in#

(php/aget-in arr ks)

Equivalent to PHP's arr[k1][k2][k...] ?? null.

Example:

(php/aget-in nested-arr ["users" 0 "name"])

[Read more]

php/apush#

(php/apush arr value)

Equivalent to PHP's arr[] = value.

Example:

(php/apush arr "new-item")

[Read more]

php/apush-in#

(php/apush-in arr ks value)

Equivalent to PHP's arr[k1][k2][k...][] = value.

Example:

(php/apush-in arr ["users"] {:name "Bob"})

[Read more]

php/aset#

(php/aset arr index value)

Equivalent to PHP's arr[index] = value.

Example:

(php/aset arr 0 "new-value")

[Read more]

php/aset-in#

(php/aset-in arr ks value)

Equivalent to PHP's arr[k1][k2][k...] = value.

Example:

(php/aset-in arr ["users" 0 "name"] "Alice")

[Read more]

php/aunset#

(php/aunset arr index)

Equivalent to PHP's unset(arr[index]).

Example:

(php/aunset arr "key-to-remove")

[Read more]

php/aunset-in#

(php/aunset-in arr ks)

Equivalent to PHP's unset(arr[k1][k2][k...]).

Example:

(php/aunset-in arr ["users" 0])

[Read more]

php/new#

(php/new expr args*)

Evaluates expr and creates a new PHP class using the arguments. The instance of the class is returned.

Example:

(php/new DateTime "2024-01-01")

[Read more]

php/oset#

(php/oset (php/-> object property) value)
(php/oset (php/:: class property) value)

Use php/oset to set a value to a class/object property.

Example:

(php/oset (php/-> obj name) "Alice")

[Read more]


repl#

repl/build-facade#

[View source]

repl/compile-str#

(compile-str s)

Compiles a Phel expression string to PHP code.

Example:

(compile-str "(+ 1 2)") ; => "(1 + 2)"

[View source]

repl/doc#

(doc sym)

Prints the documentation for the given symbol.

Example:

(doc map)

[View source]

repl/loaded-namespaces#

(loaded-namespaces)

Returns all namespaces currently loaded in the REPL.

Example:

(loaded-namespaces) ; => ["phel\core" "phel\repl"]

[View source]

repl/print-colorful#

(print-colorful & xs)

Prints arguments with colored output.

Example:

(print-colorful [1 2 3])

[View source]

repl/println-colorful#

(println-colorful & xs)

Prints arguments with colored output followed by a newline.

Example:

(println-colorful [1 2 3])

[View source]

repl/require#

(require sym & args)

Requires a Phel module into the environment.

Example:

(require phel\http :as http :refer [request]) ; => phel\http

[View source]

repl/resolve#

(resolve sym)

Resolves the given symbol in the current environment and returns a resolved Symbol with the absolute namespace or nil if it cannot be resolved.

Example:

(resolve 'map) ; => phel\core/map

[View source]

repl/use#

(use sym & args)

Adds a use statement to the environment.

Example:

(use DateTime :as DT) ; => DateTime

[View source]


str#

str/blank?#

(blank? s)

True if s is nil, empty, or contains only whitespace.

Example:

(blank? "   ") ; => true

[View source]

str/capitalize#

(capitalize s)

Converts first character to upper-case and all other characters to lower-case.

Example:

(capitalize "hELLO wORLD") ; => "Hello world"

[View source]

str/chars#

(chars s)

Returns a vector of characters from string s.

This is a convenience function for converting strings to character sequences. Properly handles multibyte UTF-8 characters.

Example:

(chars "hello") ; => ["h" "e" "l" "l" "o"]

See also: seq [View source]

str/contains?#

(contains? s substr)

True if s contains substr.

Example:

(contains? "hello world" "lo wo") ; => true

[View source]

str/ends-with?#

(ends-with? s substr)

True if s ends with substr.

Example:

(ends-with? "hello world" "world") ; => true

[View source]

str/escape#

(escape s cmap)

Returns a new string with each character escaped according to cmap.

Example:

(escape "hello" {"h" "H" "o" "O"}) ; => "HellO"

[View source]

str/includes?#

(includes? s substr)

True if s includes substr.

Example:

(includes? "hello world" "world") ; => true

[View source]

str/index-of#

(index-of s value & [from-index])

Returns the index of value in s, or nil if not found.

Example:

(index-of "hello world" "world") ; => 6

[View source]

str/join#

(join separator & [coll])

Returns a string of all elements in coll, separated by an optional separator.

Example:

(join ", " ["apple" "banana" "cherry"]) ; => "apple, banana, cherry"

[View source]

str/last-index-of#

(last-index-of s value & [from-index])

Returns the last index of value in s, or nil if not found.

Example:

(last-index-of "hello world world" "world") ; => 12

[View source]

str/lower-case#

(lower-case s)

Converts string to all lower-case.

Example:

(lower-case "HELLO World") ; => "hello world"

[View source]

str/pad-both#

(pad-both s len & [pad-str])

Returns a string padded on both sides to length len.

Example:

(pad-both "hello" 11) ; => "   hello   "

[View source]

str/pad-left#

(pad-left s len & [pad-str])

Returns a string padded on the left side to length len.

Example:

(pad-left "hello" 10) ; => "     hello"

[View source]

str/pad-right#

(pad-right s len & [pad-str])

Returns a string padded on the right side to length len.

Example:

(pad-right "hello" 10) ; => "hello     "

[View source]

str/re-quote-replacement#

(re-quote-replacement replacement)

Escapes special characters in a replacement string for literal use.

Example:

(re-quote-replacement "$1.00") ; => "\$1.00"

[View source]

str/repeat#

(repeat s n)

Returns a string containing n copies of s.

Example:

(repeat "ha" 3) ; => "hahaha"

[View source]

str/replace#

(replace s match replacement)

Replaces all instances of match with replacement in s.

Example:

(replace "hello world" "world" "there") ; => "hello there"

[View source]

str/replace-first#

(replace-first s match replacement)

Replaces the first instance of match with replacement in s.

Example:

(replace-first "hello world world" "world" "there") ; => "hello there world"

[View source]

str/reverse#

(reverse s)

Returns s with its characters reversed.

Example:

(reverse "hello") ; => "olleh"

[View source]

str/split#

(split s re & [limit])

Splits string on a regular expression, returning a vector of parts.

Example:

(split "hello world foo bar" #"\s+") ; => ["hello" "world" "foo" "bar"]

[View source]

str/split-lines#

(split-lines s)

Splits s on \n or \r\n. Trailing empty lines are not returned.

Example:

(split-lines "hello\nworld\ntest") ; => ["hello" "world" "test"]

[View source]

str/starts-with?#

(starts-with? s substr)

True if s starts with substr.

Example:

(starts-with? "hello world" "hello") ; => true

[View source]

str/subs#

(subs s start & [end])

Returns the substring of s from start (inclusive) to end (exclusive).

Example:

(subs "hello world" 0 5) ; => "hello"

[View source]

str/trim#

(trim s)

Removes whitespace from both ends of string.

Example:

(trim "  hello  ") ; => "hello"

[View source]

str/trim-newline#

(trim-newline s)

Removes all trailing newline or return characters from string.

Example:

(trim-newline "hello\n\n") ; => "hello"

[View source]

str/triml#

(triml s)

Removes whitespace from the left side of string.

Example:

(triml "  hello  ") ; => "hello  "

[View source]

str/trimr#

(trimr s)

Removes whitespace from the right side of string.

Example:

(trimr "  hello  ") ; => "  hello"

[View source]

str/upper-case#

(upper-case s)

Converts string to all upper-case.

Example:

(upper-case "hello World") ; => "HELLO WORLD"

[View source]


test#

test/deftest#

(deftest test-name & body)

Defines a test function.

Example:

(deftest test-add)

[View source]

test/is#

(is form & [message])

Asserts that an expression is true.

Example:

(is (= 4 (+ 2 2)))

[View source]

test/print-summary#

(print-summary)

Prints test results summary.

Example:

(print-summary)

[View source]

test/report#

(report data)

Records test results and prints status indicators.

Example:

(report {:state :pass})

[View source]

test/run-tests#

(run-tests options & namespaces)

Runs all tests in the given namespaces.

Example:

(run-tests {} 'my-app	est)

[View source]

test/successful?#

(successful?)

Checks if all tests passed.

Example:

(successful?) # => true

[View source]