Zelkova Api Documentation

Version: 0.4.0

jamesmacaulay.zelkova.signal

This is Zelkova’s core namespace.

activate-when

(activate-when switch-sig value-sig)

Returns a transformation of value-sig whose entire graph of signal dependencies—aside from input nodes—is skipped unless switch-sig’s state is truthy. This is accomplished by walking value-sig’s graph and wrapping its input signals with keep-when. The intial value of a signal returned from activate-when is always equal to the initial value of value-sig.

async

(async source)

Returns an “asynchronous” version of source, splitting off a new subgraph which does not maintain consistent event ordering relative to the main graph. In exchange, signals which depend on an async signal don’t have to wait for the source to finish computing new values. This function is mainly useful in multithreaded environments when you don’t want a slow computation to block the whole graph.

combine

(combine sigs)

Combines a sequence of signals into a signal of vectors. Equivalent to (signal/map vector sig1, sig2, ...)

constant

(constant x)

Returns a constant signal of the given value.

count

(count sig)

Returns a signal whose values are the number of fresh values emitted so far from sig. Repeated equal values will be counted so long as they are fresh, so if you don’t want to count repeats then you need to (count (drop-repeats sig)) instead.

count-if

(count-if pred sig)

Like count, but only increments the counter if the fresh value emitted from sig satisfies the predicate funtion pred. For example, (count-if odd? numbers) returns a signal of how many times the numbers signal emitted an odd number.

drop-if

(drop-if pred sig)(drop-if pred base sig)

Like keep-if, but drops values which match the predicate.

drop-repeats

(drop-repeats sig)

Returns a signal which relays values of sig, but drops repeated equal values.

drop-when

(drop-when switch-sig value-sig)(drop-when switch-sig base value-sig)

Like keep-when, but only relays values when switch-sig is falsy.

foldp

(foldp f base source)

Create a past-dependent signal (“fold into the past”). The values of a foldp signal are obtained by calling f with two arguments: the current value of the source signal, and the previous value of the new foldp signal (acting as the “accumulator”). init provides the initial value of the new signal, and therefore acts as the seed accumulator.

indexed-updates

(indexed-updates signal-map)

Takes a map whose values are signals, to be used as a template. Returns a new signal whose values are maps that include an entry for every signal in signal-map with a fresh value. For example, assuming that signal-map is:

{:a sig-a
 :b sig-b
 :c sig-c}

Then when sig-a has a fresh value of “foo”, sig-b’s value is cached, and sig-c has a fresh value of “bar”, then the indexed-updates signal would emit {:a "foo" :c "bar"}. When none of the signals have fresh values, no value is emitted from theindexed-updates` signal. This means that this signal never emits an empty map.

input

(input init)(input init topic)(input init topic value-source)

Returns an input signal with initial value init. The signal propagates values from events which match some topic. An asynchronous value-source may be provided, which will be used as the default value source for the given event topic. value-source may take the following forms:

  • a function taking a live graph and an options map, and returns a channel of values
  • a channel of values
  • a mult of some such value channel

keep-if

(keep-if pred sig)(keep-if pred base sig)

Returns a signal which relays values from sig, but discards any which don’t match the given predicate function pred. If a base value is provided, it will be the initial value of the returned signal if the initial value of sig does not match the predicate. If no base is provided then the returned signal will always have the same initial value as sig, even if it does not match the predicate.

keep-if-msg-xform

(keep-if-msg-xform pred)

keep-when

(keep-when switch-sig value-sig)(keep-when switch-sig base value-sig)

Returns a new signal which relays values from value-sig, but only when the current value of switch-sig is truthy.

log

(log sig)

A little convenience helper which logs signal values with pr before propagating them unchanged.

map

(map f & sources)

Takes a mapping function f and any number of signal sources, and returns a signal of values obtained by applying f to the values from the source signals.

mapseq

(mapseq f sources)

Takes a mapping function f and a sequence of signal sources, and returns a signal of values obtained by applying f to the values from the source signals.

merge

(merge & sigs)

Takes any number of source signals sigs, and returns a new signal which relays fresh values from all of the source signals. When more than one source has fresh values at the same time, the first (leftmost) signal will take precedence and the other values will be discarded. The initial value of the returned signal is equal to the initial value of the first source signal.

mergeseq

(mergeseq sigs)

Takes a sequence of signals sigs, and returns a new signal which relays fresh values from all of the source signals. When more than one source has fresh values at the same time, the first (leftmost) signal in sigs will take precedence and the other values will be discarded. The initial value of the returned signal is equal to the initial value of the first source signal.

pipe-to-atom

(pipe-to-atom x)(pipe-to-atom x atm)(pipe-to-atom x atm ks)

Pipes fresh values from a live graph into an atom. If x is a signal, it is spawned as a live graph first. If no atom is provided, then a new atom is created which takes its initial value from that of the given signal or graph. If an existing atom is provided along with a sequence of keys ks, then fresh values will be inserted into the atom’s value using swap! with assoc-in. If ks is not present, then the whole atom value is replaced with reset!.

pipeline

(pipeline xform base sig)

Takes a stateless transducer xform, a fallback value base, and a signal sig. Returns a new signal which pipes values from sig through xform. Because transducers may filter out values, you must provide a base which will be used as the derived signal’s initial value if the initial value of sig ends up being filtered. If multiple values are emitted from the transduction of the initial value of sig, then the initial value of the new signal will be the last of those emitted. Stateful transducers will give unexpected results and are not supported.

reductions

(reductions f source)(reductions f init source)

Create a past-dependent signal like foldp, with two differences: * calls f with the arguments reversed to align with Clojure: the first argument is the accumulator, the second is the current value of source. * if init is omitted, the initial value of the new signal will be obtained by calling f with no arguments.

sample-on

(sample-on sampler-sig value-sig)

Sample the current value of value-sig every time sampler-sig updates with a fresh value. For example, (sample-on mouse/clicks mouse/position) returns a signal of click positions.

select-step

(select-step init & signals-and-handlers)

Takes an initial value and a map whose keys are signals and whose values are reducing functions. Returns a past-dependent signal like reductions, except each signal has its own reducing function to use when that signal updates. If more than one source signal updates from the same input event, then each applicable reducing function is called to transform the state value in the same order as they are defined in signal-handlers-map.

spawn

(spawn s)(spawn s opts)

Take an inert signal and produce a live, running graph.

splice

(splice setup! source)(splice setup! init-fn source)

Splice into the signal graph on the level of core.async channels. Takes a setup! function which is called when the source signal gets wired up into a live graph. The setup! function is passed two arguments: a from channel and a to channel, in that order. The function is expected to be a consumer of the from channel and a producer on the to channel, and should close the to channel when the from channel is closed. There are no requirements for how many values should be put on the to channel or when they should be sent. splice returns a signal with an initial returned from init-fn. init-fn takes two functions, a live-graph and an opts map. If no init-fn is provided, then the initial value of source is used. The returned signal asynchronously produces whichever values are put on the to channel in the setup! function.

take-nothing

(take-nothing rf)

template

(template signal-map)

Takes a map whose values are signals, to be used as a template. Returns a new signal whose values are maps of the same form as signal-map, but with the current value of each signal in place of the signal itself.

to-chan

(to-chan s & args)

Takes a signal s and returns a channel of fresh values, passing any extra args to the chan constructor.

write-port

(write-port init)(write-port init topic)

Takes an init value and an optional topic, and returns an input signal which satisfies core.async’s WritePort protocol. This allows you to put values onto the signal as if it were a channel. If the write-port is being used in multiple live graphs, each value put onto the write-port is sent to all graphs.