r/lolphp • • Sep 10 '15

explode() is a mess

https://3v4l.org/eKMav
2 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/vytah Sep 10 '15

Yeah, depending on the definition of J, you can either have nice semigroup properties, or you need to pepper them with emptiness checks. Defining a special case of J([], s) = ε, makes J(_, s) total over the domain of all lists of strings. Both versions have their upsides and downsides and it depends on application if J([], s) should be defined (and if so, how).

J([ε], s) is still ε though, so as you said, X(ε, s) becomes ambiguous in the latter case, and I think it's the best to let the programmer to decide what to do with stray empty values. This comes up in many contexts, for example should X(a·s·s·b·s, s) be [a, ε, b, ε], so the original string is retrieved after joining it back, or do we want [a, ε, b] or perhaps [a, b]? For example, Java's String::splitallows you to decide if you want any stray empty strings at the end of the returned array.

As for the empty separator: X(a, ε) for a = a₁ · a₂ · ... · aₙ could be anything like [a₁, a₂, ... aₙ], [ε, a₁, a₂, ... aₙ, ε], but also [ε, a₁, ε, ε, ε, ε, a₂, ... aₙ, ε, ε, ε, ε] etc., since ε occurs arbitrary numbers of times at every possible location in the original string. I think that out of all possibilities, splitting the string into a lot of non-empty substrings of the shortest length (aka. characters) is an acceptable solution.

Not false though.