-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Index atom #789
Index atom #789
Conversation
Fixed return value
let expr = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?; | ||
let atom = args.get(1).ok_or_else(arg_error)?; | ||
let children = expr.children(); | ||
let index = atom_to_string(atom).parse::<i32>().unwrap(); | ||
if index < 0 { | ||
println!("Negative indexes currently not supported"); | ||
return unit_result() | ||
} | ||
else if (index as usize) >= children.len() { | ||
println!("Index is out of bounds"); | ||
return unit_result() | ||
} | ||
Ok(vec![children.get(index as usize).clone().expect("Unknown error").clone()]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I would use
Number
as an index argument instead of converting atom via string - I would use internal logic of the
Vec::get
method to check index bounds - It is more proper to return
Error
instead of printing it on the screen. In the former case caller can check for the error automatically:
let expr = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?; | |
let atom = args.get(1).ok_or_else(arg_error)?; | |
let children = expr.children(); | |
let index = atom_to_string(atom).parse::<i32>().unwrap(); | |
if index < 0 { | |
println!("Negative indexes currently not supported"); | |
return unit_result() | |
} | |
else if (index as usize) >= children.len() { | |
println!("Index is out of bounds"); | |
return unit_result() | |
} | |
Ok(vec![children.get(index as usize).clone().expect("Unknown error").clone()]) | |
let children = TryInto::<&ExpressionAtom>::try_into(args.get(0).ok_or_else(arg_error)?)?.children(); | |
let index = AsPrimitive::from_atom(args.get(1).ok_or_else(arg_error)?).as_number().ok_or_else(arg_error)?; | |
match children.get(Into::<i64>::into(index) as usize) { | |
Some(atom) => Ok(vec![atom.clone()]), | |
None => Err(ExecError::from("Index is our of bounds")), | |
} |
In order to make this code compile one should also make AsPrimitive
public inside arithmetics.rs
|
||
impl Grounded for IndexAtomOp { | ||
fn type_(&self) -> Atom { | ||
Atom::expr([ARROW_SYMBOL, ATOM_TYPE_EXPRESSION, ATOM_TYPE_ATOM, ATOM_TYPE_ATOM]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use Number
as an index type like in all arithmetic operations.
Atom::expr([ARROW_SYMBOL, ATOM_TYPE_EXPRESSION, ATOM_TYPE_ATOM, ATOM_TYPE_ATOM]) | |
Atom::expr([ARROW_SYMBOL, ATOM_TYPE_EXPRESSION, ATOM_TYPE_NUMBER, ATOM_TYPE_ATOM]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add:
- Rust unit tests to check it works
- Documentation into both
stdlib.metta
andstdlib_minimal.metta
Why do you have commits from other branches in this one? |
I'm working in several branches simultaneously and somehow it got really messy =( |
Thanks for your comments. I'll look into them and make suggested changes. |
Index-atom function added. Usage:
So it allows to get atom from expression (created by cons-atom) using index.