on master help sign in
Add combine to the signal library.
d27ab15evancz
libraries/Signal.elm+19−18
@@ -7,7 +7,8 @@
7-- Note: There are lift functions up to `lift8`.
7-- Note: There are lift functions up to `lift8`.
8module Signal where
8module Signal where
9
9
10−import Native.Signal as Native
10+import Native.Signal as S
11+import List as L
11
12
12-- Create a constant signal that never changes.
13-- Create a constant signal that never changes.
13constant : a -> Signal a
14constant : a -> Signal a
@@ -40,6 +41,9 @@ merge : Signal a -> Signal a -> Signal a
40-- signals update simultaneously.
41-- signals update simultaneously.
41merges : [Signal a] -> Signal a
42merges : [Signal a] -> Signal a
42
43
44+combine : [Signal a] -> Signal [a]
45+combine = List.foldr (S.lift2 (::)) (S.constant [])
46+
43-- Merge two signals into one, but distinguishing the values by marking the first
47-- Merge two signals into one, but distinguishing the values by marking the first
44-- signal as `Left` and the second signal as `Right`. This allows you to easily
48-- signal as `Left` and the second signal as `Right`. This allows you to easily
45-- fold over non-homogeneous inputs.
49-- fold over non-homogeneous inputs.
@@ -94,6 +98,20 @@ timestamp : Signal a -> Signal (Time, a)
94-- will update one second later than any mouse click.
98-- will update one second later than any mouse click.
95delay : Time -> Signal a -> Signal a
99delay : Time -> Signal a -> Signal a
96
100
101+-- An alias for `lift`. A prettier way to apply a
102+-- function to the current value of a signal.
103+(<~) : (a -> b) -> Signal a -> Signal b
104+
105+-- Signal application. This takes two signals, holding a function and
106+-- a value. It applies the current function to the current value.
107+--
108+-- So the following expressions are equivalent:
109+--
110+-- scene <~ Mouse.x ~ Mouse.y
111+-- lift2 scene Mouse.x Mouse.y
112+(~) : Signal (a -> b) -> Signal a -> Signal b
113+
114+
97{-- TODO: only found in docs
115{-- TODO: only found in docs
98 , ("average", "Int -> Signal Number -> Signal Float", [markdown|
116 , ("average", "Int -> Signal Number -> Signal Float", [markdown|
99
117
@@ -102,22 +120,5 @@ average of the signal over the last `n` events.
102
120
103So `(average 20 (fps 40))` would be the average time between the frames for
121So `(average 20 (fps 40))` would be the average time between the frames for
104the last 20 frames.|])
122the last 20 frames.|])
105− , ("foldp1", "(a -> a -> a) -> Signal a -> Signal a", [markdown|
106−Create a past-dependent signal. The first value on the signal is used
107−as the base case.|])
108− , ("foldp'", "(a -> b -> b) -> (a -> b) -> Signal a -> Signal b", [markdown|
109−Just like foldp, but instead of a base case, you provide a function to be
110−applied to the first value, creating the base case.|])
111− [ ("(<~)", "(a -> b) -> Signal a -> Signal b", [markdown|
112−An alias for `lift`. A prettier way to apply a
113−function to the current value of a signal.|])
114− , ("(~)", "Signal (a -> b) -> Signal a -> Signal b", [markdown|
115−Signal application. This takes two signals, holding a function and
116−a value. It applies the current function to the current value.
117−
118−So the following expressions are equivalent:
119−
120− scene <~ Mouse.x ~ Mouse.y
121− lift2 scene Mouse.x Mouse.y|])
122--}
123--}
123
124