Add discrete list/table

This commit is contained in:
JT
2021-09-04 18:52:28 +12:00
parent 74bb2af3e1
commit 5e33b8536b
7 changed files with 142 additions and 12 deletions

View File

@ -42,6 +42,22 @@ impl Command for Each {
match input {
Value::List { val, .. } => Ok(Value::List {
val: val
.into_iter()
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let state = context.enter_scope();
state.add_var(var_id, x.clone());
//FIXME: DON'T UNWRAP
eval_block(&state, block, Value::nothing()).unwrap()
})
.collect(),
span: call.head,
}),
Value::ValueStream { stream, .. } => Ok(Value::ValueStream {
stream: stream
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);

View File

@ -53,8 +53,8 @@ impl Command for For {
let context = context.clone();
match values {
Value::List { val, .. } => Ok(Value::List {
val: val
Value::ValueStream { stream, .. } => Ok(Value::ValueStream {
stream: stream
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
@ -68,6 +68,22 @@ impl Command for For {
.into_value_stream(),
span: call.head,
}),
Value::List { val, .. } => Ok(Value::List {
val: val
.into_iter()
.map(move |x| {
let engine_state = context.engine_state.borrow();
let block = engine_state.get_block(block);
let state = context.enter_scope();
state.add_var(var_id, x.clone());
//FIXME: DON'T UNWRAP
eval_block(&state, block, Value::nothing()).unwrap()
})
.collect(),
span: call.head,
}),
_ => Ok(Value::nothing()),
}
}

View File

@ -25,7 +25,7 @@ impl Command for Length {
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
match input {
Value::List { val, .. } => {
let length = val.count();
let length = val.len();
Ok(Value::Int {
val: length as i64,
@ -33,7 +33,23 @@ impl Command for Length {
})
}
Value::Table { val, .. } => {
let length = val.count();
let length = val.len();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
Value::ValueStream { stream, .. } => {
let length = stream.count();
Ok(Value::Int {
val: length as i64,
span: call.head,
})
}
Value::RowStream { stream, .. } => {
let length = stream.count();
Ok(Value::Int {
val: length as i64,