0% found this document useful (0 votes)
92 views9 pages

Uiua Array Paradigm Guide

Uploaded by

Pete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views9 pages

Uiua Array Paradigm Guide

Uploaded by

Pete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Uiua A programming language for variable dislikers

Support Uiua's development Home

Back to Docs Home

〈 Previous: Advanced Array Manipulation Next: Macros 〉

Thinking with Arrays


So far, we've covered the mechanics of working with arrays in Uiua. However,
if you are new to the array paradigm, it may not be clear how to use arrays to
solve problems.

This section covers some of the common functions and modifiers that pop up
when solving many different problems.

Masks and ▽ keep

Many languages have some sort of filter function that takes a predicate
and a list and returns a list of all the elements that satisfy the predicate. In
array languages, we take a different approach.

First, we create a mask array. A mask array is an array of 0 s and 1 s where


1 s represent the rows that satisfy the predicate. For pervasive functions, this
is extremely simple.

For example, if we wanted to create a mask of all numbers greater that 4, we


simply treat the whole array as a single unit.

1 >4. [2 8 3 9 1 7 2] � ↧ � 1/4

[2 8 3 9 1 7 2] Run < >


[0 1 0 1 0 1 0]
The ▽ keep function takes a mask array and an array and returns an array
of all the rows that have a 1 in the mask. This is essentially a filter.

1 ▽ >4. [2 8 3 9 1 7 2] � ↧ � 1/5

[8 9 7] Run < >

▽ keep also works with ⍜ under so that you can modify the rows that
have a 1 in the mask.

1 ⍜▽(×10) >4. [2 8 3 9 1 7 2] � ↧ � 1/5

[2 80 3 90 1 70 2] Run < >

▽ keep has a few other use cases with non-masks. See its documentation
for more.

⊚ where

The ⊚ where function converts a mask array into an array of indices where
the mask is 1 .

1 ⊚. >4. [2 8 3 9 1 7 2] � ↧ � 1/6

[2 8 3 9 1 7 2] Run < >


[0 1 0 1 0 1 0]
[1 3 5]

This works with multi-dimensional arrays as well.

1 ⊚. >4. [2_8_3 9_1_7] � ↧ � 1/6

╭─ Run < >


╷ 2 8 3
9 1 7

╭─
╷ 0 1 0
1 0 1

╭─
╷ 0 1
1 0
1 2

° un ⊚ where converts an array of indices into a mask array.

1 °⊚ [3 9 5 8] � ↧ � 1/2

[0 0 0 1 0 1 0 0 1 1] Run < >

1 °⊚ [1_2 3_4] � ↧ � 1/2

╭─ Run < >


╷ 0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 1

⊏ select ⊚ where is equivalent to ▽ keep (at least for boolean


predicates).

1 ⊏⊚ =0◿2. [2 8 3 9 1 7 2] � ↧ � 1/8

[2 8 2] Run < >

1 ▽ =0◿2. [2 8 3 9 1 7 2] � ↧ � 1/7

[2 8 2] Run < >

\ scan

The \ scan modifier is similar to / reduce , but it returns an array of all


the intermediate results.
1 /+ [1 2 3 4] � ↧ � 1/4
2 \+ [1 2 3 4]
10 Run < >
[1 3 6 10]

This can be useful when used on a mask.

For example, if we wanted to get the first word of a string, we could start by
creating a mask of all the non-space characters.

Then we can use \ scan × multiply to zero the mask after the first
word.

Finally, we can use ▽ keep to apply the mask and get the first word.

Use the arrows to see how the mask changes.

1 ▽ \× ≠@ . "What's the first word?" � ↧ � 1/6

"What's" Run < >

⬚ fill

Recall that the ⬚ fill modifier sets a "fill value" that can be used by certain
functions.

One common use is to set a default value that will be used when the shapes
of arrays do not match.

1 ⬚0+ 10_20 3_4_5_6 � ↧ � 1/3

[13 24 5 6] Run < >

