Skip to content

Adds an example (OCaml Examples Project)#163

Open
johnwhitington wants to merge 2 commits intomirage:mainfrom
johnwhitington:main
Open

Adds an example (OCaml Examples Project)#163
johnwhitington wants to merge 2 commits intomirage:mainfrom
johnwhitington:main

Conversation

@johnwhitington
Copy link

Hello!

This pull request adds some examples to Digestif. This is part of a pilot programme funded by the OCaml Software Foundation.

Many OCaml libraries have no examples, or perfunctory examples only. This makes it difficult to get started with a library, particularly if it has an elaborate interface. A working example, no matter how small, can help a newcomer get started quickly. One day, it would be nice to have an example for every Opam package.

For now, examples begin in the OCaml Nursery, here: https://github.com/johnwhitington/ocaml-nursery (you can read in the README there about the principles behind these examples.) Then, if package authors agree, they are promoted to upstream source. The hope is that this will mean they are more likely to be kept up to date with the library.

The examples are included in a separate directory, within a separate Dune workspace. And so they are intended to be used after installation of the library.

As well as considering accepting this pull request, please give any comments you have on this programme.

let n = input fh buf 0 1024 in
context := Digestif.SHA256.feed_bytes !context ~off:0 ~len:n buf;
if n = 0 then fin := true
done;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be nice to show a comparative example which does not use a reference but proposes a more functional way to digest something:

let rec go ctx =
  let len = input fh buf 0 1024 in
  if len = 0 then Digestif.SHA256.get ctx
  else go (Digestif.SHA256.feed_bytes ctx ~off:0 ~len buf) in
go Digestif.SHA256.empty

fe6e6639a817c23857b507e2d833ec776f23f327
```

A further example is given in the examples directory.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example above is like what you propose. It will be nice to show up something more complex like: digest something in parallel (OCaml 4 & OCaml 5) with digest_bigstring. Digestif has a nice property to release the GC when it's about digest a bigstring.

@dinosaure
Copy link
Member

Another question highlighted by the README.md is about the comparison of hashes. It's intended to propose unsafe_compare instead of compare (by this way, a user can not easily make a {Set.Map}.Make from a digestif module. It will be nice to inform the user about that and the reason behind unsafe_compare and an introduction to eqaf.

Thanks for your contribution, I don't have too much time these weeks to really help you but I can try to help you to complete the documentation and examples next month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants