Skip to content

Instantly share code, notes, and snippets.

@randomPoison
Created April 25, 2019 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randomPoison/8b039b4810a8ad3bf0b2300dc6c2d01e to your computer and use it in GitHub Desktop.
Save randomPoison/8b039b4810a8ad3bf0b2300dc6c2d01e to your computer and use it in GitHub Desktop.
Notes and code sketches from the method chaining discussion at the April 2019 Chicago Rust Meetup.
// Hypothetical pipeline syntax.
let value = foo()? |> bar()? |> baz()?;
// Command example.
// ------------------------------------------
let command = Command::new("foo")
..arg("--bar");
if set_baz {
command
..arg("--baz")
..arg("--baz")
..arg("--baz")
..arg("--baz")
..arg("--baz")
..arg("--baz");
}
let result = command
..arg("quux")
.status()
.unwrap();
// How to write `arg` without returning `self`.
impl Command {
pub fn arg<S>(&mut self, arg: S) {
// Add the argument...
}
}
// Using command macro.
let command = cascade! {
Command::new("foo");
..arg("--bar");
}
if set_baz {
cascade! {
command
..arg("--baz");
..arg("--baz");
..arg("--baz");
..arg("--baz");
..arg("--baz");
..arg("--baz");
}
}
// MONADS OH GOD WHY
let result = Magic(command)
..arg("quux")
.status()
.unwrap();
struct Magic(T);
impl Magic {
pub fn arg(&mut self) -> &must Self {
// Do a thing.
self
}
}
// tapping
Command::new("foo")
.tap(|com| com.arg("--bar"))
.tap(|com| com.arg("--bar"))
.tap(|com| com.arg("--bar"))
.tap(|com| com.arg("--bar"));
// What if method-call syntax is bad???
let result = Command::new("foo")
..arg("--bar")
..arg("--baz")
..arg("quux")
.status()
.unwrap();
let items: Vec<_> = my_collection
.iter()
|> map(|| ...)
|> chain(|| ...)
|> filter(|| ...)
|> collect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment