Dynamic
This module contains dynamic functions which are used in the Carp repl and during compilation. They are not available in compiled code. Read more about dynamic functions in the Language Guide.
<
Dynamic
checks whether its first argument is less than its second.
Example Usage:
(< 1 2) ; => true
>
Dynamic
checks whether its first argument is greater than its second.
Example Usage:
(> 1 2) ; => false
all-but-last
Dynamic
gets all elements except for the last one of a list or array.
Example Usage:
(all-but-last '(1 2 3)) ; => '(1 2)
all?
Dynamic
(all? f xs)
Checks whether all of the elements in xs
conform to the predicate
function f
.
(all? (fn [x] (< 1 x)) '(2 3 4))
=> true
(all? (fn [x] (< 1 x)) '(-1 0 1))
=> false
any?
Dynamic
(any? f xs)
Checks whether any of the elements in xs
conforms to the predicate
function f
.
(any? (fn [x] (= 'a x)) '(a b c))
=> true
(any? (fn [x] (= 'a x)) '(e f g))
=> false
append
Dynamic
appends two lists or arrays.
Example Usage:
(append '(1 2) '(3 4)) ; => '(1 2 3 4)
apply
Dynamic
(apply f argument-list)
Applies the function f
to the provided argument list, passing each value
in the list as an argument to the function.
array
Dynamic
creates an array from a collection of elements.
Example Usage:
(array 1 2 3) ; => [1 2 3]
build
Dynamic
builds the current code to an executable. Optionally takes a boolean that, when true, silences the output.
Example Usage:
(build)
c
Dynamic
prints the C code emitted for a binding.
Example Usage:
(c '(+ 2 3)) ; => int _3 = Int__PLUS_(2, 3);
collect-into
Dynamic
(collect-into xs f)
Transforms a dynamic data literal into another, preserving order
column
Dynamic
returns the column a symbol was defined on.
Example Usage:
(column mysymbol)
compose
Dynamic
(compose f g)
Returns the composition of two functions f
and g
for functions of any
arity; concretely, returns a function accepting the correct number of
arguments for g
, applies g
to those arguments, then applies f
to the
result.
If you only need to compose functions that take a single argument (unary arity)
see comp
. Comp also generates the form that corresponds to the composition,
compose contrarily evaluates 'eagerly' and returns a computed symbol.
;; a silly composition
((compose empty take) 3 [1 2 3 4 5])
;; => []
(String.join (collect-into ((compose reverse map) Symbol.str '(p r a c)) array))
;; => 'carp'
;; comp for comparison
((comp (curry + 1) (curry + 2)) 4)
;; => (+ 1 (+ 2 4))
cons
Dynamic
adds an element to the front of an array or list
Example Usage:
(cons 1 '(2 3)) ; => '(1 2 3)
cons-last
Dynamic
adds an element to the back of an array or list
Example Usage:
(cons-last 3 '(1 2)) ; => '(1 2 3)
curry
Dynamic
(curry f x)
Returns a curried function accepting a single argument, that applies f
to x
and then to the following argument.
(map (curry Symbol.prefix 'Foo) '(bar baz))
=> (Foo.bar Foo.baz)
curry*
Dynamic
(curry* f :rest args)
Curry functions of any arity.
(map (curry* Dynamic.zip + '(1 2 3)) '((4 5) (6)))
=> (((+ 1 4) (+ 2 5)) ((+ 1 6)))
((curry* Dynamic.zip cons '(1 2 3)) '((4 5) (6)))
=> ((cons 1 (4 5)) (cons (2 (6))))
(defndynamic add-em-up [x y z] (+ (+ x y) z))
(map (curry* add-em-up 1 2) '(1 2 3))
=> (4 5 6)
cxr
Dynamic
(cxr x pair)
takes a specification of a traversal in a
(head) and d
(tail)
symbols and a list pair
to traverse.
Example:
(cxr '(1 a 1 d) '(1 2 3)) ; => 2
(cxr '(1 a 1 d 1 a 4 d) '(1 2 3 4 (5 6 7))) ; => 6
defdynamic
Dynamic
defines a new dynamic value, i.e. a value available at compile time.
Example Usage:
(defdynamic name value)
definterface
Dynamic
defines a new interface (which could be a function or symbol).
Example Usage:
(definterface mysymbol MyType)
defmacro
Dynamic
defines a new macro.
Example Usage:
(defmacro name [args :rest restargs] body)
defmodule
Dynamic
defines a new module in which expressions
are defined.
Example Usage:
(defmodule MyModule <expressions>)
defndynamic
Dynamic
defines a new dynamic function, i.e. a function available at compile time.
Example Usage:
(defndynamic name [args] body)
deftemplate
Dynamic
defines a new C template.
Example Usage:
(deftemplate symbol Type declString defString)
deftuple
Macro
(deftuple name :rest props)
defines a tuple type.
For example:
; is the definition of Pair in the stdlib
(deftuple Pair a b)
dynamic-type
Dynamic
Gets the dynamic type as a string.
Example Usage:
(dynamic-type '()) ; => "list"
empty
Dynamic
(empty xs)
Returns the empty form of xs
.
(empty '(1 2 3 4))
=> ()
(empty '[1 2 3 4])
=> []
empty?
Dynamic
(empty? xs)
Returns true if the provided data literal is empty, false otherwise.
expand
Dynamic
expands a macro and prints the result.
Example Usage:
(expand '(when true 1)) ; => (if true 1 ())
expand-compiled
Dynamic
expands and desugars the code.
Example Usage:
(expand-compiled '(+ 2 3)) ; => (Int.+ 2 3)
filter
Dynamic
(filter p xs)
Returns a list containing only the elements of xs
that satisify
predicate p
.
(filter (fn [x] (= 'a x)) '(a b a b a b a b))
=> (a a a a)
flatten
Dynamic
(flatten l)
Flattens a list recursively.
(flatten '(1 2 (3 (4))))
=> '(1 2 3 4)
flip
Dynamic
(flip f)
Flips the arguments of a function f
.
((flip Symbol.prefix) 'Bar 'Foo)
=> (Foo.Bar)
get-env
Dynamic
gets an environment variable. The result will be ()
if it isn’t set.
Example Usage:
(read-file "CARP_DIR")
hash
Dynamic
calculates the hash associated with a value.
Example Usage:
(hash '('my 'value)) ; => 3175346968842793108
host-arch
Dynamic
prints the host architecture (as returned by the Haskell function System.Info.arch
).
Example Usage:
(host-arch)
host-bit-width
Dynamic
gets the bit width of the host platform.
Example Usage:
(host-bit-width) ; => your host machine’s bit width, e.g. 32 or 64
host-os
Dynamic
prints the host operating system (as returned by the Haskell function System.Info.os
).
Example Usage:
(host-os)
implements
Dynamic
designates a function as an implementation of an interface.
Example Usage:
(implements zero Maybe.zero)
info
Dynamic
prints all information associated with a symbol.
Example Usage:
(info mysymbol)
last
Dynamic
gets the last element of a list or array.
Example Usage:
(last '(1 2 3)) ; => 3
length
Dynamic
returns the length of the argument (must be an array, string or list).
Example Usage:
(length '(1 2 3)) ; => 3
list
Dynamic
creates an array from a collection of elements.
Example Usage:
(list 1 2 3) ; => (1 2 3)
load
Dynamic
loads a file into the current environment.
Example Usage:
(load "myfile.carp")
(load "myrepo@version" "myfile")
load-once
Dynamic
loads a file and prevents it from being reloaded (see reload
).
Example Usage:
(load-once "myfile.carp")
(load "myrepo@version" "myfile")
macro-error
Dynamic
logs an error and errors out of a macro.
Example Usage:
(macro-error "this is wrong")
macro-log
Dynamic
logs a message in a macro.
Example Usage:
(macro-log "this will be printed at compile time")
managed?
Dynamic
checks whether a type is managed by Carp by checking whether delete
was implemented for it. For an explanation of memory management, you can reference this document.
Example Usage:
(register-type Unmanaged "void*")
(managed? Unmanaged) ; => false
map
Dynamic
(map f xs)
Applies a function f
to each element in the list or array xs
and
returns a list dynamic data literal containing the result of the function
applications.
'(map symbol? '(a b c))
=> (true true true)
'(map (curry + 1) '(1 2 3))
=> (2 3 4)
members
Dynamic
returns the members of a type as an array.
Example Usage:
(members MyType)
meta
Dynamic
gets the value under "mykey"
in the meta map associated with a symbol. It returns ()
if the key isn’t found.
Example Usage:
(meta mysymbol "mykey")
meta-set!
Dynamic
sets a new key and value pair on the meta map associated with a symbol.
Example Usage:
(meta-set! mysymbol "mykey" "myval")
mod
Dynamic
(mod x y)
implements modulo, is slower than modulo specialized on
integers imod
.
parse
Dynamic
parses a string into an expression
Example Usage:
(parse "(+ 1 2)") ; => (+ 1 2)
postwalk
Dynamic
(postwalk f form)
Performs a depth-first, post-order traversal of form
and
calls f
on each subform, collecting the result.
Example:
; note: this could be achieved using `walk-replace` as well
(postwalk (fn [x]
(cond
(= x '+) '*
(= x '-) '/
x))
'(+ 1 (- 2 3))) ; => (* 1 (/ 2 3))
prewalk
Dynamic
(prewalk f form)
behaves like postwalk
, but does pre-order
traversal.
Example:
(prewalk (fn [x] (if (number? x) (* x 4) x)) '(+ 1 (- 2 3))) ; => (+ 4 (- 8 12))
reduce
Dynamic
(reduce f x xs)
Reduces or 'folds' a data literal, such as a list or array, into a single
value through successive applications of f
.
register
Dynamic
registers a new function. This is used to define C functions and other symbols that will be available at link time.
Example Usage:
(register name <signature> <optional: override>)
register-type
Dynamic
registers a new type from C.
Example Usage:
(register-type Name <optional: c-name> <optional: members>)
relative-include
Dynamic
adds a relative include, i.e. a C include
with quotes. It also prepends the current directory.
Example Usage:
(relative-include "myheader.h")
reload
Dynamic
reloads all currently loaded files that weren’t marked as only loading once (see load
and load-once
).
Example Usage:
(reload)
reverse
Dynamic
(reverse xs)
Reverses the order of elements in an array or list.
(reverse [1 2 3 4])
=> [4 3 2 1]
run-exe-with-args
Dynamic
runs an executable with arguments.
Example Usage:
(run-exe-with-args "path-to-executable" 1 2 3)
run-with-args
Dynamic
runs the built executable with arguments.
Example Usage:
(run-with-args 1 2 3)
s-expr
Dynamic
returns the s-expression associated with a binding. When the binding is a type, the deftype form is returned instead of the type's module by default. Pass an optional bool argument to explicitly request the module for a type instead of its definition form. If the bool is true, the module for the type will be returned. Returns an error when no definition is found for the binding.
Example Usage:
(s-expr foo), (s-expr foo true)
save-docs-ex
Dynamic
takes two arrays, one with paths to modules (as symbols), and one with filenames (as strings). The filenames are used to emit global symbols in those files into a 'Global' module.
Example Usage:
(save-docs-internal '(ModuleA ModuleB) '("globals.carp"))
set-env
Dynamic
sets an environment variable.
Example Usage:
(set-env "CARP_WAS_HERE" "true")
sort
Dynamic
(sort l compare)
Sorts a list using the provided predicate. It is not a stable sort. Example:
(sort '(1 3 4 2 5 4) <) ; => (1 2 3 4 4 5)
(sort '(1 3 4 2 5 4) >) ; => (5 4 4 3 2 1)
(sort '("one" "two---" "three" "four") (fn [a b] (< (String.length a) (String.length b)))) ; => ("one" "four" "three" "two---")
structured-info
Dynamic
gets all information associated with a symbol as a list of the form (type|(), info|(), metadata)
.
Example Usage:
(structured-info mysymbol)
system-include
Dynamic
adds a system include, i.e. a C #include
with angle brackets (<>
).
Example Usage:
(system-include "stdint.h")
take
Dynamic
(take n xs)
Returns a list containing the first n
elements of a list.
(take 3 '(1 2 3 4 5))
=> (1 2 3)
unreduce
Dynamic
(unreduce f x lim acc)
Applies f
to a starting value x
, then generates a sequence of values
by successively applying f
to the result lim-1
times.
Collects results in the structure given by acc
.
(unreduce (curry + 1) 0 10 (list))
=> (1 2 3 4 5 6 7 8 9 10)
use
Dynamic
uses a module, i.e. imports the symbols inside that module into the current module.
Example Usage:
(use MyModule)
walk
Dynamic
(walk inner outer form)
traverses form
, an arbitrary data structure, using the functions
inner
and outer
. It will apply inner
to each element of form
, building
up a data structure of the same type, then applies outer
to the result. It
recognizes arrays and lists. Other data structures will just be passed to
outer
.
Example:
(walk car reverse [[1 2] [3 4] [5 6]]) ; => [5 3 1]
walk-replace
Dynamic
(walk-replace pairs form)
Performs a depth-first, pre-order traversal of form
and
finds a matching replacement for each subform in the replacement pairs pairs
,
if applicable, collecting the result.
Example:
(walk-replace '((+ *) (- /)) '(+ 1 (- 2 3))) ; => (* 1 (/ 2 3))
write-file
Dynamic
writes a string to a file.
Example Usage:
(write-file "myfile" "hello there!")
zip
Dynamic
(zip f :rest forms)
Returns the form that results from applying a function f
to each of
the values supplied in forms
.
If the members of a single form are exhuasted, the result of the
applications thus far is returned, and any remaining members in the other
forms are ignored.
(zip + '(1 2 3) '(4 5 6))
=> ((+ 1 4) (+ 2 5) (+ 3 6))
It's important to note that zip operates on forms, and that the form
returned by zip may not be evaluable by itself. For instance, to actually
transform the result in the example above into something Carp can
evaluate, we need to wrap each member of the list in a do
:
(append (list 'do) (zip + '(1 2 3) '(4 5 6)))
=> (do (+ 1 4) (+ 2 5) (+ 3 6))
(eval (append (list 'do) (zip + '(1 2 3) '(4 5 6))))
=> 9 ;; do returns the value of the last form in its body