OCaml in Basthon
Open the console or the notebook in OCaml (URL parameter kernel=ocaml). Examples are in the gallery, and the modules and libraries added by Basthon are described on the OCaml: what Basthon adds page.
OCaml's Basthon module (not to be confused with the basthon Python module) shows images, SVG or HTML, downloads files and draws on a canvas. Libraries are loaded with the #require directive, as with opam: they are only downloaded at the first #require and, already compiled to JavaScript, load almost instantly (list, post). Basthon.help () shows the help of the module and the list of the libraries.
What you can do
Each example has its notebook in the gallery ("try it" link).
Drawing, animating, playing
The Graphics_js module (the equivalent of Graphics for the browser) draws on a canvas created by Basthon.create_canvas and then shown by Basthon.display_canvas (try it):
let canvas = Basthon.create_canvas () in
Graphics_js.open_canvas canvas;
Graphics_js.resize_window 200 200;
Graphics_js.set_color Graphics_js.red;
Graphics_js.fill_circle 100 100 20;
Basthon.display_canvas canvasThe canvas shown stays alive after the run: it reacts to the mouse and the keyboard (Graphics_js.loop, mouse_pos, read_key) and can be animated with Lwt, which allows games such as the Pong of the gallery (post).
Visualizing memory and recursive calls
The visu library draws the trace and the call tree of a recursive function (redundant computations in orange), the memory representation of a value (blocks, pointers, sharing, cyclic values), trees, graphs and arrays (try it, post):
#require "visu";;
module Visu = Basthon.Visu;;
let fib fib n = if n < 2 then n else fib (n - 1) + fib (n - 2);;
Visu.arbre_appels ~nom:"fib" string_of_int string_of_int fib 5;;
Visu.memoire ~nom:"m" (Array.make 3 (Array.make 2 0));;The function observed is written in open recursion: its first parameter is the function to use for its recursive calls. The #trace directive of the toplevel does not work in Basthon: Visu.trace replaces it.
Drawing graphs
graphviz draws a graph described in DOT, and ocamlgraph provides graph structures and the classic algorithms (traversals, Dijkstra, connected components, topological sort, flows…) (try it, post):
#require "graphviz";;
module Gv = Basthon.Graphviz;;
Gv.display "digraph { a -> b; b -> c; c -> a }";;
#require "ocamlgraph";;
module G = Graph.Pack.Digraph;;
let g = G.Classic.full 4;;
G.dot_output g "g.dot";;
Gv.display_file "g.dot";;Big integers and rationals
OCaml's integers are 32 bits here (max_int is 2,147,483,647): zarith brings integers of arbitrary size (module Z) and exact rationals (module Q), for arithmetic up to a miniature RSA (try it, post):
#require "zarith";;
let rec fact n = if n = 0 then Z.one else Z.mul (Z.of_int n) (fact (n - 1));;
fact 100;;
Z.probab_prime Z.(~$2 ** 127 - ~$1) 25;;
Q.(of_ints 1 10 + of_ints 2 10);;Priority queues, deques…
containers adds what the standard library lacks: heaps (CCHeap), deques (CCDeque), persistent queues (CCFQueue), dynamic arrays (CCVector)…, for instance for heap sort or Dijkstra's algorithm (try it, post):
#require "containers-data";;
module Heap = CCHeap.Make (struct type t = int let leq = ( <= ) end);;
Heap.to_list_sorted (Heap.of_list [ 5; 3; 8; 1 ]);;
let d = CCDeque.of_list [ 2; 3 ];;
CCDeque.push_front d 1;;Reading CSV and JSON data
csv and yojson read and write real data, for instance a file opened in Basthon or attached to the notebook (try it, post):
#require "csv";;
#require "yojson";;
let rows = Csv.Rows.load ~has_header:true "students.csv";;
List.map (fun r -> Csv.Row.find r "name") rows;;
let json = Yojson.Safe.from_string {|{"name": "Ada", "marks": [15, 18]}|};;
Yojson.Safe.Util.(json |> member "marks" |> to_list);;Using your own modules
.ml files (and their .mli interfaces) opened in Basthon are used as modules, as with separate compilation: each module is loaded when first used, its errors are shown in the cell that uses it, and a module opened again after a change is reloaded (see Files and modules, try it, post).
Completion
The Tab key completes the values, constructors and modules known to the toplevel, including those defined by the code run or opened with open, and the members of a module (List.ma offers map, map2, mapi), including those of a library loaded with #require (Z. after #require "zarith"). The user's modules and the names of the libraries (#require "za gives zarith) are completed too. The fields of a record (r.) are not completed yet.
Files and modules
Extra files are available in the filesystem (open_in, Sys.readdir "."…). The open button (or the module URL parameter) installs *.ml and *.mli files as modules, usable directly (My_module.f).
Downloading a file
A file is downloaded with Basthon.download "path/to/file", a canvas with Basthon.download_canvas canvas.