-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharp_core.html
More file actions
251 lines (209 loc) · 17.5 KB
/
Sharp_core.html
File metadata and controls
251 lines (209 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
<link rel="Start" href="index.html">
<link rel="previous" href="Sharp.html">
<link rel="next" href="Sharp_event.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of class methods" rel=Appendix href="index_methods.html">
<link title="Index of class types" rel=Appendix href="index_class_types.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="Sharp" rel="Chapter" href="Sharp.html">
<link title="Sharp_core" rel="Chapter" href="Sharp_core.html">
<link title="Sharp_event" rel="Chapter" href="Sharp_event.html">
<link title="Sharp_form" rel="Chapter" href="Sharp_form.html">
<link title="Sharp_vdom" rel="Chapter" href="Sharp_vdom.html">
<link title="Sharp_vdom_subnetwork" rel="Chapter" href="Sharp_vdom_subnetwork.html">
<link title="Sharp_router" rel="Chapter" href="Sharp_router.html">
<link title="Sharp_ticker" rel="Chapter" href="Sharp_ticker.html"><title>Sharp_core</title>
</head>
<body>
<div class="navbar"><a class="pre" href="Sharp.html" title="Sharp">Previous</a>
<a class="up" href="index.html" title="Index">Up</a>
<a class="post" href="Sharp_event.html" title="Sharp_event">Next</a>
</div>
<h1>Module <a href="type_Sharp_core.html">Sharp_core</a></h1>
<pre><span class="keyword">module</span> Sharp_core: <code class="code">sig</code> <a href="Sharp_core.html">..</a> <code class="code">end</code></pre><div class="info module top">
This the main module containing the code of the project. It defines how
signals operate and how they can be used. Simply put, signals are
time-varying value to which the code can react.
<p>
More precisely, they are functions taking the time and returning a value of
the appropriate type and a new signal which can be used with future times.
<p>
Signals are functors, applicative functors and monads. They can be combined
just like any other applicative functors or monads. For example,
<code class="code">map f s1</code> maps the function <code class="code">f</code> on the values taken by the signal <code class="code">s1</code>
returning a signal of the results. <code class="code">apply (map f s1) s2</code> or, more
idiomatically, <code class="code">f <$> s1 <*> s2</code> applies the function <code class="code">f</code> on the values taken
by the signals <code class="code">s1</code> and <code class="code">s2</code> and returns a signal of the results. Of course,
<code class="code">g <$> s1 <*> s2 <*> s3</code> will work with a function <code class="code">g</code> taking three arguments
(and so forth). Finally, you can bind two signals sequentially with the
monadic interface. Say you have a signal <code class="code">s1</code> returning the time and a
function <code class="code">f</code> taking the time as an argument and returning a signal of
integers, you can bind both with <code class="code">bind s1 f</code> or <code class="code">s1 >>= f</code> to yield a signal
of integers. The monadic bind on signals can be a bit tricky but <code class="code">map</code> and
<code class="code">apply</code> should be more than often in most cases.
<p>
Signals are useless if they are not connected to the real world somehow.
<p>
To input data into a network of signals, you will need the function <code class="code">event</code>.
It returns a optional signal (i.e. a signal of type <code class="code">'a option t</code>, that is, a
signal taking values of type <code class="code">'a option</code> together with a function to trigger
the event. Whenever this trigger is called with a value <code class="code">x</code>, the signal will
take the value <code class="code">Some x</code>, otherwise it will be <code class="code">None</code>.
<p>
In order to react to events, there are a few functions which roughly work the
same way. The ones you will use the most are <code class="code">react_with</code> and <code class="code">react</code> which
is a specialised version of <code class="code">react_with</code>. <code class="code">react_with</code> reacts to a signal of
type <code class="code">'a option t</code> when it has a value and another "normal signal" and
executes a given function with the values taken by the signals. Here is a
simple example:
<p>
<pre class="codepre"><code class="code"> let (int_signal, trigger_int) = event () in
react_with int_signal time (fun x t ->
print_endline ("The time is " ^ string_of_float(t)
^ "and the event has been triggered with the value: "
^ string_of_int(x))
)
</code></pre>
<p>
This module also includes a couple of functions transforming signals. Other
modules provide ways of binding signal networks to the external world, e.g.
reacting to the click of a button.<br>
</div>
<hr width="100%">
<pre><span id="TYPEtime"><span class="keyword">type</span> <code class="type"></code>time</span> = <code class="type">float</code> </pre>
<pre><span id="TYPEt"><span class="keyword">type</span> <code class="type">'a</code> t</span> </pre>
<div class="info ">
Type of signals.<br>
</div>
<pre><span id="VALconst"><span class="keyword">val</span> const</span> : <code class="type">'a -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
The simplest signal, always returning the same value. Note that, for
convenience and to reflect the intent of your code more accurately, you
can also use its aliases: return, pure and lift0.<br>
</div>
<pre><span id="VALtime"><span class="keyword">val</span> time</span> : <code class="type"><a href="Sharp_core.html#TYPEtime">time</a> <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Return the current time.<br>
</div>
<pre><span id="VALmap"><span class="keyword">val</span> map</span> : <code class="type">('a -> 'b) -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Map a function onto a signal's (current and future) values.<br>
</div>
<pre><span id="VAL(<$>)"><span class="keyword">val</span> (<$>)</span> : <code class="type">('a -> 'b) -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">map</code>.<br>
</div>
<pre><span id="VALpure"><span class="keyword">val</span> pure</span> : <code class="type">'a -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">const</code>.<br>
</div>
<pre><span id="VALapply"><span class="keyword">val</span> apply</span> : <code class="type">('a -> 'b) <a href="Sharp_core.html#TYPEt">t</a> -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Signals are applicative functors.<br>
</div>
<pre><span id="VAL(<*>)"><span class="keyword">val</span> (<*>)</span> : <code class="type">('a -> 'b) <a href="Sharp_core.html#TYPEt">t</a> -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">apply</code>.<br>
</div>
<pre><span id="VALlift0"><span class="keyword">val</span> lift0</span> : <code class="type">'a -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
The following functions lift a function of values into a function of
signals.<br>
</div>
<pre><span id="VALlift"><span class="keyword">val</span> lift</span> : <code class="type">('a -> 'b) -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre>
<pre><span id="VALlift2"><span class="keyword">val</span> lift2</span> : <code class="type">('a -> 'b -> 'c) -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a> -> 'c <a href="Sharp_core.html#TYPEt">t</a></code></pre>
<pre><span id="VALlift3"><span class="keyword">val</span> lift3</span> : <code class="type">('a -> 'b -> 'c -> 'd) -><br> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a> -> 'c <a href="Sharp_core.html#TYPEt">t</a> -> 'd <a href="Sharp_core.html#TYPEt">t</a></code></pre>
<pre><span id="VALlift4"><span class="keyword">val</span> lift4</span> : <code class="type">('a -> 'b -> 'c -> 'd -> 'e) -><br> 'a <a href="Sharp_core.html#TYPEt">t</a> -><br> 'b <a href="Sharp_core.html#TYPEt">t</a> -> 'c <a href="Sharp_core.html#TYPEt">t</a> -> 'd <a href="Sharp_core.html#TYPEt">t</a> -> 'e <a href="Sharp_core.html#TYPEt">t</a></code></pre>
<pre><span id="VALlift5"><span class="keyword">val</span> lift5</span> : <code class="type">('a -> 'b -> 'c -> 'd -> 'e -> 'f) -><br> 'a <a href="Sharp_core.html#TYPEt">t</a> -><br> 'b <a href="Sharp_core.html#TYPEt">t</a> -><br> 'c <a href="Sharp_core.html#TYPEt">t</a> -> 'd <a href="Sharp_core.html#TYPEt">t</a> -> 'e <a href="Sharp_core.html#TYPEt">t</a> -> 'f <a href="Sharp_core.html#TYPEt">t</a></code></pre>
<pre><span id="VALmap_opt"><span class="keyword">val</span> map_opt</span> : <code class="type">('a -> 'b) -> 'a option <a href="Sharp_core.html#TYPEt">t</a> -> 'b option <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
A kind of <code class="code">map</code> on optional signals.<br>
</div>
<pre><span id="VAL(<$?>)"><span class="keyword">val</span> (<$?>)</span> : <code class="type">('a -> 'b) -> 'a option <a href="Sharp_core.html#TYPEt">t</a> -> 'b option <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">map_opt</code>.<br>
</div>
<pre><span id="VALapply_opt"><span class="keyword">val</span> apply_opt</span> : <code class="type">('a -> 'b) option <a href="Sharp_core.html#TYPEt">t</a> -><br> 'a option <a href="Sharp_core.html#TYPEt">t</a> -> 'b option <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
A kind <code class="code">apply</code> on optional signals.<br>
</div>
<pre><span id="VAL(<*?>)"><span class="keyword">val</span> (<*?>)</span> : <code class="type">('a -> 'b) option <a href="Sharp_core.html#TYPEt">t</a> -><br> 'a option <a href="Sharp_core.html#TYPEt">t</a> -> 'b option <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">apply_opt</code>.<br>
</div>
<pre><span id="VALsequence"><span class="keyword">val</span> sequence</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> list -> 'a list <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Combine a list of signals into a signal of list.<br>
</div>
<pre><span id="VALreturn"><span class="keyword">val</span> return</span> : <code class="type">'a -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">const</code>.<br>
</div>
<pre><span id="VALjoin"><span class="keyword">val</span> join</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> <a href="Sharp_core.html#TYPEt">t</a> -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Flatten a nested signal. When called, the next signal will be determined by
the outer signal and the new one determined by the inner one will be
discarded.<br>
</div>
<pre><span id="VALbind"><span class="keyword">val</span> bind</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> -> ('a -> 'b <a href="Sharp_core.html#TYPEt">t</a>) -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Monadic bind. It allows to sequentially compose signals together.<br>
</div>
<pre><span id="VAL(>>=)"><span class="keyword">val</span> (>>=)</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> -> ('a -> 'b <a href="Sharp_core.html#TYPEt">t</a>) -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Alias for <code class="code">bind</code>.<br>
</div>
<pre><span id="VAL(>>)"><span class="keyword">val</span> (>>)</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Like <code class="code">>>=</code> but it discards the value of the first signal.<br>
</div>
<pre><span id="VALperform"><span class="keyword">val</span> perform</span> : <code class="type">?force:bool -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> ('a -> unit) -> unit</code></pre><div class="info ">
Perform an action every time the signal changes value.
<p>
To be precise, the signal could have the same value as before but something
happened upstream. If some signals have been triggered and the signal passed
to <code class="code">perform</code> somehow depends on it, through calls to <code class="code">map</code> or <code class="code">apply</code> for
instance, the function given to <code class="code">perform</code> will be called.<br>
</div>
<pre><span id="VALperform_state"><span class="keyword">val</span> perform_state</span> : <code class="type">?force:bool -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> init:'b -> f:('b -> 'a -> 'b) -> unit</code></pre><div class="info ">
Like perform but maintain a state between calls.<br>
</div>
<pre><span id="VALreact"><span class="keyword">val</span> react</span> : <code class="type">'a option <a href="Sharp_core.html#TYPEt">t</a> -> ('a -> unit) -> unit</code></pre><div class="info ">
Run the function when the optional signal takes a value.<br>
</div>
<pre><span id="VALreact_with"><span class="keyword">val</span> react_with</span> : <code class="type">'a option <a href="Sharp_core.html#TYPEt">t</a> -> 'b <a href="Sharp_core.html#TYPEt">t</a> -> ('a -> 'b -> unit) -> unit</code></pre><div class="info ">
Same as <code class="code">react</code> with an additional signal.
<p>
This is useful for things like
<code class="code">react_with button_cliked data_signal (fun () data -> (* do something *))</code>.<br>
</div>
<pre><span id="VALevent"><span class="keyword">val</span> event</span> : <code class="type">unit -> 'a option <a href="Sharp_core.html#TYPEt">t</a> * ('a -> unit)</code></pre><div class="info ">
Create a optional signal with the function to give the signal the given
value at the current time.<br>
</div>
<pre><span id="VALconnected_event"><span class="keyword">val</span> connected_event</span> : <code class="type">(('a -> unit) -> unit -> unit) -><br> 'a option <a href="Sharp_core.html#TYPEt">t</a> * ('a -> unit) * (unit -> unit)</code></pre><div class="info ">
Create a signal from a callback and returns the signal, the trigger function
and another callback to disconnect the event.
<p>
The first callback takes the trigger function and should return the second
callback to disconnect the event. To make its use clearer, imagine you want
to create a signal which should be triggered automatically when some JS
event occurs. The first callback would be a function starting to listen to
a JS event, calling the trigger function when the event occurs and returning
a callback to destroy the listener when it's called.<br>
</div>
<pre><span id="VALon"><span class="keyword">val</span> on</span> : <code class="type">'a option <a href="Sharp_core.html#TYPEt">t</a> -> init:'b -> f:('b -> 'a -> 'b) -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Return a signal starting with the value <code class="code">init</code> and changing this value based
on the previous one and a value of an optional signal when it takes one.<br>
</div>
<pre><span id="VALlast"><span class="keyword">val</span> last</span> : <code class="type">'a option <a href="Sharp_core.html#TYPEt">t</a> -> init:'a -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Return the last value taken by an optional signal, <code class="code">init</code> if none.<br>
</div>
<pre><span id="VALtoggle"><span class="keyword">val</span> toggle</span> : <code class="type">'b option <a href="Sharp_core.html#TYPEt">t</a> -> init:bool -> bool <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Toggle between <code class="code">true</code> and <code class="code">false</code> each time the given optional signal takes
a value.<br>
</div>
<pre><span id="VALcount"><span class="keyword">val</span> count</span> : <code class="type">?init:int -> 'b option <a href="Sharp_core.html#TYPEt">t</a> -> int <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Count the number of times the optional signal has had a value.<br>
</div>
<pre><span id="VALupon"><span class="keyword">val</span> upon</span> : <code class="type">?init:'a -> 'c option <a href="Sharp_core.html#TYPEt">t</a> -> 'a <a href="Sharp_core.html#TYPEt">t</a> -> 'a <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
The new signal will keep the same value until the optional value takes a
value.<br>
</div>
<pre><span id="VALfold"><span class="keyword">val</span> fold</span> : <code class="type">'a <a href="Sharp_core.html#TYPEt">t</a> -> init:'b -> f:('b -> 'a -> 'b) -> 'b <a href="Sharp_core.html#TYPEt">t</a></code></pre><div class="info ">
Fold the values taken by a signal.
<p>
It is important to understand that signals are only called when something
happened. It is therefore not continuous.<br>
</div>
</body></html>