Builtin

str_rel

str_rel{s:str, sep:str, r{seq:int, ss:str}}

A relation generator. Can convert string s into substrings (ss) and vice-versa.

If sep is provided it is used to split the string (or join the substrings) otherwise no separator is used and the string is split (or joined) by character.

If sep is "\N", where N is an integer, the splits are in groups of N characters.

If sep is "\b", the splits/joins are byte-based.

{seq:int} is used to sequence the substrings.

Examples

split into characters
str_rel(s:="hello world")
sepr
seqss
0h
1e
2l
3l
4o
5
6w
7o
8r
9l
10d

str_rel has taken the str s and converted it into a nested relation, r (hence the name: str_rel)


find the position(s) of the space characters
str_rel(s:="this has spaces")..r[ss=" "]
seqss
4
8

join words
str_rel(r:={seq:int,ss:str}{[0,"hello"],[1,"world"],}, sep:=" ")
s
hello world

bulk apply via composition to get reversed CITY
$S{*, s:=CITY}(str_rel){*, r:=r{*, seq:=-seq}}(str_rel){*, YTIC:=s}
SNOSNAMESTATUSYTIC
S1Smith20nodnoL
S2Jones10siraP
S3Blake30siraP
S4Clark20nodnoL
S5Adams30snehtA

Note: since the second str_rel builds left to right, from smallest seq to largest, we can reverse the strings by inverting the seq provided by the first str_rel using seq:=-seq.

To see how this works here's the intermediate relation, after the first composition:

$S{*, s:=CITY}(str_rel)
SNOSNAMESTATUSsepr
S1Smith20
seqss
0L
1o
2n
3d
4o
5n
S2Jones10
seqss
0P
1a
2r
3i
4s
S3Blake30
seqss
0P
1a
2r
3i
4s
S4Clark20
seqss
0L
1o
2n
3d
4o
5n
S5Adams30
seqss
0A
1t
2h
3e
4n
5s