This is a challenge I thought of recently which I think is not hard but allows for a wide range of possible solutions. Seems like a perfect fit for the creativity of PPCG users :)
Your Task
Define two functions which together form a lossless round-trip String × String → String → String × String
.
More specifically, define two functions combine
and separate
with repsective types String × String → String
and String → String × String
such that separate ∘ combine = id
.
Even more specifically, define two functions (or methods/procedures/etc), which I will refer to as combine
and separate
, and which exhibit the following characteristics:
When invoked with a single string,
separate
produces two strings as results. (You may use any reasonable string type. Most languages don't canonically allow producing multiple values, so feel free to to do something equivalent, such as returning a 2-tuple or modifying two global variables.)When invoked with two strings,
combine
produces a single string as result. (You may accept input toseparate
in the same form thatcombine
produces, or any other reasonable form.)For any two strings
a
andb
,separate(combine(a, b))
must result in two strings which are (structurally) equivalent toa
andb
.The above bullets are the only restrictions on
combine
andseparate
. So, for instance,combine ∘ separate
need not beid
, andseparate
can even be a partial function, if you like.
(Defer to the final and more-verbose description if there are any ambiguities between the three)
Intent
My goal with this challenge is to inspire creative and interesting solutions. As such, in order to avoid priming/suggesting your thinking about how to solve this problem, I will not be providing test cases, and I strongly suggest that you do not look at the existing answers until you submit your own.
Winning Criteria
This challenge is a popularity-contest; the answer with the highest score at noon on April 5th, UTC, is the winner.
Guidelines for voters, included to be a valid popularity-contest, are as follows:
- Strongly prefer answers which you do not immediately comprehend, are not obviously correct, and/or surprise you
- Prefer answers which go beyond strings, i.e., those which make use of more than the problem statement did (functions, strings, pairs)
- Avoid answers which are similar to your own solution(s)
- Avoid answers which boil down to language builtins
Have fun!
combine ∘ separate
also be an identity function? \$\endgroup\$