-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
A trivial project using batteries:
(* euler001.ml *)
open Batteries_uni
let main () =
(1--999) (* the enum that counts from 1 to 999 *)
|> Enum.filter (fun i -> i mod 3 = 0 || i mod 5 = 0)
|> Enum.reduce (+) (* add all remaining values together *)
|> Int.print stdout
let () = main ()
To compile this program, run ocamlfind ocamlc -package batteries -linkpkg euler001.ml -o euler001
.
To use the syntax extensions included in batteries, run ocamlfind ocamlc -package batteries -package batteries.syntax -syntax camlp4o -linkpkg foo.ml -o foo
.
This project opens Batteries_uni
because it doesn't use threads. Version 2 of batteries replaces this with open Batteries
for all projects, and adds open BatteriesThreads
for projects that use threads.
If you have a larger project, ocamlbuild and OMake both work well with batteries.
##Ocamlbuild
###OCaml 3.12 or newer Ocamlbuild comes with support for findlib since 3.12.0. There may be bugs in its handling of threads in 3.12.0, so we recommend 3.12.1 if you are going to follow these instructions. The 3.10-3.11 instructions will work under 3.12.0 as well.
- Tell
ocamlbuild
to use batteries through a_tags
file:<*>: package(batteries), thread
- Use
open Batteries_uni
in your source files to allow referring toBatFoo
as justFoo
. This also integrates BatList and all other stdlib-extensions into the standard module names. - Compile with
ocamlbuild -use-ocamlfind foo.byte
wherefoo.ml
is the name of your main module file. - To use Batteries' Syntax Extensions, add
package(batteries.syntax), syntax(camlp4o)
to your_tags
file.
###OCaml 3.10 to 3.11
- Use a findlib-enabled "myocamlbuild.ml":https://github.com/ocaml-batteries-team/batteries-included/blob/master/examples/benchmark/myocamlbuild.ml Copy this file into your project directory
- Tell
ocamlbuild
to use batteries through a_tags
file:<*>: pkg_batteries,pkg_threads
- Use
open Batteries
in your source files to allow referring toBatFoo
as justFoo
. This also integrates BatList and all other stdlib-extensions into the standard module names. - Compile with
ocamlbuild foo.byte
wherefoo
is the name of your main module. - To use Batteries' Syntax Extensions, add
pkg_batteries.syntax, syntax_camlp4o
to your_tags
file.
##OMake
If you're already using OMake, add the following to your OMakefile:
OCAMLPACKS[] += batteries
OCAMLFLAGS += -thread
To load batteries included in your toplevel, copy the ocamlinit
file from the batteries source tree to ~/.ocamlinit
and it will auto-load ocamlfind and the syntax extensions. You should see a graphical banner indicating that batteries is loaded.
To use Batteries with ocamlscript, you only need to include the appropriate findlib package(s).
#!/usr/bin/env ocamlscript
(* Compile and link with Batteries *)
Ocaml.packs := ["batteries"];;
--
open Batteries_uni
let () = print_endline "Hello world!"
Using syntax extensions is relatively straightforward:
#!/usr/bin/env ocamlscript
Ocaml.packs := ["batteries"; "batteries.pa_string.syntax"];;
--
open Batteries_uni
let () = Print.printf p"Hello! Here's a list: %{int list}\n" [1; 2; 3]
Finally, bring in threaded Batteries and turn on debugging symbols for backtraces. This example doesn't use threads, but shows how to write something with ocamlscript using threaded Batteries:
#!/usr/bin/env ocamlscript
(* Extra compilation arguments required for threads and debugging symbols *)
Common.extra_args := ["-thread"; "-g"];;
Ocaml.packs := ["threads"; "batteries"];;
--
open Batteries
(* Print a few integers *)
let () = iter (fun x -> Printf.printf "%d\n" x) (1 -- 10)