Code Monkey home page Code Monkey logo

ocamlbyexample's Introduction

Ocaml By Example

Screen Shot 2021-06-02 at 9 48 58 PM

Learn the OCaml language, following a series of examples!

You can visit the rendered course here: https://o1-labs.github.io/ocamlbyexample/.

This page is inspired by https://gobyexample.com.

Adding your own examples (chapters)

Create a folder in book/chapters/ (using another example), then add your chapter in book/chapters.json.

Some guidelines:

  1. short examples are best
  2. one chapter teaches one concept well
  3. one block of explanation teaches one subconcept well
  4. a concept is always introduced and explained the first time it is being used in an example

How to render the webpage locally

You need the OCaml language setup. Once you've set up OCaml you can install the dependencies needed by the project with:

make deps

Then simply run:

make

or to automatically update the website as you change files:

make watch

Can I use this for another programming language?

Yes, while the code is written in OCaml you can use it to build similar webpages for any programming language (although syntax highlighting will only work for languages supported by highlight.js).

So feel free to fork this page and use to teach your own stuff!

ocamlbyexample's People

Contributors

jspada avatar mimoo avatar yukiioz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocamlbyexample's Issues

chapter: ppx

you can dump any AST by doing ocamlfind ppx_tools/dumpast your_file.ml

ocamlbyexample 2.0

I think this library did a faux pas. It shouldn't have separated the comments from the text. It's just too much of a pain to create a chapter due to this. I think it should detect, or have the user provide what type of comment is being used in a file, and extract comment from code.

The other faux pas is how the code is broken in sections, where it meets the comments. I think a better model is to calculate how many space the comment creates, and add linebreaks in the code in correct places to pad.

Highlight syntax on build, not in browser

Currently the syntax highlighting is done via a client-side highlighter downloaded from a CDN. There are problems with this approach:

  • <noscript> is not supported; text is black on black so it's not remotely readable with JavaScript disabled out of limitations or security/privacy--and if the goal is to deliver content accessibly to the most folks, then lowering requirements should be favored
  • Requires trust and giving up data to a third-party CDN; should users need trust this third party when this could be vendored to first-party? (this also applies to Bootstrap, but at a minimum, at least this script includes a subresource integrity check to verify that the file is correct from a third party)
  • Provided the CDN is trustworthy and delivering the same file, all users are downloading and executing the exact same highlighter code on the same code blocks which create wasteful processing on the device and also requires extra bandwidth to download and execute this

Moving syntax highlighting to the build phase would:

  • transformation from blank <pre> block to highlighted is only executed once (memoized at build) emitting code with the appropriate CSS classes on the HTML
  • Users do not need to trust or execute any third-party code
  • Users on browsers with JavaScript missing or disabled, can still have a rich experience
  • No additional data or CPU resources are used on client devices which is better for energy consumption and site responsiveness (no need to parse and then repaint)

chapter utop

analyze a single file without its .mli:

$ utop
# #use_output "dune ocaml top-module path/to/module.ml";;

chapter: private types

  • can't construct the type manually (it's abstract)
  • but you can deconstruct it (it's not fully abstract)
module M : sig type t = private { a : int } val create : unit -> t end = struct type t = { a : int } let create () = { a = 5 } end
(* let a = { M.a = 5 }  <-- not gonna work *)
let a = M.create ();;
(* val a : M.t = {M.a = 5} *)
let M.{ a = b } = a
(* val b : int = 5 *)

string vs bytes

emphasize the fact that these terms are misleading, and that one should think about them as:

  • string: immutable bytearray
  • bytes: mutable bytearray

better to avoid recurse modules

  • declare recursive functions and types first
  • then reuse in module
  • recurse modules have some overhead with the way ocaml construct them

early returns

you can do early returns in ocaml by throwing exceptions and catching them, which is what Core.with_return does

Locally abstract types

(* an example with type variables that produces a bug *)
let map (x: 'a * 'a) (f:'a -> 'b) : ('b * 'b) =
	let x1, x2 = x in	
	(f x1, x2)

gives

val map : 'b * 'b -> ('b -> 'b) -> 'b * 'b = <fun>

whereas it should be (f x1, f x2)

keeping type abstracts prevents unification, which would have had the compiler yell at us:

let map (type a b) (x: a * a) (f:a -> b) : (b * b) =
	let x1, x2 = x in	
	(f x1, x2)

gives us: Error: This expression has type a but an expression was expected of type b

Also, it's useful for GADTs (see GADT section)

Errors on the map page

Hey, I spotted a few mistakes on the page about maps (https://o1-labs.github.io/ocamlbyexample/basics-map.html)

  • Maps in Core are not hashmaps, they are implemented with search trees. There are various important differences between the two, for instance in a hash table the keys must be hashable but in a search tree they need to be comparable instead. Core does have hashmaps as well in the Hashtbl module.
  • "dictionnaries" has an extra n

Also, in the example code let balances = Map.empty (module String) I think it would be more idiomatic to do let balances = String.Map.empty. Many modules like String in Core have a specialised map submodule like this.

first order modules

module A = struct
  type t = int
end

module type A = module type of A

let a = (module A : A)

module B = (val a : A)

module C = (val a)

chapter: lazy

utop # let a = lazy "prout";;
val a : string lazy_t = lazy "prout"

utop # Lazy.force a;;
- : string = "prout"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.