Skip to content

Commit e2ad5cb

Browse files
committed
Finish week 7
1 parent ed3dfd3 commit e2ad5cb

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

java_and_dataflow/counter.oz

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
declare
2+
fun {Counter S}
3+
fun {Count Lst M}
4+
case Lst of
5+
nil then [M#1]
6+
[] X#Y|T then
7+
if X==M then X#(Y+1)|T
8+
else X#Y|{Count T M} end
9+
end
10+
end
11+
local Cell={NewCell nil} in
12+
fun {Consume Stream}
13+
case Stream of H|T then
14+
Cell:={Count @Cell H}
15+
@Cell|{Consume T}
16+
else _ end
17+
end
18+
end
19+
in
20+
thread {Consume S} end
21+
end
22+
23+
local InS X in
24+
X={Counter InS}
25+
InS=a|b|a|c|_
26+
{Delay 2000}
27+
{Browse X}
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
declare
2+
fun {Producer N}
3+
fun {Prod M}
4+
if M<N+1 then M|{Prod M+1}
5+
else nil end
6+
end
7+
in
8+
{Prod 1}
9+
end
10+
11+
fun {Filter S}
12+
case S of H|T then
13+
if (H mod 2)==1 then H|{Filter T}
14+
else {Filter T} end
15+
else nil end
16+
end
17+
18+
fun {Consumer S}
19+
case S of H|T then
20+
H+{Consumer T}
21+
else 0 end
22+
end
23+
24+
proc {Disp S}
25+
case S of H|T then
26+
{Browse H}
27+
{Disp T}
28+
end
29+
end
30+
31+
32+
local P F in
33+
thread P={Producer 20} end
34+
thread F={Filter P} end
35+
%thread {Disp F} end
36+
thread {Browse {Consumer F}} end
37+
end

java_and_dataflow/queue.oz

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
declare
2+
class Queue
3+
attr cell
4+
5+
meth init
6+
cell:=nil
7+
end
8+
9+
meth size($)
10+
{Length @cell}
11+
end
12+
13+
meth isEmpty($)
14+
@cell==nil
15+
end
16+
17+
meth front($)
18+
if @cell==nil then
19+
raise frontEmptyQueue end
20+
else {Nth @cell {Length @cell}} end
21+
end
22+
23+
meth enqueue(X)
24+
cell:=X|@cell
25+
end
26+
27+
meth dequeue($)
28+
if @cell==nil then
29+
raise dequeueEmptyQueue end
30+
else local Len={Length @cell} H List in
31+
H = {Nth @cell Len}
32+
List=@cell
33+
cell:=nil
34+
for I in Len-1..1;~1 do
35+
cell:={Nth List I}|@cell
36+
end
37+
H
38+
end
39+
end
40+
end
41+
end
42+
43+
local S={New Queue init} in
44+
{Browse {S size($)}}
45+
{Browse {S isEmpty($)}}
46+
%{Browse {S top($)}}
47+
{S enqueue(8)}
48+
{S enqueue(7)}
49+
{Browse {S size($)}}
50+
{Browse {S isEmpty($)}}
51+
{Browse {S front($)}}
52+
{Browse {S dequeue($)}}
53+
{Browse {S size($)}}
54+
{Browse {S front($)}}
55+
%{Browse {S pop($)}}
56+
end

java_and_dataflow/reverse.oz

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare
2+
fun {Reverse S}
3+
local I={NewCell S} in
4+
local Temp={NewCell nil} in
5+
for C in @I do
6+
Temp:=C|@Temp
7+
end
8+
@Temp
9+
end
10+
end
11+
end
12+
13+
{Browse {Reverse [1 2 3 4]}}
14+
{Browse {Reverse nil}}

java_and_dataflow/stack.oz

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
declare
2+
class Stack
3+
attr cell
4+
5+
meth init
6+
cell:=nil
7+
end
8+
9+
meth size($)
10+
{Length @cell}
11+
end
12+
13+
meth isEmpty($)
14+
@cell==nil
15+
end
16+
17+
meth top($)
18+
if @cell==nil then
19+
raise topEmptyStack end
20+
else @cell.1 end
21+
end
22+
23+
meth push(X)
24+
cell:=X|@cell
25+
end
26+
27+
meth pop($)
28+
if @cell==nil then
29+
raise popEmptyStack end
30+
else local H in
31+
H = @cell.1
32+
cell:=@cell.2
33+
H
34+
end
35+
end
36+
end
37+
end
38+
39+
local S={New Stack init} in
40+
{Browse {S size($)}}
41+
{Browse {S isEmpty($)}}
42+
%{Browse {S top($)}}
43+
{S push(8)}
44+
{Browse {S size($)}}
45+
{Browse {S isEmpty($)}}
46+
{Browse {S top($)}}
47+
{Browse {S pop($)}}
48+
%{Browse {S pop($)}}
49+
end
50+
51+
52+
53+
54+
55+

java_and_dataflow/stream.oz

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
declare
2+
proc {Disp S}
3+
case S of H|T then {Browse H} {Disp T} end
4+
end
5+
6+
fun {Prod N}
7+
{Delay 1000}
8+
N|{Prod N+1}
9+
end
10+
11+
fun {Trans S}
12+
case S of N|T then
13+
N*N|{Trans T}
14+
end
15+
end
16+
17+
18+
declare S S2
19+
thread S={Prod 1} end
20+
thread S2={Trans S} end
21+
thread {Disp S2} end
22+
23+
%declare S2 in S=a|b|c|S2
24+
%declare S3 in S2=d|e|f|s3

0 commit comments

Comments
 (0)