Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
First baseline using Map.Make on a string-keyed map:
module StringOrd = struct
type t = string
let compare = String.compare
end
module SMap = Map.Make (StringOrd)
let count_words text =
let words = String.split_on_char ' ' text in
List.fold_left (fun m w ->
let n = match SMap.find_opt w m with
| Some n -> n
| None -> 0
in
SMap.add w (n + 1) m
) SMap.empty words
For 'the quick brown fox jumps over the lazy dog' ('the' appears
twice), SMap.cardinal -> 8.
Complements bag.ml (Hashtbl-based) and unique_set.ml (Set.Make)
with a sorted Map view of the same kind of counting problem. 35
baseline programs total.
22 lines
449 B
OCaml
22 lines
449 B
OCaml
module StringOrd = struct
|
|
type t = string
|
|
let compare = String.compare
|
|
end
|
|
|
|
module SMap = Map.Make (StringOrd)
|
|
|
|
let count_words text =
|
|
let words = String.split_on_char ' ' text in
|
|
List.fold_left (fun m w ->
|
|
let n = match SMap.find_opt w m with
|
|
| Some n -> n
|
|
| None -> 0
|
|
in
|
|
SMap.add w (n + 1) m
|
|
) SMap.empty words
|
|
|
|
;;
|
|
|
|
let m = count_words "the quick brown fox jumps over the lazy dog" in
|
|
SMap.cardinal m
|