Skip to content

Commit

Permalink
filled scan sets initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Nov 21, 2024
1 parent 360c802 commit e483163
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This version is not yet released. If you are reading this on the website, then t
- This new behavior is more useful
- **Breaking Change**: [`un °`](https://uiua.org/docs/un) [`json`](https://uiua.org/docs/json) no longer attempts to form multidimensional arrays
- This makes deserializing JSON more consistent
- **Breaking Change**: [`fill ⬚`](https://uiua.org/docs/fill)ed [`scan \\`](https://uiua.org/docs/scan) now sets the initial value as well as filling row shapes
- This behavior is hard to get otherwise
- Fixes to existing code should be simple
- Stabilize [`sort ⍆`](https://uiua.org/docs/sort)
- Sorting is a very common operation, and it's useful to have such simple access to it
- Stabilize [`last ⊣`](https://uiua.org/docs/last)
Expand Down
2 changes: 1 addition & 1 deletion site/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn all_fills() -> impl IntoView {
{ fill_row(Reshape, "Fills excess elements", "⬚0↯ 2_4 [1 2 3]") }
{ fill_row(Rotate, "Fills instead of wrapping", "⬚0↻ 2 [1 2 3 4 5]") }
{ fill_row(Reduce, "Sets initial value", "⬚10/+ [1 2 3]") }
{ fill_row(Scan, "Fills row shapes", "⬚10\\⊂ [1 2 3]") }
{ fill_row(Scan, "Sets initial value and fills row shapes", "⬚10\\⊂ [1 2 3]") }
{ fill_row(Rows, "Fills row shapes", "⬚0≡⇡ [4 7 3]") }
{ fill_row(Each, "Fills row shapes", "⬚0∵⇡ [3_2 2_4]") }
{ fill_row(Partition, "Fills row shapes", "⬚@ ⊜∘ ≠@ . \"Hey there\"") }
Expand Down
14 changes: 12 additions & 2 deletions src/algorithm/reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ pub fn scan(ops: Ops, env: &mut Uiua) -> UiuaResult {
if xs.rank() == 0 && f.sig.args <= 2 {
return Err(env.error(format!("Cannot {} rank 0 array", Primitive::Scan.format())));
}
if env.value_fill().is_some() {
return generic_scan(f, xs, env);
}
match (f.node.as_flipped_primitive(), xs) {
(Some((prim, flipped)), Value::Num(nums)) => {
let arr = match prim {
Expand Down Expand Up @@ -771,12 +774,19 @@ fn generic_scan(f: SigNode, xs: Value, env: &mut Uiua) -> UiuaResult {
))),
2 => {
if xs.row_count() == 0 {
env.push(xs.first_dim_zero());
env.push(
env.value_fill()
.cloned()
.unwrap_or_else(|| (xs.first_dim_zero())),
);
return Ok(());
}
let row_count = xs.row_count();
let mut rows = xs.into_rows();
let mut acc = rows.next().unwrap();
let mut acc = env
.value_fill()
.cloned()
.unwrap_or_else(|| rows.next().unwrap());
let mut scanned = Vec::with_capacity(row_count);
scanned.push(acc.clone());
env.without_fill(|env| -> UiuaResult {
Expand Down
7 changes: 7 additions & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,13 @@ primitive!(
/// ex: ⊕□\+=@ . "Everyday man's on the block"
/// : ⊕□\+↻¯1=@ . "Everyday man's on the block"
///
/// [fill] both sets the initial value and fills mismatched shapes if necessary.
/// ex: \+ [1 2 3 4 5]
/// : ⬚@a\+ [1 2 3 4 5]
/// ex: +1⇡5
/// : ⬚0\⊂ .
/// : ↘1_1 .
///
/// If the function takes more than 2 arguments, additional arguments above the array on the stack will be passed to the function on every iteration.
/// ex: \(+×) 10 [1 2 3 4]
/// ex: ⬚@ \(⊂⊂) @, "abcd"
Expand Down
2 changes: 1 addition & 1 deletion tests/loops.ua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ A ← ↯2_3_4⇡24

# Scan
⍤⤙≍ [1 3 6 10] \+[1 2 3 4]
⍤⤙≍ [1_0_0 1_2_0 1_2_3] ⬚0\⊂ [1 2 3]
⍤⤙≍ [0_0_0_0 0_1_0_0 0_1_2_0 0_1_2_3] ⬚0\⊂ [1 2 3]
⍤⤙≍ [1 2 3 4 5] °\+[1 3 6 10 15]
⍤⤙≍ [1_2_3 4_5_6 7_8_9] °\+[1_2_3 5_7_9 12_15_18]
⍤⤙≍ ⊃(⊂⊢:≡/=◫2.|°\=) [1 0 1 1 0]
Expand Down
1 change: 0 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
## 0.14
The next version of Uiua

- Code macro auto-repr
- Stabilize `backward`, `case`
- Optimize `group`/`partition` reduction replacement
- Compile-time code string evaluation
Expand Down

0 comments on commit e483163

Please sign in to comment.