pike 0.1.2

A macro collection to pipe |> your functions calls, like in F# or Elixir.
Documentation
# pike


Pike is a macro collection to pipe your functions calls, like in F# or Elixir.

## Examples


The pipe operator |> allows you to establish "pipelines" of functions in a flexible manner.

```rust
fn times(a: u32, b: u32) -> u32{
    a * b
}

fn times2(n: u32) -> u32 {
    times(n, 2)
}

// Passes the preceding expression as the only argument of proceding function.
let num = pike::pipe!(
  2
  |> times2
  |> times2
);
assert_eq!(num, 2 * 2 * 2);

// Passes the preceding expression as the first argument of proceding function.
// by wrapping the function in parentheses we can pass the remanining arguments by partially
// calling the `times` as `times(?, 2)` and passing 2 as its first argument via the pipeline.
let num = pike::pipe!(
  1
  |> (times(2))
  |> (times(3))
);
assert_eq!(num, 1 * 2 * 3);

// call a method using pipelines
let len = pike::pipe!(
  "abcd"
  |> str::len
);
assert_eq!(len, "abcd".len());

// Closures can also be pipelined similar to partial functions.
let c = pike::pipe!(
  ['a', 'b', 'c', 'd']
  |> (|it: [char; 4]| it[2])
);
assert_eq!(c, 'c');

// Piping through `&` symbol would get a reference to the preceding expression.
let it = "it";
let is_it = |r: &&str| it == *r;

let is_it = pike::pipe!(
  it
  |> &
  |> is_it
);
assert_eq!(is_it, true);

// takes a string length, doubles it and converts it back into a string
let len = pike::pipe!(
    "abcd"
    |> str::len
    |> (as u32)
    |> (times(2))
    |> &
    |> u32::to_string
);
```


## Macros


- `pipe!` is the "standard" pipe macro
- `pipe_res!` works like `pipe!` but takes only functions that return a `Result` (of the
  same type) and returns early if that result is an Err. Useful for combining multiple IO
  transformations like opening a file, reading the contents and making an HTTP request.
- `pipe_opt!` works like `pipe!` but takes only functions that return an `Option` (of the same type).
  The pipeline will continue to operate on the initial value as long as `None` is returned from all functions.
  If a function in the pipeline returns `Some`, the macro will exit early and return that value.
  This can be useful if you want to try out several functions to see which can make use of that value in a specified order.

## Syntax Features


Any `pipe` starts with an expression as initial value and requires you
to specify a function to transform that initial value.
```rust
let result = pipe!(2 |> times2);
// same as times2(2)
```

You can get more fancy with functions, too, if you add parentheses like
in a normal function call, the passed parameters will be applied to that
function after the transformed value.

```rust
let result = pipe!(2 |> (times(2)));
// same as times(2, 2)
```

You can pass closures \o/! A closure must be wrapped in parentheses as well.
```rust
let result = pipe!(
  2
  |> (times(2))
  |> (|i: u32| i * 2)
);
// same as (|i: u32| i * 2)(time(2, 2))
```

If you want a function to be called as a method on the transform value, just pass it as a path.

```rust
let result = pipe!(
    "abcd"
    |> str::len
);
// same as "abcd".len()
```

## License


Licensed under either of

 * Apache License, Version 2.0, ([LICENSE-APACHE]LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Pike is spritual successor to [pipeline.rs](https://github.com/johannhof/pipeline.rs) and derives its license from the said project.

### Contribution


Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.