summaryrefslogtreecommitdiff
path: root/tests/demo.apl
blob: 99ef783c6fe6c384075e3f2dda6b6bd8ccd034e8 (plain) (blame)
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
⍝ The following demonstrates message passing between an 'iota sever',
⍝ and a client (the user of the iota function). The client always initiates
⍝ communication, and the server must be running, but the client should not
⍝ be able to notice a difference between monadic ⍳ and the iota function,
⍝ even though one does the computation in a different thread.

iotaServer←{
	msg←1⍨⍇0
	msg≡'stop': ⎕←'Bye bye from indexer'
	(from num)←msg
	num<0: ∇⍵⊣ ('some slow result'⊣⎕DL 2)⍈from
	_←(⍳num)⍈from
	∇⍵
}

id←0 ⍝ this is an invalid thread id, so it is a sane starting point

start←{
	id=0: iotaServer&⍬
	id
}

stop←{
	'stop'⍈id
}

flush←{
	⍺←0
	12::⍺
	msg←1⍨⍇0
	(⍺+1)∇⍵
}

iota←{
	⍺←⍬ ⍝ No timeout by default
	11::'Sorry, the iota server is not running'
	12::'Sorry, the iota server was too slow to respond'
	⍝ Sending messages to a nonexisting thread throws a domain error (11)
	⍝ And if a timeout happens, an error 12 will happen.
	_←(⎕self ⍵)⍈id
	1⍨⍇⍺
}