4 ms·
If I were designing a functional language, I'd be thinking about whether data.splitBy or delimiter.split was useful even when not immediately invoked. Consider
by thefifthsetpin 6y ago
If I were designing a functional language, I'd be thinking about whether data.splitBy or delimiter.split was useful even when not immediately invoked.
Consider this crude csv parser:
using splitBy:
csv.splitBy("\n").map(csv_row=>csv_row.splitBy(","))
using split:
"\n".split(csv).map(",".split)
split was the clear winner here. Given splitBy, I basically recreated split as a lambda.
I tried to create an analog to that where splitBy would come out looking better. I figured that if we didn't know the dimensionality of our data, then delimiters becomes an array of arbitrary length and we could pass data.!splitBy into something like delimiters.reduce. When actually writing that, however, I wound up recreating split again:
using splitBy:
delimiters.reduce((accum,delim)=>accum.deepMap(data=>data.splitBy(delim)),[data])
using split:
delimiters.reduce((accum,delim)=>accum.deepMap(delim.split),[data])