on 727d708 help sign in
elk/libraries/Signal.elm
109 lines 4.8 kB history raw
2-- The library for general signal manipulation. Some useful functions for
3-- working with time (e.g. setting FPS) and combining signals and time (e.g.
4-- delaying updates, getting timestamps) can be found in the
5-- [`Time`](/docs/Signal/Time.elm) library.
6--
7-- Note: There are lift functions up to `lift8`.
8module Signal where
10import Native.Signal as S
11import List as L
13-- Create a constant signal that never changes.
14constant : a -> Signal a
16-- Transform a signal with a given function.
17lift : (a -> b) -> Signal a -> Signal b
19-- Combine two signals with a given function.
20lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
22-- Combine three signals with a given function.
23lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
24lift4 : (a -> b -> c -> d -> e) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e
25lift5 : (a -> b -> c -> d -> e -> f) -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f
26lift6 : (a -> b -> c -> d -> e -> f -> g)
27 -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g
28lift7 : (a -> b -> c -> d -> e -> f -> g -> h)
29 -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h
30lift8 : (a -> b -> c -> d -> e -> f -> g -> h -> i)
31 -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g -> Signal h -> Signal i
33-- Create a past-dependent signal. Each value given on the input signal will
34-- be accumulated, producing a new output value.
35--
36-- For instance, `(foldp (\\t acc -> acc + 1) 0 (Time.every second))` increments every second.
37foldp : (a -> b -> b) -> b -> Signal a -> Signal b
39-- Merge two signals into one, biased towards the first signal if both signals
40-- update at the same time.
41merge : Signal a -> Signal a -> Signal a
43-- Merge many signals into one, biased towards the left-most signal if multiple
44-- signals update simultaneously.
45merges : [Signal a] -> Signal a
47-- Combine a list of signals into a signal of lists.
48combine : [Signal a] -> Signal [a]
49combine = L.foldr (S.lift2 (::)) (S.constant [])
51 -- Merge two signals into one, but distinguishing the values by marking the first
52 -- signal as `Left` and the second signal as `Right`. This allows you to easily
53 -- fold over non-homogeneous inputs.
54 -- mergeEither : Signal a -> Signal b -> Signal (Either a b)
56-- Count the number of events that have occured.
57count : Signal a -> Signal Int
59-- Count the number of events that have occured that satisfy a given predicate.
60countIf : (a -> Bool) -> Signal a -> Signal Int
62-- Keep only events that satisfy the given predicate. Elm does not allow
63-- undefined signals, so a base case must be provided in case the predicate is
64-- never satisfied.
65keepIf : (a -> Bool) -> a -> Signal a -> Signal a
67-- Drop events that satisfy the given predicate. Elm does not allow undefined
68-- signals, so a base case must be provided in case the predicate is never
69-- satisfied.
70dropIf : (a -> Bool) -> a -> Signal a -> Signal a
72-- Keep events only when the first signal is true. When the first signal becomes
73-- true, the most recent value of the second signal will be propagated. Until
74-- the first signal becomes false again, all events will be propagated. Elm does
75-- not allow undefined signals, so a base case must be provided in case the first
76-- signal is never true.
77keepWhen : Signal Bool -> a -> Signal a -> Signal a
79-- Drop events when the first signal is true. When the first signal becomes false,
80-- the most recent value of the second signal will be propagated. Until the first
81-- signal becomes true again, all events will be propagated. Elm does not allow
82-- undefined signals, so a base case must be provided in case the first signal is
83-- always true.
84dropWhen : Signal Bool -> a -> Signal a -> Signal a
86-- Drop sequential repeated values. For example, if a signal produces the
87-- sequence `[1,1,2,2,1]`, it becomes `[1,2,1]` by dropping the values that
88-- are the same as the previous value.
89dropRepeats : Signal a -> Signal a
91-- Sample from the second input every time an event occurs on the first input.
92-- For example, `(sampleOn clicks (every second))` will give the approximate
93-- time of the latest click.
94sampleOn : Signal a -> Signal b -> Signal b
96-- An alias for `lift`. A prettier way to apply a
97-- function to the current value of a signal.
98--
99-- lift f signal == f <~ signal
100(<~) : (a -> b) -> Signal a -> Signal b
102-- Signal application. This takes two signals, holding a function and
103-- a value. It applies the current function to the current value.
104--
105-- So the following expressions are equivalent:
106--
107-- scene <~ Window.dimensions ~ Mouse.position
108-- lift2 scene Window.dimensions Mouse.position
109(~) : Signal (a -> b) -> Signal a -> Signal b