Skip to content

Commit 5d27ba1

Browse files
authored
Update 2024-10-01-CollaborativeEditorPatterns.md
1 parent 83e8711 commit 5d27ba1

File tree

1 file changed

+78
-48
lines changed

1 file changed

+78
-48
lines changed

_posts/2024-10-01-CollaborativeEditorPatterns.md

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ There are too many books and papers that deal with a multitude of algorithms.
1818

1919
I will add some sections like this to explain the reason for experimenting with new paradigms. In many cases the code is too dense and will seem complicated when new techniques are introduced needlessly but Effect handlers are interesting to learn. Application though should be selective. There will be many usecases for these in the future.
2020

21-
So the following is an experiment.
21+
So the following is an experiment in the sense that the code quality will be fixed only later. So, for example, global references are used to complete the code even though they
22+
are unnecessary.
2223

2324
# Breaking paragraphs into lines
2425

@@ -36,50 +37,53 @@ type entry = {
3637
mutable score : int}
3738

3839

39-
let l = ref []
40+
let gl = ref []
4041

4142
let is_space= function ' ' -> true | _ -> false
4243

43-
let final_state (start, idx) =
44+
let final_state l (start, idx) =
4445

4546
let e = {first = start; last = idx; next = -1; score = -1} in
4647
l := !l @ [e];
47-
Printf.printf " Final state %d %d%!" start idx
48+
Printf.printf " Final state %d %d\n" start idx;
49+
Printf.printf " Size of list is %d\n" (List.length !l);
50+
l
4851

4952
type _ Effect.t += Skipping_spaces : (int * int ) -> unit Effect.t
5053

51-
let parabreak text ideal_width max_width =
54+
let parabreak l text ideal_width max_width =
55+
Printf.printf " parabreak\n";
5256
let start = ref 0 in
5357
let idx,_ =
5458
String.fold_left (fun (idx,word_or_space) c ->
5559
match (is_space c, word_or_space) with
5660
| (true,true)
5761
->
58-
(* Printf.printf "Space at index %d, skipping\n%!" idx; *)
62+
Printf.printf "Space at index %d, skipping\n" idx;
5963

6064
if !start < idx then
61-
perform (Skipping_spaces (!start,idx));
65+
perform (Skipping_spaces (!start,idx) );
6266

6367
start := idx + 1;
6468
(idx + 1,false);
6569
| (true,false)
66-
-> Printf.printf "No Space at index %d, skipping\n%!" idx;
70+
-> Printf.printf "No Space at index %d, skipping\n" idx;
6771
(idx + 1,false)
6872
| (false,_)
6973
->(idx + 1,true)) (0,false) text
7074
in
7175

