Skip to content

Commit

Permalink
README clarifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Mar 8, 2019
1 parent 3c3533f commit cad0d00
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ caml!(build_tuple(i) {
});

caml!(average(arr) {
let arr = Array::new(arr);
let arr = Array::from(arr);
let len = arr.len();
let sum = 0f64;

for i in 0..len {
sum += arr.get_double_unchecked(i);
}

return Value::f64(sum / len as f64);
Value::f64(sum / len as f64)
})
```

Expand All @@ -54,19 +54,24 @@ Instead of:

```rust
caml!(function_name, |a, b, c|, <local> {
...
...
} -> local);
```

you can now write:

```rust
caml!(function_name(a, b, c) {
caml_local!(local);
...
return local;
})
caml_local!(local);
...
return local;
});
```

However, when using the type wrappers provided by `ocaml-rs` (`Array`, `List`, `Tuple`, `Str`, `Array1`, ...), `caml_local!` is already called internally.
However, when using the type wrappers provided by `ocaml-rs` (`Array`, `List`, `Tuple`, `Str`, `Array1`, ...), `caml_local!` is already called internally. This means that the following is valid without having to declare a local value for the result:

```rust
caml!(example(a, b, c){
List::from(&[a, b, c])
});
```
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@
//! let i = i.val_i32();
//! Tuple::from(&[i + 1, i + 2, i + 3])
//! });
//! ```
//! ```norun
//!
//! caml!(average(arr) {
//! let arr = Array::new(arr);
//! let arr = Array::from(arr);
//! let len = arr.len();
//! let sum = 0f64;
//!
//! for i in 0..len {
//! sum += arr.get_double_unchecked(i);
//! }
//!
//! return Value::f64(sum / len as f64);
//! })
//! Value::f64(sum / len as f64)
//! });
//! ```
//!
//! In OCaml the stubs for these functions looks like this:
Expand All @@ -59,7 +58,8 @@
//! external average: float array -> float = "average"
//! ```
//!
//! For more examples see [./example](https://github.com/zshipko/ocaml-rs/blob/master/example) or [ocaml-vec](https://github.com/zshipko/ocaml-vec).
//! For more examples see [./example](https://github.com/zshipko/ocaml-rs/blob/master/example)
//! or [ocaml-vec](https://github.com/zshipko/ocaml-vec).
#[macro_use]
mod macros;
Expand Down

0 comments on commit cad0d00

Please sign in to comment.