Skip to content

Commit

Permalink
doc: Add more information to the README
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Jan 8, 2017
1 parent a09795b commit ffe4c41
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
19 changes: 4 additions & 15 deletions README.md
Expand Up @@ -5,7 +5,9 @@
[![Build Status](https://travis-ci.org/Kerollmops/ripin-rs.svg)](https://travis-ci.org/Kerollmops/ripin-rs)
[![Coverage Status](https://coveralls.io/repos/github/Kerollmops/ripin-rs/badge.svg?branch=master)](https://coveralls.io/github/Kerollmops/ripin-rs?branch=master)

A library to handle Reverse Polish notated expressions
A library to handle [`Reverse Polish notated`](https://en.wikipedia.org/wiki/Reverse_Polish_notation) expressions.

Ripin can also evaluate variable expressions and is not limited to string tokens, it can handle any iterator type, the only limit is your imagination. There is [`examples`](https://github.com/Kerollmops/ripin-rs/tree/master/examples) to understand how to implement your own expression from custom types.

## Installation

Expand Down Expand Up @@ -37,19 +39,6 @@ let expr = FloatExpr::<f32>::from_iter(tokens).unwrap();
println!("Expression {:?} gives {:?}", expr_str, expr.evaluate())
```

You don't care about floating-point expressions ? Why not integer expression evaluation ?

```rust
use ripin::expression::IntExpr;

let expr_str = "3 4 + 2 *"; // (3 + 4) * 2
let tokens = expr_str.split_whitespace();

let expr = IntExpr::<i32>::from_iter(tokens).unwrap();

println!("Expression {:?} gives {:?}", expr_str, expr.evaluate())
```

It is also possible to use variables in your expressions to make them more "variable".

```rust
Expand All @@ -58,7 +47,7 @@ use ripin::variable::VarIdx;

let variables = vec![3.0, 500.0]; // Try changing variables here

let expr_str = "3 4 + 2 * $0 -"; // (3 + 4) * 2 - $0
let expr_str = "3 $1 + 2 * $0 -"; // (3 + $1) * 2 - $0
let tokens = expr_str.split_whitespace();

let expr = VariableFloatExpr::<f32, VarIdx>::from_iter(tokens).unwrap();
Expand Down
8 changes: 6 additions & 2 deletions src/expression.rs
Expand Up @@ -33,13 +33,17 @@ pub struct Expression<T, V, E: Evaluate<T>> {
}

impl<T: Copy, V: Copy, E: Evaluate<T> + Copy> Expression<T, V, E> {
/// Evaluate the `RPN` expression. Returns the result
/// Evaluate `RPN` expressions. Returns the result
/// or the [`evaluate Error`](../evaluate/trait.Evaluate.html#associatedtype.Err).
pub fn evaluate(&self) -> Result<T, E::Err> where (): From<V> {
self.evaluate_with_variables(DummyVariables::default())
}

// TODO doc
/// Evaluate `RPN` expressions containing variables. Returns the result
/// or the [`evaluate Error`](../evaluate/trait.Evaluate.html#associatedtype.Err).
///
/// # Panics
/// Panics if a variables doesn't exists in the variable container.
pub fn evaluate_with_variables<I, C>(&self, variables: C) -> Result<T, E::Err>
where V: Into<I>,
C: Index<I, Output=T>
Expand Down

0 comments on commit ffe4c41

Please sign in to comment.