Created
April 25, 2019 16:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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