join
Joins a set of strings into a single string
Syntax
text = join[token, given, index, with]
Attributes
token
- set of strings to be joinedgiven
- establishes the set being joined. If tokens are not unique, you can add attributes here that will make them unique. Must at least providetoken
as part of the given set, or only the first one will be returned.index
- indicates where eachtoken
is ordered intext
.with
- inserted between every element intoken
.
Description
text = join[token, index, given, with]
takes tokens
together using with
in an order specified by index
. Returns the joined string.
Examples
Split a sentence into tokens, and join the tokens into a sentence again
search
// Split the sentence into words
(token, index) = split[text: "the quick brown fox", by: " "]
// Join the words back into a sentence, but with hyphens instead of spaces
text = join[token given: token, index with: "-"]
bind @view
[#value | value: text] // Expected "the-quick-brown-fox"
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.
Let’s split the phrase “hello world” into letters:
search
//token = (h, e, l, l, o, w, o, r, l, d)
(token, index) = split[text: "hello world", by: ""]
bind
[#phrase token index]
bind @view
[#value | value: token]
Let’s join this phrase back together. Like last time, we’ll join with a -
. Notice that some tokens (“l” and “o”) should appear multiple times in the phrase. To correctly join them, we add index
as part of the given
set:
search
[#phrase token index]
// given = (("h", 1), ("e", 2), ("l", 3), ("l", 4) ... ("l", 10), ("d", 11))
// without including index, the result is "h-e-l-o- -w-r-d". Try it and see!
text = join[token given: (token, index) index with: "-"]
bind @view
[#value | value: text]
The result expected result is “h-e-l-l-o- -w-o-r-l-d”.