#function #macro #pipe #composition #elixir

pike

A macro collection to pipe |> your functions calls, like in functional languages such as F#, Elixir and OCamel

7 releases

0.1.6 Jun 21, 2024
0.1.5 Jun 21, 2024

#407 in Rust patterns

Download history 113/week @ 2024-07-06 124/week @ 2024-07-13 53/week @ 2024-07-20 163/week @ 2024-07-27 157/week @ 2024-08-03 28/week @ 2024-08-10 29/week @ 2024-08-17 101/week @ 2024-08-24 208/week @ 2024-08-31 70/week @ 2024-09-07 101/week @ 2024-09-14 49/week @ 2024-09-21 27/week @ 2024-09-28 6/week @ 2024-10-05 4/week @ 2024-10-12 54/week @ 2024-10-19

95 downloads per month
Used in 4 crates (via git-next-core)

MIT/Apache

14KB
229 lines

Pike - PipeLike

Pike is a macro collection to pipe your functions calls, like in functional languages such as F#, Elixir and OCamel.

Examples

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

TL;DR

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

// same as

let len = times("abcd".len() as u32, 2).to_string();
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! {
  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! {
  1
  |> (times(2))
  |> (times(3))
};
assert_eq!(num, 1 * 2 * 3);

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

// Closures can also be pipelined similar to partial functions.
let c = pike! {
  ['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! {
  it
  |> &
  |> is_it
};
assert_eq!(is_it, true);


// There are also special macros for options and results but those already have an ergonomic API for chaining.

let data = pike_opt!(id |> get_cached |> fetch_local |> fetch_remote);
// same as get_cached(id).or_else(|| fetch_local(id)).or_else(|| fetch_remote(id));

let result = pike_res!("http://rust-lang.org" |> download |> parse |> get_links);
// same as download("http://rust-lang.org").map(parse).map(get_links);

Macros

  • pike! is the "standard" pipe macro
  • pike_res! works like pike! 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.
  • pike_opt! works like pike! 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 pike starts with an expression as initial value and requires you to specify a function to transform that initial value.

let result = pike!(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.

let result = pike!(2 |> (times(2)));
// same as times(2, 2)

You can pass closures \o/! A closure must be wrapped in parentheses as well.

let result = pike! {
  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.

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

License

Licensed under either of

at your option.

Pike is spritual successor to 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.

No runtime deps