Remove the -n flag from shuffle (#1823)

This commit is contained in:
Shaurya Shubham 2020-05-18 12:42:35 +05:30 committed by GitHub
parent 334685af23
commit b6cdfb1b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,34 +2,18 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, ReturnValue, Value};
use nu_source::Tagged;
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use rand::thread_rng; use rand::thread_rng;
pub struct Shuffle; pub struct Shuffle;
#[derive(Deserialize)]
pub struct Arguments {
#[serde(rename = "num")]
limit: Option<Tagged<u64>>,
}
impl WholeStreamCommand for Shuffle { impl WholeStreamCommand for Shuffle {
fn name(&self) -> &str { fn name(&self) -> &str {
"shuffle" "shuffle"
} }
fn signature(&self) -> Signature {
Signature::build("shuffle").named(
"num",
SyntaxShape::Int,
"Limit `num` number of rows",
Some('n'),
)
}
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Shuffle rows randomly." "Shuffle rows randomly."
} }
@ -43,16 +27,12 @@ impl WholeStreamCommand for Shuffle {
} }
} }
fn shuffle(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> { fn shuffle(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! { let stream = async_stream! {
let (Arguments { limit }, mut input) = args.process(&registry).await?; let mut input = args.input;
let mut values: Vec<Value> = input.collect().await; let mut values: Vec<Value> = input.collect().await;
let out = if let Some(n) = limit { let out = {
let (shuffled, _) = values.partial_shuffle(&mut thread_rng(), *n as usize);
shuffled.to_vec()
} else {
values.shuffle(&mut thread_rng()); values.shuffle(&mut thread_rng());
values.clone() values.clone()
}; };