647 search results for ""

Showing 601 - 647
  1. Higher Order Functions

    Mapping

    This is called mapping . For example, if we have a list of users, maybe we want to get a list of usernames. Or if we have an optional password, we may want to encrypt it only if it is set. In cont

    Introduction
  2. 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

    Tutorials
  3. Profiling

    Arrays

    So arrays of floats are unboxed, as expected. (Each float in the array is 8 bytes long.) The assembler syntax is rather complex, but the bracketed expression -4(%ecx, %eax, 4) means "at the ad

    Guides
  4. A Tour of OCaml

    Conclusion

    In this tutorial, OCaml was used interactively. The next tutorial, Your First OCaml Program , shows you how to write OCaml files, how to compile them, and how to kickstart a project. <!-- 1. Values

    First Steps
  5. Sets

    Creating a Set

    There's another relevant function StringSet.of_seq: string Seq.t -> StringSet.t that creates a set from a sequence . Converting a list into a set using StringSet.of_list : A set with a sing

    Data Structures
  6. Preprocessors and PPXs

    Writing a PPX

    If you want to write your own PPX, the place to start is ppxlib's documentation .

    Advanced Topics
  7. Lists

    Sorting Lists

    The function Fun.flip reverses a binary function parameter order. The function List.sort , given a comparison function of type 'a -> 'a -> int (zero if equal, negative if first smaller, p

    Introduction
  8. Error Handling

    Runtime Crashes

    Although OCaml is a very safe language, it is possible to trigger unrecoverable errors at runtime.

    Guides
  9. Maps

    Checking if a Key is Contained in a Map

    To check if a key is a member of a map, use the mem function:

    Data Structures
  10. Profiling

    Summary

    Write your program as simply as possible. If it takes too long to run, profile it to find out where it's spending its time and concentrate optimisations on just those areas. Check for unintention

    Guides
  11. File Manipulation

    Seeking

    Whenever you write or read something to or from a channel, the current position changes to the next character after what you just wrote or read. Occasionally, you may want to skip to a particular

    Tutorials
  12. Sets

    Subtracting a Set from Another

    With the function StringSet.diff , we can remove the elements of the second set from the first set.

    Data Structures
  13. Modules

    Introduction

    Note : The files that illustrate this tutorial are available as a Git repo . Modules are collections of definitions grouped together. This is the basic means to organise OCaml software. Separate co

    Module System
  14. Profiling

    Floats

    string_of_float isn't polymorphic, but suppose we have a polymorphic function foo : 'a -> unit taking one polymorphic argument. If we call foo with %eax containing 7, then this is equivalen

    Guides
  15. Comparison of Standard Containers

    Stack

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml stacks are mutable last-in-first-out (LIFO) data structures. They are just like lists except they are mutable, i.e., adding an

    Resources
  16. Libraries With Dune

    Conclusion

    The OCaml module system allows organising a project in many ways. Dune provides several means to arrange modules into libraries.

    Module System
  17. Objects

    Objects Without Class

    Here we examine how to use objects pretty much like records, without necessarily using classes.

    Advanced Topics
  18. Basic Data Types and Pattern Matching

    Variants

    Variants are also called tagged unions . They relate to the concept of disjoint union .

    Introduction
  19. Lists

    Folds

    Here are some more redefinitions of familiar functions in terms of fold_left or fold_right . Can you work out how they operate? Unfortunately, the order of evaluation here is such that larg

    Introduction
  20. Functors

    Conclusion

    Functor application essentially works the same way as function application: passing parameters and getting results. The difference is that we are passing modules instead of values. Beyond comfort, it

    Module System
  21. Maps

    Creating a Map

    Example 2 : By Adding an Element Example 1 : By Annotation When you create your new map with an annotation By adding an element to the map: The type of the values can be specified in two ways: T

    Data Structures
  22. Configuring Your Editor

    Emacs

    A major mode, which, among other things, supports syntax highlighting and the structuring of indentation levels A minor mode, which will interact with a language server (such as ocaml-lsp-server or

    Tooling
  23. Error Handling

    Documentation

    <!-- $MDX skip --> Functions that can raise exceptions should be documented like this:

    Guides
  24. Maps

    Merging Maps

    pick_fst picks the result's value from the first map pick_snd picks the result's value from the second map drop drops both entries in the result map Here are examples of duplicate key resolut

    Data Structures
  25. Loops and Recursions

    Approach 2

    The imperative approach uses a while loop that is broken out of using an exception.

    Introduction
  26. Profiling

    Speed

    But you will need to know some assembler to get the most out of this section. Don't be afraid! I'll help you out by translating the assembler into a C-like pseudocode (after all C is just a porta

    Guides
  27. Options

    Conclusion

    By the way, any type where map and join functions can be implemented, with similar behaviour, can be called a monad , and option is often used to introduce monads. But don't freak out! You do

    Data Structures
  28. Sets

    Intersection of Two Sets

    With the function StringSet.inter , we can compute the intersection of two sets.

    Data Structures
  29. Arrays

    Map an Array

    The Array.map function creates a new array by applying a given function to each element of an array. For example, we can get an array containing the square of each number in the even_numbers ar

    Data Structures
  30. Arrays

    Conclusion

    In this tutorial, we covered the basics of arrays in OCaml, including how to create and manipulate them, as well as some of the most useful functions and use cases. Please refer to the standard libr

    Data Structures
  31. OCaml on Windows

    WSL2

    After you have installed WSL2 and chosen one Linux distribution (we suggest Ubuntu LTS ), you can follow the Installing OCaml: Installation for Linux and macOS steps. If you only need to run O

    Resources
  32. Comparison of Standard Containers

    Queue

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml queues are mutable first-in-first-out (FIFO) data structures.

    Resources
  33. Sets

    Filtering a Set

    The function StringSet.filter of type (string -> bool) -> StringSet.t -> StringSet.t creates a new set by keeping the elements that satisfy a predicate from an existing set.

    Data Structures
  34. Profiling

    .data

    .long writes a word (4 bytes) to the current segment. .byte writes a single byte. .ascii writes a string of bytes (NOT nul-terminated). .space writes the given number of zero bytes. Normall

    Guides
  35. Maps

    Filtering a Map

    To filter a map, use the filter function. It takes a predicate to filter entries and a map. It returns a new map containing the entries satisfying the predicate.

    Data Structures
  36. Preprocessors and PPXs

    PPXs

    PPxs are a different kind of preprocessor—one that does not run on the textual source code, but rather on the parsing result: the Abstract Syntax Tree (AST), which in the OCaml compiler is call

    Advanced Topics
  37. Arrays

    Length of an Array

    The Array.length function returns the size of an array:

    Data Structures
  38. File Manipulation

    Example

    <!-- $MDX dir=examples --> We can compile and run this example: <!-- $MDX file=examples/file_manip.ml -->

    Tutorials
  39. Maps

    Map a Map

    The keys are the same in both maps. For each key, a value in lucky_numbers is converted into a value in lucky_strings using string_of_int . Using StringMap.map , we create a map associatin

    Data Structures
  40. Sets

    Conclusion

    We gave an overview of OCaml's Set module by creating a StringSet module using the Set.Make functor. Further, we looked at how to create sets based on a custom comparison function. For more in

    Data Structures
  41. Sets

    Union of Two Sets

    With the function StringSet.union , we can compute the union of two sets.

    Data Structures
  42. Sets

    Working With Sets

    Let's look at a few functions for working with sets using these two sets.

    Data Structures
  43. Maps

    Conclusion

    For more information, refer to Map in the Standard Library documentation. This was an overview of OCaml's Map module. Maps are reasonably efficient and can be an alternative to the imperativ

    Data Structures
  44. Maps

    Working With Maps

    Throughout the rest of this tutorial, we will use the following map:

    Data Structures
  45. Configuring Your Editor

    Vim

    After installing Merlin above, instructions will be printed on how to link Merlin with your editor. If you do not have them visible, just run this command: For Vim, we won't use the LSP server b

    Tooling
  46. Profiling

    .text

    Text is the Unix way of saying "program code". The text segment simply means the part of the executable where program code is stored. The .text directive switches the assembler so it starts w

    Guides
  47. Operators

    Goals

    Using operators as functions and reciprocally, using functions as operators Assign the right associativity and precedence to a custom operator Use and define custom let binders The learning goals o

    Advanced Topics