Skip to content

Commit 4aaea35

Browse files
authored
Merge pull request #48 from witheve/update/join
Update join
2 parents 51b9a85 + f00d237 commit 4aaea35

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

handbook/strings/join.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,69 @@ Joins a set of strings into a single string
1212
## Syntax
1313

1414
```eve
15-
text = join[token, index, with]
15+
text = join[token, given, index, with]
1616
```
1717

1818
## Attributes
1919

20-
- `token` - set of elements to be joined
21-
- `index` - indicates the order of the `tokens` in the joined string
22-
- `with` - inserted between every element in `tokens`
20+
- `token` - set of strings to be joined
21+
- `given` - establishes the set being joined. If tokens are not unique, you can add attributes here that will make them unique. Must at least provide `token` as part of the given set, or only the first one will be returned.
22+
- `index` - indicates where each `token` is ordered in `text`.
23+
- `with` - inserted between every element in `token`.
2324

2425
## Description
2526

26-
`text = join[token, index, with]` takes `tokens` tokens together using `with` in an order specified by `index`. Returns the joined string.
27+
`text = join[token, index, given, with]` takes `tokens` together using `with` in an order specified by `index`. Returns the joined string.
2728

2829
## Examples
2930

30-
Split a sentence into tokens
31+
Split a sentence into tokens, and join the tokens into a sentence again
3132

3233
```eve
3334
search
35+
// Split the sentence into words
3436
(token, index) = split[text: "the quick brown fox", by: " "]
3537
36-
bind
37-
[#token token index]
38+
// Join the words back into a sentence, but with hyphens instead of spaces
39+
text = join[token given: token, index with: "-"]
40+
41+
bind @view
42+
[#value | value: text] // Expected "the-quick-brown-fox"
3843
```
3944

40-
Join the tokens into a sentence again, but with hyphens instead of spaces
45+
---
46+
47+
Since join is an aggregate, set semantics play an important part here; if we don't specify what makes each token unique, then the results can be surprising. The following example will demonstrate this.
48+
49+
Let's split the phrase "hello world" into letters:
4150

4251
```eve
4352
search
44-
[#token token index]
45-
text = join[token, index, with: " "]
53+
//token = (h, e, l, l, o, w, o, r, l, d)
54+
(token, index) = split[text: "hello world", by: ""]
4655
4756
bind
48-
[#div text] // Expected "the-quick-brown-fox"
57+
[#phrase token index]
58+
59+
bind @view
60+
[#value | value: token]
4961
```
5062

63+
Let's join this phrase back together. Like last time, we'll join with a `-`. You'll notice that some tokens are duplicate ("l" and "o" appear multiple times in the phrase). To correctly join these tokens, we can add `index` as part of the `given` set:
64+
65+
```eve
66+
search
67+
[#phrase token index]
68+
// given = (("h", 1), ("e", 2), ("l", 3), ("l", 4) ... ("l", 10), ("d", 11))
69+
// without including index, the result is "h-e-l-o- -w-r-d". Try it and see!
70+
text = join[token given: (token, index) index with: "-"]
71+
72+
bind @view
73+
[#value | value: text]
74+
```
75+
76+
The result expected result is "h-e-l-l-o- -w-o-r-l-d".
77+
5178
## See Also
5279

53-
[join](../join) | [split](../split) | [char-at](../char-at) | [find](../find) | [length](../length) | [replace](../replace)
80+
[split](../split)

0 commit comments

Comments
 (0)