Skip to content

Commit

Permalink
allow taking and dropping from scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 3, 2024
1 parent 8e2e260 commit ad779f3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/algorithm/dyadic/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ impl<T: ArrayValue> Array<T> {
impl Value {
/// Use this value to `take` from another
pub fn take(self, from: Self, env: &Uiua) -> UiuaResult<Self> {
if from.rank() == 0 {
return Err(env.error("Cannot take from scalar"));
}
let index = self.as_ints_or_infs(env, "Index must be a list of integers or infinity")?;
Ok(match from {
Value::Num(a) => Value::Num(a.take(&index, env)?),
Expand Down Expand Up @@ -417,6 +414,9 @@ impl<T: ArrayValue> Array<T> {
/// `take` from this array
pub fn take(mut self, index: &[Result<isize, bool>], env: &Uiua) -> UiuaResult<Self> {
let map_keys = self.take_map_keys();
if self.rank() == 0 {
self.shape.push(1);
}
let row_count = self.row_count();
let mut arr = match index {
[] => self,
Expand Down Expand Up @@ -606,10 +606,10 @@ impl<T: ArrayValue> Array<T> {
}
/// `drop` from this array
pub fn drop(mut self, index: &[Result<isize, bool>], env: &Uiua) -> UiuaResult<Self> {
let map_keys = self.take_map_keys();
if self.rank() == 0 {
return Err(env.error("Cannot drop from scalar"));
self.shape.push(1);
}
let map_keys = self.take_map_keys();
let row_count = self.row_count();
let mut arr = match index {
[] => self,
Expand Down Expand Up @@ -688,12 +688,15 @@ impl<T: ArrayValue> Array<T> {
name: &str,
past: &str,
index: &[isize],
into: Self,
mut into: Self,
env: &Uiua,
) -> UiuaResult<Self> {
if self.map_keys().is_some() {
return Err(env.error("Cannot undo take from map array"));
}
if into.rank() == 0 {
into.shape.push(1);
}
let from = self;
match from.rank().cmp(&into.rank()) {
Ordering::Less => {
Expand Down Expand Up @@ -751,10 +754,13 @@ impl<T: ArrayValue> Array<T> {
}
})
}
fn undo_drop(self, index: &[isize], into: Self, env: &Uiua) -> UiuaResult<Self> {
fn undo_drop(self, index: &[isize], mut into: Self, env: &Uiua) -> UiuaResult<Self> {
if self.map_keys().is_some() {
return Err(env.error("Cannot undo drop from map array"));
}
if into.rank() == 0 {
into.shape.push(1);
}
let index: Vec<isize> = index
.iter()
.zip(&into.shape)
Expand Down

0 comments on commit ad779f3

Please sign in to comment.