@@ -7,46 +7,64 @@
7-- Note: There are lift functions up to `lift8`.
7-- Note: There are lift functions up to `lift8`.
8module Signal where
8module Signal where
10−import Native.Signal as S
10+import Native.Signal as Native
11−import List as L
11+import List (foldr)
13-- Create a constant signal that never changes.
13-- Create a constant signal that never changes.
14constant : a -> Signal a
14constant : a -> Signal a
15+constant = Native.constant
16-- Transform a signal with a given function.
17-- Transform a signal with a given function.
17lift : (a -> b) -> Signal a -> Signal b
18lift : (a -> b) -> Signal a -> Signal b
19-- Combine two signals with a given function.
21-- Combine two signals with a given function.
20lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
22lift2 : (a -> b -> c) -> Signal a -> Signal b -> Signal c
22-- Combine three signals with a given function.
25-- Combine three signals with a given function.
23lift3 : (a -> b -> c -> d) -> Signal a -> Signal b -> Signal c -> Signal d
26lift3 : (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
29lift4 : (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
32lift5 : (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)
35lift6 : (a -> b -> c -> d -> e -> f -> g)
27 -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g
36 -> Signal a -> Signal b -> Signal c -> Signal d -> Signal e -> Signal f -> Signal g
28lift7 : (a -> b -> c -> d -> e -> f -> g -> h)
39lift7 : (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
40 -> 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)
43lift8 : (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
44 -> 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
48-- Create a past-dependent signal. Each value given on the input signal will
34-- be accumulated, producing a new output value.
49-- be accumulated, producing a new output value.
36-- For instance, `(foldp (\\t acc -> acc + 1) 0 (Time.every second))` increments every second.
51-- For instance, `(foldp (\\t acc -> acc + 1) 0 (Time.every second))` increments every second.
37foldp : (a -> b -> b) -> b -> Signal a -> Signal b
52foldp : (a -> b -> b) -> b -> Signal a -> Signal b
39-- Merge two signals into one, biased towards the first signal if both signals
55-- Merge two signals into one, biased towards the first signal if both signals
40-- update at the same time.
56-- update at the same time.
41merge : Signal a -> Signal a -> Signal a
57merge : Signal a -> Signal a -> Signal a
43-- Merge many signals into one, biased towards the left-most signal if multiple
60-- Merge many signals into one, biased towards the left-most signal if multiple
44-- signals update simultaneously.
61-- signals update simultaneously.
45merges : [Signal a] -> Signal a
62merges : [Signal a] -> Signal a
63+merges = Native.merges
47-- Combine a list of signals into a signal of lists.
65-- Combine a list of signals into a signal of lists.
48combine : [Signal a] -> Signal [a]
66combine : [Signal a] -> Signal [a]
49−combine = L.foldr (S.lift2 (::)) (S.constant [])
67+combine = foldr (Native.lift2 (::)) (Native.constant [])
51 -- Merge two signals into one, but distinguishing the values by marking the first
69 -- 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
70 -- signal as `Left` and the second signal as `Right`. This allows you to easily
@@ -55,19 +73,23 @@ combine = L.foldr (S.lift2 (::)) (S.constant [])
56-- Count the number of events that have occured.
74-- Count the number of events that have occured.
57count : Signal a -> Signal Int
75count : Signal a -> Signal Int
59-- Count the number of events that have occured that satisfy a given predicate.
78-- Count the number of events that have occured that satisfy a given predicate.
60countIf : (a -> Bool) -> Signal a -> Signal Int
79countIf : (a -> Bool) -> Signal a -> Signal Int
80+countIf = Native.countIf
62-- Keep only events that satisfy the given predicate. Elm does not allow
82-- 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
83-- undefined signals, so a base case must be provided in case the predicate is
64-- never satisfied.
84-- never satisfied.
65keepIf : (a -> Bool) -> a -> Signal a -> Signal a
85keepIf : (a -> Bool) -> a -> Signal a -> Signal a
86+keepIf = Native.keepIf
67-- Drop events that satisfy the given predicate. Elm does not allow undefined
88-- 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
89-- signals, so a base case must be provided in case the predicate is never
69-- satisfied.
90-- satisfied.
70dropIf : (a -> Bool) -> a -> Signal a -> Signal a
91dropIf : (a -> Bool) -> a -> Signal a -> Signal a
92+dropIf = Native.dropIf
72-- Keep events only when the first signal is true. When the first signal becomes
94-- 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
95-- true, the most recent value of the second signal will be propagated. Until
@@ -75,6 +97,7 @@ dropIf : (a -> Bool) -> a -> Signal a -> Signal a
75-- not allow undefined signals, so a base case must be provided in case the first
97-- not allow undefined signals, so a base case must be provided in case the first
76-- signal is never true.
98-- signal is never true.
77keepWhen : Signal Bool -> a -> Signal a -> Signal a
99keepWhen : Signal Bool -> a -> Signal a -> Signal a
100+keepWhen = Native.keepWhen
79-- Drop events when the first signal is true. When the first signal becomes false,
102-- 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
103-- the most recent value of the second signal will be propagated. Until the first
@@ -82,22 +105,26 @@ keepWhen : Signal Bool -> a -> Signal a -> Signal a
82-- undefined signals, so a base case must be provided in case the first signal is
105-- undefined signals, so a base case must be provided in case the first signal is
83-- always true.
106-- always true.
84dropWhen : Signal Bool -> a -> Signal a -> Signal a
107dropWhen : Signal Bool -> a -> Signal a -> Signal a
108+dropWhen = Native.dropWhen
86-- Drop sequential repeated values. For example, if a signal produces the
110-- 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
111-- sequence `[1,1,2,2,1]`, it becomes `[1,2,1]` by dropping the values that
88-- are the same as the previous value.
112-- are the same as the previous value.
89dropRepeats : Signal a -> Signal a
113dropRepeats : Signal a -> Signal a
114+dropRepeats = Native.dropRepeats
91-- Sample from the second input every time an event occurs on the first input.
116-- 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
117-- For example, `(sampleOn clicks (every second))` will give the approximate
93-- time of the latest click.
118-- time of the latest click.
94sampleOn : Signal a -> Signal b -> Signal b
119sampleOn : Signal a -> Signal b -> Signal b
120+sampleOn = Native.sampleOn
96-- An alias for `lift`. A prettier way to apply a
122-- An alias for `lift`. A prettier way to apply a
97-- function to the current value of a signal.
123-- function to the current value of a signal.
99-- lift f signal == f <~ signal
125-- lift f signal == f <~ signal
100(<~) : (a -> b) -> Signal a -> Signal b
126(<~) : (a -> b) -> Signal a -> Signal b
102-- Signal application. This takes two signals, holding a function and
129-- Signal application. This takes two signals, holding a function and
103-- a value. It applies the current function to the current value.
130-- a value. It applies the current function to the current value.
@@ -107,3 +134,4 @@ sampleOn : Signal a -> Signal b -> Signal b
107-- scene <~ Window.dimensions ~ Mouse.position
134-- scene <~ Window.dimensions ~ Mouse.position
108-- lift2 scene Window.dimensions Mouse.position
135-- lift2 scene Window.dimensions Mouse.position
109(~) : Signal (a -> b) -> Signal a -> Signal b
136(~) : Signal (a -> b) -> Signal a -> Signal b
137+(~) = Native.lift2 (\f x -> f x)