New set_from_array command

This commit is contained in:
sagie gur ari 2020-05-07 16:29:32 +00:00
parent f519a20074
commit 9e6b35ce07
9 changed files with 132 additions and 1 deletions

View file

@ -2,6 +2,7 @@
### v0.3.4
* New set_from_array command.
* New set_to_array command.
* New set_contains command.
* New set_remove command.

View file

@ -38,6 +38,7 @@
* [std::collections::Set (set_new)](#std__collections__Set)
* [std::collections::SetClear (set_clear)](#std__collections__SetClear)
* [std::collections::SetContains (set_contains)](#std__collections__SetContains)
* [std::collections::SetFromArray (set_from_array)](#std__collections__SetFromArray)
* [std::collections::SetIsEmpty (set_is_empty)](#std__collections__SetIsEmpty)
* [std::collections::SetPut (set_put, set_add)](#std__collections__SetPut)
* [std::collections::SetRemove (set_remove)](#std__collections__SetRemove)
@ -1622,6 +1623,52 @@ found = set_contains ${handle} value2
#### Aliases:
set_contains
<a name="std__collections__SetFromArray"></a>
## std::collections::SetFromArray
```sh
set_handle = set_from_array array_handle
```
Returns a set handle created from the provided array values.
#### Parameters
The array handle.
#### Return Value
The new set handle.
#### Examples
```sh
array_handle = array value1 value2 value3
set_handle = set_from_array ${handle}
```
#### Source:
```sh
if not is_array ${scope::set_from_array::argument::1}
trigger_error "Invalid input, non array handle or array not found."
end
scope::set_from_array::set = set_new
for scope::set_from_array::next_value in ${scope::set_from_array::argument::1}
set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value}
end
set ${scope::set_from_array::set}
```
#### Aliases:
set_from_array
<a name="std__collections__SetIsEmpty"></a>
## std::collections::SetIsEmpty

View file

@ -30,6 +30,7 @@ mod read_properties;
mod set;
mod set_clear;
mod set_contains;
mod set_from_array;
mod set_is_empty;
mod set_put;
mod set_remove;
@ -78,6 +79,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(set::create(&package))?;
commands.set(set_clear::create(&package))?;
commands.set(set_contains::create(&package))?;
commands.set(set_from_array::create(&package)?)?;
commands.set(set_is_empty::create(&package)?)?;
commands.set(set_put::create(&package))?;
commands.set(set_remove::create(&package))?;

View file

@ -0,0 +1,20 @@
```sh
set_handle = set_from_array array_handle
```
Returns a set handle created from the provided array values.
#### Parameters
The array handle.
#### Return Value
The new set handle.
#### Examples
```sh
array_handle = array value1 value2 value3
set_handle = set_from_array ${handle}
```

View file

@ -0,0 +1,22 @@
use crate::types::command::create_alias_command;
use crate::utils::pckg;
use duckscript::types::command::Command;
use duckscript::types::error::ScriptError;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
pub(crate) fn create(package: &str) -> Result<Box<dyn Command>, ScriptError> {
let name = pckg::concat(package, "SetFromArray");
let command = create_alias_command(
name,
vec!["set_from_array".to_string()],
include_str!("help.md").to_string(),
"set_from_array".to_string(),
include_str!("script.ds").to_string(),
1,
)?;
Ok(Box::new(command))
}

View file

@ -0,0 +1,7 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create("").unwrap());
}

View file

@ -0,0 +1,11 @@
if not is_array ${scope::set_from_array::argument::1}
trigger_error "Invalid input, non array handle or array not found."
end
scope::set_from_array::set = set_new
for scope::set_from_array::next_value in ${scope::set_from_array::argument::1}
set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value}
end
set ${scope::set_from_array::set}

View file

@ -0,0 +1,21 @@
fn test_valid
arr = array 1 2 3 1
handle = set_from_array ${arr}
size = set_size ${handle}
assert_eq ${size} 3
result = set_contains ${handle} 1
contained = not equals ${result} false
assert ${contained}
result = set_contains ${handle} 2
contained = not equals ${result} false
assert ${contained}
result = set_contains ${handle} 3
contained = not equals ${result} false
assert ${contained}
release ${handle}
release ${arr}
end

View file

@ -1,5 +1,5 @@
fn test_map_keys
fn test_valid
handle = set_new 1 2 3 4
arr = set_to_array ${handle}