For example, if you wanted to logical OR two masks with different shapes,
you could use ⬚ fill with a different fill value depending on what you
want to do with the mismatched parts.
1 ⬚0↥ 1_0_0_1_0 0_1_0 � ↧ � 1/6
2 ⬚1↥ 1_0_0_1_0 0_1_0
[1 1 0 1 0] Run < >
[1 1 0 1 1]

Another interesting use is a ⬚ fill ed ↻ rotate . Instead of wrapping


values around, it fills in one side of the array with the fill value.

1 ↻¯2 [1 2 3 4 5] � ↧ � 1/6
2 ⬚0↻¯2 [1 2 3 4 5]
[4 5 1 2 3] Run < >
[0 0 1 2 3]

⊜ partition

⊜ partition is a powerful modifier that splits up an array based on a list


of consecutive keys. Before explaining it further, let's look at a simple example
of a very common use case: splitting a string into words.

1 ⊜□ ≠@ . "Look at that!" � ↧ � 1/5

{"Look" "at" "that!"} Run < >

First, we create a mask of all the non-space characters. Then, ⊜ partition


calls □ box on each section of the string that corresponds to a run of 1 s in
the mask.

Here is another example using ⊜ partition □ box with the inputs


explicitly defined.

1 [1 2 3 4 5 6 7 8] � ↧ � 1/3
2 [1 1 0 5 6 6 0 1]
3 ⊜□
{[1 2] [4] [5 6] [8]} Run < >
Notice that 0 s in the keys array cause the corresponding sections of the
input array to be skipped, so 3 and 7 are omitted from the output.

We use □ box here because the resulting sections have different lengths. If
we expect the sections to have the same lengths, we can use ∘ identity
instead.

1 [1 2 3 4 5 6 7 8] � ↧ � 1/3
2 [1 1 2 2 0 0 3 3]
3 ⊜∘
╭─ Run < >
╷ 1 2
3 4
7 8

⊜ partition is very useful when working with strings. See the Strings
tutorial for more.

A hint for one of the challenges below: ⊜ partition works with


⍜ under !

Challenges

Challenge 1

Write a program that negates each number in a list that is not a multiple
of 3.

1 � ↧ �

Run
Example: [1 2 3 4 5 6 7 8 9]
[¯1 ¯2 3 ¯4 ¯5 6 ¯7 ¯8 9]

Input: [3 0 1 8]
[3 0 1 8]
Input: [3 6 9 12 15 18 21 25 27]
[3 6 9 12 15 18 21 25 27]

Challenge 2

Write a program that returns the last word of a string.

1 � ↧ �

Run
Example: "What's the last word?"
"word?"

Input: "Um, I um, arrays"


"Um, I um, arrays"

Input: "I like trains"


"I like trains"

Challenge 3

Write a program that for every multiple of 3 in a list, multiplies the


following number by 10.

1 � ↧ �

Run
Example: [1 2 3 4 5 6 7]
[1 2 3 40 5 6 70]

Input: [2 9 3 8 7 1]
[2 9 3 8 7 1]

Input: [3 3 3 3]
[3 3 3 3]

Challenge 4

Write a program that given a matrix of 0s an 1s, only keeps the 1s that
have even x and y coordinates.

1 � ↧ �

Run
Example: [1_1_0 0_1_1 0_1_1]
╭─
╷ 1 0 0
0 0 0
0 0 1

Input: ↯3_4 1_0_1


╭─
╷ 1 0 1 1
0 1 1 0
1 1 0 1

Input: ↯4_4 1_0_0_1_0


╭─
╷ 1 0 0 1
0 1 0 0
1 0 1 0
0 1 0 1

Challenge 5

Write a program that reverses each word in a string but keeps the words
in the same order.

1 � ↧ �
Example: "get in the racecar" Run
"teg ni eht racecar"

Input: "arrays are neat"


"arrays are neat"

Input: "wow mom"


"wow mom"

〈 Previous: Advanced Array Manipulation Next: Macros 〉

Back to Docs Home

You might also like