Introspect
Dynamic functions that return information about the s-expressions associated to a binding.
arguments
Dynamic
(arguments binding)
What are the arguments to this binding?
- When
binding
is a function, returns the argument array. - When
binding
is a command, returns the argument array. - When
binding
is a primitive, returns the argument array. - When
binding
is an interface, returns the argument array. - When
binding
is a struct, returns the fields. - When
binding
is a sumtype, returns a list of the type arguments of each constructor. - Otherwise it returns an empty list.
arity
Dynamic
(arity binding)
What's the arity of this binding?
- When
binding
is a function, returns the number of arguments. - When
binding
is a command, returns the number of arguments. - When
binding
is a primitive, returns the number of arguments. - When
binding
is an interface, returns the number of arguments. - When
binding
is a struct, returns the number of fields. - When
binding
is a sumtype, returns a list of the number of type arguments of each constructor. - Otherwise it returns 0.
with-copy
Macro
(with-copy function arg)
Returns a reference to an anonymous 'proxied' variant of function
in
which the argument in position arg
(indexed from 0) is copied from a
reference before being passed to function
:
;; Array.reduce expects a function that takes a *ref* in its second argument:
;; (Fn [a (Ref b c)] ...)
;; so we can't use a function like `+` directly; enter proxy
(reduce (with-copy + 2) 0 &[1 2 3])
=> 6
;; compare this with an inline anonymous function that achieves the same thing:
(reduce &(fn [x y] (+ x @y)) 0 &[1 2 3]) === (reduce (with-copy + 2) 0 &[1 2 3])
This is useful when using higher-order functions that operate over structures that return references to their inhabitants, such as arrays or structs. It allows you to use a function over values without writing a custom anonymous function to handle copying.
Furthermore, one can define bespoke variants for working with particular
higher-order functions. For instace, reduce
always expacts a reference in the
second positon:
(defmacro reducer [function]
(eval (list with-copy function 2)))
(reduce (reducer +) 0 &[1 2 3])
=> 6