364 search results for "function"
-
Profiling
Further Reading
p of pointer indirection. Objects in OCaml are also dynamically dispatched. Since this is the point with polymorphism in an OO setting. * Dynamic method dispatch often hinders a compiler to inline function and this hits the performance. * In Java is a dynamic type check (aka cast) much more expensive than a dynamic method dispatch. --> You can find out more about how OCaml represents different typ
Guides -
Command-line Arguments
Sys.argv
ndard library, therefore its full name is Sys.argv . The number of arguments including the name of the program itself is simply the length of the array. It is obtained using the Array.length function.
Tutorials -
File Manipulation
Writing
Standard out_channel s: stdout , stderr Commonly used functions: open_out , open_out_bin , flush , close_out , close_out_noerr Open the file to obtain an out_channel Write to the channel If you want to force writing to the physical device, you must flush
Tutorials -
File Manipulation
Reading
Standard in_channel : stdin Commonly used functions: open_in , open_in_bin , close_in , close_in_noerr Open the file to obtain an in_channel Read characters from the channel. Reading consumes the channel, so if you read a character, the chan
Tutorials -
Comparison of Standard Containers
Lists: Immutable Singly-Linked Lists
Not very efficient for: random access, indexed elements Well-suited for: I/O, pattern-matching Adding an element: O(1) , cons operator :: Length: O(n) , function List.length Accessing cell i : O(i) Finding an element: O(n) Adding an element always creates a new list l from an element x List tl . tl remains unchanged, but it is not copied either.
Resources -
Comparison of Standard Containers
Arrays: Mutable Vectors
of elements of known size, accessing elements by numeric index, and modifying in-place elements. Basic arrays have a fixed length. Adding an element (by creating a new array): O(n) Length: O(1) , function Array.length Accessing cell i : O(1) Finding an element: O(n) Arrays are mutable data structures with a fixed length and random access.
Resources -
How to Work with the Garbage Collector
The Gc Module
.ml --> <!-- TODO: Probably write a GC example without dependencies --> Here is a program that runs and then prints out GC statistics just before quitting: The Gc module contains some useful functions for querying and calling the garbage collector from OCaml programs.
Guides -
How to Work with the Garbage Collector
Exercises
xtend the program so it acquires a read lock on getting the record, but upgrades this to a write lock just before the user updates any field. Support a variable number of records , and add a function to create a new record (in the file). [Tip: OCaml has support for weak hashtables.] Add support for variable-length records . Make the underlying file representation a DBM-style hash . Provide
Guides -
Transitioning to Multicore with ThreadSanitizer
Address the Reported Races and Rerun the Tests, Take 2 (Steps 3 and 2)
ml 5.x parallelism, hurrah! We can now rerun our tests under TSan to confirm the fix: Oh, wait! When raising an exception in transfer , we forgot to unlock the Mutex again. Let's adapt the function to do so:
Guides -
Transitioning to Multicore with ThreadSanitizer
Final Remarks and a Word of Warning
l fix as follows: The programming pattern of 'always-having-to-do-something-at-the-end' that we encountered with the missing Mutex.unlock is a recurring one for which OCaml offers a dedicate function:
Guides -
Error Handling
Documentation
<!-- $MDX skip --> Functions that can raise exceptions should be documented like this:
Guides -
Error Handling
Language Bugs
sh File an issue in the OCaml Bug Tracker in GitHub it may be a language bug. It happens. Here is what to do when this is suspected: A limitation of the native code compiler An inherently unsafe function such as are found in modules Marshal and Obj When a crash isn't coming from:
Guides -
Error Handling
External Resources
odule result in Ocaml Library “Error Handling” in “Real World OCaml”, part 7, Yaron Minsky and Anil Madhavapeddy, 2ⁿᵈ edition, Cambridge University Press, October 2022 “Add "finally" function to Pervasives”, Marcello Seri, GitHub PR, ocaml/ocaml/pull/1855 “A guide to recover from interrupts”, Guillaume Munch-Maccagnoni, parf the memprof-limits documentation
Guides -
Using the OCaml Compiler Toolchain
The ocamlc and ocamlopt Compilers
tance, if you create a file graphics.ml and use the graphics library, the Graphics module exposed from the graphics library will be hidden by your newly defined module, hence all of the functions defined in it will be made inaccessible. Moving on, we'll see how to use ocamlopt . Let's assume that our program program has two source files, module1.ml and module2.ml . We will compile
Guides