7276
if !start < idx then
77+
Printf.printf "Final - Skipping spaces %d %d\n" !start idx;
7378
perform (Skipping_spaces (!start,idx));
74-
final_state (!start,idx)
75-
let effective text =
76-
Printf.printf "Handling effects%!";
77-
match_with (fun () -> parabreak text 10 29)
79+
final_state l (!start,idx)
80+
let effective text l =
81+
match_with (fun () -> parabreak l text 10 29)
7882
()
79-
{ effc = (fun (type c) (eff: c Effect.t) ->
80-
match eff with
83+
{ effc = (fun (type c) (eff1: c Effect.t) ->
84+
match eff1 with
8185
| Skipping_spaces (s,s1) -> Some (fun (k: (c,_) continuation) ->
82-
(* Printf.printf "Skipping spaces \"%d %d\"\n%!" s s1; *)
86+
Printf.printf "Skipping spaces \"%d %d\"\n" s s1;
8387

8488
let e = {first = s; last = s1; next = -1; score = -1} in
8589
l := !l @ [e];
@@ -91,23 +95,30 @@ let effective text =
9195
| e -> raise e
9296
);
9397
(* retc = fun _ -> failwith "Fatal error" *)
98+
(* retc = (fun res -> Printf.printf "Computation returned %d: \n" (List.length !l)) *)
99+
100+
retc = (fun _ -> l)
94101

95-
retc = (fun res -> Printf.printf "Computation returned: \n")
96102
}
97103

98104

99105
type _ Effect.t += Plass_break : int -> unit Effect.t
100106

101107
let rec plassbreak indent idx idealwidth maxwidth =
102-
let
103-
lastrecord = List.nth !l idx in
108+
(* Printf.printf "Size of list in plassbreak is %d\n " (List.length !gl); *)
109+
110+
if idx < (List.length !gl) then(
111+
let jdx = idx + 1 in
112+
let lastrecord = List.nth !gl idx in
113+
Printf.printf "let record = List.nth !gl idx in - %d\n" idx;
104114
let llen = ref (lastrecord.last - lastrecord.first) in
105115
let bscore = idealwidth - !llen in
106-
let bscore = ref (bscore * 2) in
107-
let btail = ref (idx + 1) in
116+
let bscore = ref (bscore * bscore) in
117+
let btail = ref jdx in
108118
let _ =
109119
List.fold_left (fun acc entry ->
110120

121+
if acc < (List.length !gl) then(
111122
match entry with
112123
| {first; last; next; score} ->
113124
let wwidth = last - first in
@@ -121,92 +132,111 @@ let rec plassbreak indent idx idealwidth maxwidth =
121132
llen := !llen + wwidth + 1;
122133

123134
if score == -1 then
124-
plassbreak (indent + 1) (acc + 1) idealwidth maxwidth;
135+
plassbreak (indent + 1) acc idealwidth maxwidth;
125136

126137
let
127-
record = List.nth !l acc in
138+
record = List.nth !gl acc in
128139
if ((!lscore + score) < !bscore) then(
129140
bscore := !lscore + record.score;
130141
btail := acc;
131142
);
132143

133144
acc + 1;
134145
)
135-
) idx !l in
146+
)else acc
147+
) jdx !gl in
136148

137-
let record = List.nth !l idx in
149+
let record = List.nth !gl idx in
150+
Printf.printf "let record = List.nth !gl idx in - %d\n" idx;
138151
record.next <- !btail;
139152
record.score <- !bscore;
140-
141-
if (record.next + 1) = List.length !l then (
153+
if (record.next + 1) = List.length !gl then(
142154
record.score <- 0;
143155
)
156+
)
144157

145-
let pbreak =
158+
let pbreak () =
146159

147-
Printf.printf "Handling effects%!";
148-
match_with (fun () -> plassbreak 0 10 29)
149-
()
160+
match_with (fun () -> plassbreak 0 0 10 29)
161+
()
150162
{ effc = (fun (type c) (eff: c Effect.t) ->
151163
match eff with
152-
| Plass_break s -> Some (fun (k: (c,_) continuation) ->
153-
Printf.printf "Plass Break\"%d\"\n%!" s ;
164+
| Plass_break s -> Some (fun (k1: (c,_) continuation) ->
165+
Printf.printf "Plass Break %d\n" s ;
154166
(* continue k () *)
155167
)
156168
| _ -> None
157169
);
158170
exnc = (function
159171
| e -> raise e
160172
);
161-
(* retc = fun _ -> failwith "Fatal error" *)
173+
retc = fun _ -> failwith "Fatal error"
174+
(* retc = (fun res -> Printf.printf "Computation returned: \n") *)
162175

163-
retc = (fun res -> Printf.printf "Computation returned: \n")
164176
}
165177

166178
let rec loop_while line text lines idx next acc =
167-
if acc > next || (acc + 1) >= List.length !l then
179+
Printf.printf "%s\n" line;
180+
if acc > next || (acc + 1) >= List.length !gl then
168181
line
169182
else
170183
let {first; last; _} = List.nth lines acc in
171184
if (last - first) <= 0 then
172185
line
173186
else
174187
let new_line =
175-
line ^ (if acc = idx then "" else " ") ^
188+
line ^ (if acc == idx then "" else " ") ^
176189
String.sub text first (last - first)
177190
in
178191
loop_while new_line text lines idx next (acc + 1)
179192

180193

181194
let layout text idealwidth maxwidth =
195+
182196
let rec loop idx lines line =
183-
if idx < List.length !l then
184-
let {first; last; next; _} = List.nth lines idx in
185-
let line = loop_while line text lines idx next idx in
197+
Printf.printf "loop %s\n" line;
198+
if idx < List.length !gl then
199+
200+
let entry = List.nth lines idx in
201+
let line = loop_while line text lines idx entry.next idx in
186202
if (String.length line < maxwidth)
187203
then
204+
(
188205
let line = String.make (maxwidth - String.length line) ' ' in
206+
Printf.printf "%s\n" line;
207+
)
208+
else(
189209
let subline = String.sub line 0 idealwidth in
190210
let subline1 = String.sub line idealwidth (String.length line - idealwidth) in
191211
let line = subline ^ "+" ^ subline1 ^ "|" in
192-
Printf.printf "%s\n" line;
193-
else Printf.printf "%s" line;
194-
loop (idx + 1) !l line
212+
Printf.printf "else %s" line;
213+
loop entry.next !gl line
214+
)
195215
in
196-
loop 0 !l ""
216+
loop 0 !gl ""
197217

198218
(* read the entire file *)
199219
let read_file() =
200220
let contents = In_channel.with_open_text "/Users/anu/Documents/go/wiki.txt" In_channel.input_all in
201221
contents
202222

203-
let () =
223+
let pbreak_main =
204224
try
225+
let l = ref [] in
205226
let file_contents = read_file() in
206-
let _ = effective file_contents in
207-
let _ = pbreak in
208-
let _ = layout file_contents 10 30 in
227+
228+
let l1 = effective file_contents l in
229+
gl := !l1;
230+
231+
pbreak();
232+
Printf.printf "Global list %d\n" (List.length !gl) ;
233+
234+
let _ = layout file_contents 10 20 in
235+
209236
()
237+
238+
(* List.iter( fun s -> Printf.printf "%d %d %d %d\n" s.first s.next s.last s.score ) !l *)
239+
210240
with
211-
| exn -> Printf.printf "Unhandled exception: %s\n%!" (Printexc.to_string exn)
241+
| exn -> Printf.printf "Unhandled exception: %s\n" (Printexc.to_string exn)
212242
{% endhighlight OCaml %}

0 commit comments

Comments
 (0)