Skip to content
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

Closed
wants to merge 29 commits into from
Closed

Index atom #789

wants to merge 29 commits into from

Conversation

DaddyWesker
Copy link
Contributor

@DaddyWesker DaddyWesker commented Oct 25, 2024

Index-atom function added. Usage:

!(index-atom (5 2) 1)
[2]

So it allows to get atom from expression (created by cons-atom) using index.

@DaddyWesker DaddyWesker requested a review from vsbogd October 25, 2024 11:17
Comment on lines +1181 to +1193
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()])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I would use Number as an index argument instead of converting atom via string
  2. I would use internal logic of the Vec::get method to check index bounds
  3. 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:
Suggested change
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])
Copy link
Collaborator

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.

Suggested change
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])

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add:

  1. Rust unit tests to check it works
  2. Documentation into both stdlib.metta and stdlib_minimal.metta

@vsbogd
Copy link
Collaborator

vsbogd commented Oct 25, 2024

Why do you have commits from other branches in this one?

@DaddyWesker
Copy link
Contributor Author

Why do you have commits from other branches in this one?

I'm working in several branches simultaneously and somehow it got really messy =(

@DaddyWesker
Copy link
Contributor Author

Thanks for your comments. I'll look into them and make suggested changes.

@DaddyWesker DaddyWesker deleted the index-atom branch October 28, 2024 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants