Skip to content

Commit 328dcd6

Browse files
committed
allow map multiinsert for under get
1 parent 9d654a8 commit 328dcd6

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

site/primitives.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,13 @@
12011201
"description": "Get the reciprocal of a number",
12021202
"experimental": true
12031203
},
1204+
"recur": {
1205+
"outputs": 1,
1206+
"modifier_args": 3,
1207+
"class": "Algorithm",
1208+
"description": "Execute a recursive or tree algorithm",
1209+
"experimental": true
1210+
},
12041211
"recv": {
12051212
"args": 1,
12061213
"outputs": 1,

src/algorithm/map.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,27 @@ impl Value {
157157
}
158158
/// Insert a key-value pair into a map array
159159
#[allow(clippy::unit_arg)]
160-
pub fn insert(&mut self, key: Value, value: Value, env: &Uiua) -> UiuaResult {
161-
// if value.rank() == self.rank() && value.type_id() == self.type_id() {
162-
// if key.row_count() != value.row_count() {
163-
// return Err(env.error(format!(
164-
// "You appear to be inserting multiple keys. \
165-
// Inserted keys and values must have the same length, \
166-
// but their shapes are {} and {}",
167-
// key.shape,
168-
// value.shape
169-
// )));
170-
// }
171-
// for (key, value) in key.into_rows().zip(value.into_rows()) {
172-
// self.insert(key, value, env)?;
173-
// }
174-
// return Ok(());
175-
// }
160+
pub fn insert(
161+
&mut self,
162+
key: Value,
163+
value: Value,
164+
allow_multi: bool,
165+
env: &Uiua,
166+
) -> UiuaResult {
167+
if allow_multi && value.rank() == self.rank() && value.type_id() == self.type_id() {
168+
if key.row_count() != value.row_count() {
169+
return Err(env.error(format!(
170+
"You appear to be inserting multiple keys. \
171+
Inserted keys and values must have the same length, \
172+
but their shapes are {} and {}",
173+
key.shape, value.shape
174+
)));
175+
}
176+
for (key, value) in key.into_rows().zip(value.into_rows()) {
177+
self.insert(key, value, false, env)?;
178+
}
179+
return Ok(());
180+
}
176181

177182
if !self.is_map() && self.row_count() == 0 {
178183
self.map(Value::default(), env)?;

src/compile/invert/under.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ static UNDER_PATTERNS: &[&dyn UnderPattern] = &[
177177
(PopUnd(3), UndoRegex),
178178
),
179179
// Map control
180-
&MaybeVal((Get, (CopyUnd(2), Get), (PopUnd(1), Flip, PopUnd(1), Insert))),
180+
&MaybeVal((
181+
Get,
182+
(CopyUnd(2), Get),
183+
(PopUnd(1), Flip, PopUnd(1), UndoGet),
184+
)),
181185
&Stash(2, Remove, UndoRemove),
182186
&MaybeVal((Insert, (CopyUnd(3), Insert), (PopUnd(3), UndoInsert))),
183187
// Shaping

src/impl_prim.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ impl_primitive!(
193193
(3, UndoPartition2),
194194
(1(1)[1], UndoGroup1),
195195
(3, UndoGroup2),
196+
(3, UndoGet),
196197
(4, UndoInsert),
197198
(3, UndoRemove),
198199
(1(0), TryClose),
@@ -378,6 +379,7 @@ impl fmt::Display for ImplPrimitive {
378379
UndoAntiOrient => write!(f, "{Under}{Orient}"),
379380
DoRegex => write!(f, "{Regex}"),
380381
UndoRegex => write!(f, "{Under}{Regex}"),
382+
UndoGet => write!(f, "{Under}{Get}"),
381383
UndoInsert => write!(f, "{Under}{Insert}"),
382384
UndoRemove => write!(f, "{Under}{Remove}"),
383385
UndoPartition1 | UndoPartition2 => write!(f, "{Under}{Partition}"),

src/run_prim.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub fn run_prim_func(prim: &Primitive, env: &mut Uiua) -> UiuaResult {
262262
let key = env.pop("key")?;
263263
let val = env.pop("value")?;
264264
let mut map = env.pop("map")?;
265-
map.insert(key, val, env)?;
265+
map.insert(key, val, false, env)?;
266266
env.push(map);
267267
}
268268
Primitive::Has => {
@@ -916,6 +916,13 @@ impl ImplPrimitive {
916916
env.push(left);
917917
}
918918
ImplPrimitive::TryClose => _ = run_sys_op(&SysOp::Close, env),
919+
ImplPrimitive::UndoGet => {
920+
let key = env.pop("key")?;
921+
let val = env.pop("value")?;
922+
let mut map = env.pop("map")?;
923+
map.insert(key, val, true, env)?;
924+
env.push(map);
925+
}
919926
ImplPrimitive::UndoInsert => {
920927
let key = env.pop(1)?;
921928
let _value = env.pop(2)?;

tests/under.ua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ G ← ⍜(⊙⊙⊢) ⋅⊙∘
411411
⍤⤙≍ map "abc" [1 2 3] ⍜remove∘ @d map "abc" [1 2 3]
412412
⍤⤙≍ ⟜⍜(get"abc")∘ map {"abc"} [1]
413413
⍤⤙≍ □⟜⍜(◇get"abc")∘ map {"abc"} [1]
414+
map {"alice" "bob" "charlie"} [1 2 3]
415+
⍜get(+1) {"alice" "charlie"}
416+
⍤⤙≍ map {"alice" "bob" "charlie"} [2 2 4]
414417

415418
# Pattern matching
416419
⍤⤙≍ "2 - 4" ⍜°$"_ - _"∩(×2⋕) "1 - 2"

0 commit comments

Comments
 (0)