OCaml circular linked list http://robanderson.dev/ocaml-cll/cll
  • OCaml 95.4%
  • Dune 3.7%
  • Shell 0.9%
Find a file
Rob Anderson ece05bb13b
Update index.mld
Updated docs to correct example usage
2024-05-14 13:30:50 +01:00
.devcontainer Fixed container json and updated README 2023-10-12 15:46:18 +00:00
.github/workflows Added doc/ to paths to watch for build 2023-10-30 23:45:56 +00:00
doc Update index.mld 2024-05-14 13:30:50 +01:00
examples Added find function (#1) 2023-10-28 22:23:04 +01:00
lib Added iter function (#3) 2023-11-11 17:04:46 +00:00
test Added iter function (#3) 2023-11-11 17:04:46 +00:00
.gitignore container changes, and code coverage sort of working 2023-10-12 15:46:18 +00:00
.ocamlformat container changes, and code coverage sort of working 2023-10-12 15:46:18 +00:00
cll.opam Added iter function (#3) 2023-11-11 17:04:46 +00:00
coverage.sh Coverage stuff 2023-10-12 21:51:51 +01:00
dune-project Added iter function (#3) 2023-11-11 17:04:46 +00:00
LICENSE Initial commit 2023-10-09 23:26:59 +01:00
README.md Update README.md 2024-05-14 13:29:18 +01:00

ocaml-cll

OCaml circular linked list library

build and test

Cll documentation

Opam package

Installation

$ opam install cll

Usage

To be run in the ocaml toplevel

$ dune utop
let cll = Cll.init [ 1; 2; 3; 4; 5; 6 ];;

Cll.head cll;;
(* int option = Some 1 *)

Cll.length cll;;
(* int = 6 *)

Cll.next cll;;
Cll.head cll;;
(* int option = Some 2 *)

Cll.prev cll;;
Cll.prev cll;;
Cll.head cll;;
(* int option = Some 6 *)

Cll.add cll 7;;
Cll.head cll;;
(* int option = Some 7 *)

Cll.pop cll;;
(* int option = Some 7 *)
Cll.head cll;;
(* int option = Some 1 *)

Cll.find cll 4;;
(* bool = true *)
Cll.head cll;;
(* int option = Some 4 *)

Cll.iter cll (fun x -> Printf.printf "%i\n" x)
(*
    4
    5
    6
    1
    2
    3
*)

Cll.to_list cll;;
(* int list = [4; 5; 6; 1; 2; 3] *)

Development requirements

  • OCaml
  • Opam
  • Dune
$ ocaml --version
The OCaml toplevel, version 5.1.0

$ opam --version
2.1.5

$ dune --version
3.11.0

Development setup

Install dependencies

opam install . --deps-only --with-test -y

Build code

dune build

Testing

Run unit tests

$ dune test
.................
Ran: 17 tests in: 0.11 seconds.
OK

Check code coverage

dune test --instrument-with bisect_ppx --force

# show coverage summary
bisect-ppx-report summary

# create html coverage report
bisect-ppx-report html
open _coverage/index.html

Running examples

There aren't many uses I'm aware of for a circular linked list, but I have found two programming puzzles where it's been handy.

Prerequisites

From the project's root directory:

# install utop
$ opam install utop

Codewars: Josephus problem

# run the josphus_problem.ml toplevel script
$ dune utop . -- examples/josephus_problem.ml
4

Advent of Code 2020 day 23: Crab Cups

By default only part one of this puzzle will run, as part two takes about 40 seconds on my laptop when running with utop.

To enable part two, uncomment the last three lines in examples/crab_cups.ml.

# run part one of the crab cups toplevel script
$ dune utop . -- examples/crab_cups.ml
part one: 67384529