Skip to content

Commit

Permalink
use new member in docs
Browse files Browse the repository at this point in the history
fixes #537
  • Loading branch information
kaikalii committed Aug 16, 2024
1 parent 359d872 commit 9e5d28a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/goldbach.ua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Calculate the primes that add up to the first n even numbers

Primes ← ▽¬∊:♭⊞×...+2⇡
Primes ← ▽¬♭⊞×...+2⇡
Goldbach ← ∵(|1
▽:⟜(/+=⊞+.)Primes..
⊂:⊟⊢:⊢⇌.
Expand Down
4 changes: 2 additions & 2 deletions site/src/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,10 @@ fn TutorialArrays() -> impl IntoView {
<Editor example=r#"["Uiua" "APL" "J" "BQN" "K" "Q"] # Fails"#/>
<Editor example=r#"{"Uiua" "APL" "J" "BQN" "K" "Q"} # Works!"#/>
<p>"Functions that require their arguments to have matching types may require "<Prim prim=Box/>"ing an argument."</p>
<p>"For example, to check if a string is in a list of "<Prim prim=Box/>"ed strings with "<Prim prim=Member/>", you would need to "<Prim prim=Box/>" the string first."</p>
<p>"For example, to check if a string is in a list of "<Prim prim=Box/>"ed strings with "<Prim prim=MemberOf/>", you would need to "<Prim prim=Box/>" the string first."</p>
<Editor example=
r#"Langs ← {"Uiua" "APL" "J" "BQN" "K" "Q"}
"APL" Langs"#/>
∈ Langs "APL""#/>
<p>"Pervasive functions work through boxes and preserve the maximum "<Prim prim=Box/>" depth of their arguments."</p>
<Editor example="¯ 1\n¯ □1\n¯ □□1"/>
<Editor example="+1 4\n+1 □4\n+1 □□4\n+□□1 □4"/>
Expand Down
46 changes: 24 additions & 22 deletions site/text/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,73 @@ It can also be used on arrays of boxed strings.

As discussed in the [Thinking With Arrays](/tutorial/thinkingwitharrays) tutorial, [`partition`]() can be used to split an array by a delimiter.

First, we create a mask of places where the delimiter is *not* using [`not equals ≠`](). In this case, we'll use the space character.
First, we create a mask of places where the delimiter is *not* using [`by`]() [`not equals ≠`](). In this case, we'll use the space character.
```uiua
≠@ . "Split this string"
≠@ "Split this string"
```
[`partition`]() will then split the strings at the places where the mask changes, ommitting `0`s.
[`partition`]() will then split the strings at the places where the mask changes, omitting `0`s.
```uiua
⊜□ ≠@ . "Split this string"
⊜□ ≠@ "Split this string"
```
Notice that this reads almost as a description of what it does: "Partition box by not equals space"

[`partition`]() has an alternate functionality when its function has signature `|2.1` instead of `|1.1`. This will perform a reduction operation, similar to [`reduce`]().

Using [planet notation](/tutorial/advancedstack#planet-notation), we can select the first or last split section.
```uiua
⊜⊙◌ ≠@ . "Split this string"
⊜⊙◌ ≠@ "Split this string"
```
```uiua
⊜⋅∘ ≠@ . "Split this string"
⊜⋅∘ ≠@ "Split this string"
```
For parts of the string that are not the first or last, we can simply [`box`]() and [`select`]().
```uiua
⊏1_3 ⊜□≠@,. "lorem,ipsum,dolor,sit,amet"
⊏1_3 ⊜□≠@, "lorem,ipsum,dolor,sit,amet"
```

[`partition`]() can be nested to split by multiple delimiters.

For example, if you were reading from a file that contained rows of numbers separated by spaces, you could use [`partition`]() to create a multi-dimensional array.

Here, the contents of the file will be represented as a multi-line string. We use [`parse`]() as the inner function to parse the numbers.

```uiua
$ 1 8 4 99
$ 5 20 0 0
$ 78 101 1 8
⊜(⊜⋕≠@ .)≠@\n.
⊜(⊜⋕≠@ )⊸≠@\n
```

This assumes that the two delimiters delimit different dimensions of the array. If they delimit the same dimension, we can use [`not`]()[`member`]().
This assumes that the two delimiters delimit different dimensions of the array. If they delimit the same dimension, we can use [`not`]() and [`memberof`]().
```uiua
$ 1 8 4 99
$ 5 20 0 0
$ 78 101 1 8
⊜⋕¬∊," \n"
⊜⋕¬⊸∈" \n"
```

## Finding substrings with [`mask`]()

What if we want to split by a non-scalar delimiter? Simply dropping a string delimiter into the code above produces an error.
```uiua should fail
⊜□ ≠" - ". "foo - bar - ba-az"
⊜□ ≠" - " "foo - bar - ba-az"
```
We might try [`find`](). While there may be cases when this output is useful, it is not quite what we want here.
```uiua
⌕" - " "foo - bar - ba-az"
⊜□ ¬⌕" - ". "foo - bar - ba-az"
⌕" - " "foo - bar - ba-az"
⊜□ ¬⌕" - " "foo - bar - ba-az"
```
This is because [`find`]() only marks the start of each matching substring.

[`mask`]() marks each substring with an increasing number.
```uiua
⦷" - ". "foo - bar - ba-az"
⦷" - " "foo - bar - ba-az"
```
This works great with [`partition`]() to split the string how we want.
```uiua
⦷" - " "foo - bar - ba-az"
¬⦷" - " "foo - bar - ba-az"
⊜□ ¬⦷" - ". "foo - bar - ba-az"
⦷" - " "foo - bar - ba-az"
¬⦷" - " "foo - bar - ba-az"
⊜□ ¬⦷" - " "foo - bar - ba-az"
```
Notice that while [`not`]() leaves parts of the mask negative, [`partition`]() ignores all sections that are not positive.

Expand All @@ -99,17 +101,17 @@ Because [`under`]() works with [`partition`](), we can use it with [`mask`]() to

In this example, we replace each row of the [`partition`]()ed array with the string `"orb"`.
```uiua
⦷ "ab" "abracadabra"
⊜∘ ⦷ "ab". "abracadabra"
⍜⊜∘≡⋅"orb" ⦷ "ab". "abracadabra"
⦷ "ab" "abracadabra"
⊜∘ ⦷ "ab" "abracadabra"
⍜⊜∘≡⋅"orb" ⦷ "ab" "abracadabra"
```
This can even be used to replace the matches with different strings.
```uiua
⍜⊜□◌ ⦷ "ab". "abracadabra" {"[first]" "[second]"}
⍜⊜□◌ ⦷ "ab" "abracadabra" {"[first]" "[second]"}
```
Here is how you might replace with a variable number of strings.
```uiua
F ← ⍜⊜□(↙⧻) ⦷ "ab".:°⋕⇡10
F ← ⍜⊜□(↙⧻) ⦷ "ab" :°⋕⇡10
F "abracadabra"
F "abcdefg"
F "ababab|abababab"
Expand Down
2 changes: 1 addition & 1 deletion src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) mod enabled {
use crate::Uiua;

const BENCHMARKS: &[(&str, &str)] = &[
("PRIMES", "▽¬∊:♭⊞×...+2⇡1000"),
("PRIMES", "▽¬♭⊞×...+2⇡1000"),
(
"STRIPES",
"\
Expand Down

0 comments on commit 9e5d28a

Please sign in to comment.