1

What would be the best way to combine "__L_" and "_E__" to "_EL_" in Scala?

I don't want to use if and for commands.

10
  • 6
    def combine(x: String, y: String) = "EL". But seriously, your problem is severely underspecified. Commented Nov 21, 2015 at 8:27
  • 2
    @JulleImmonen you should add that description to the question body Commented Nov 21, 2015 at 9:12
  • 2
    @JulleImmonen what if both symbols are defined at specific position? Commented Nov 21, 2015 at 9:37
  • 2
    Please edit your question to clarify what you mean. Are you looking to concatenate two strings, without the last character of the first and the first character of the second? Or do you need to check that last/first characters are from some set of "hidden letters"? Or that they match? Really, you've not explained your problem yet. Commented Nov 21, 2015 at 9:38
  • 3
    @TheArchetypalPaul I guess it's homework restriction Commented Nov 21, 2015 at 9:55

2 Answers 2

3

How about this:

def combine(xs: String, ys: String): String = 
  (xs zip ys).map {
    case ('_', y) => y
    case (x, _) => x 
  }.mkString("")

The only thing that is not really nice about this is how to get back from a collection (IndexedSeq[Char]) to a string. An alternative is to use the String constructor that takes an Array[Char]. That would probably be more efficient.

Note that zip will work for strings of different length, but the result will be the size of the shorter string. This may or may not be what you want.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry it's my accidental misclick downvote. If you will do some minor edit to your answer I will able to remove it.
My downvote wasn't accidental :) combine("_E_", "_L_") //> res0: String = _E_ is not a desirable outcome
What would be the desirable outcome in that case?
"-EL-". The OP even gives that as an example in a comment (using-, not _ because _ gets eaten by the markdown)
@TheArchetypalPaul Your donwvote should have been accidental. The words from example have 4 chars.
|
2
def zipStrings(first: String,
               second: String,
               comb: (Char, Char) => String = (f, _) => f.toString, 
               placeholder: Char = '_') =
  first.zipAll(second, '_', '_').map {
  case (c, `placeholder`) => c
  case (`placeholder`, c) => c
  case (f, s) => comb(f, s)
}.mkString

that prioritises characters from first over second by default

zipStrings("__A", "X_CD") // yields "X_AD"

zipStrings("A__YY", "BXXXX", (f, s) => s"($f|$s)") // yields "(A|B)XX(Y|X)(Y|X)"

For you original strings:

zipStrings("L_", "_E")  // yield "LE"

zipStrings("--L-", "-E--", placeholder = '-') // yields "-EL-"

4 Comments

It would be good to give examples for the OP's two test cases zipStrings("E_", "_L") //> res0: String = EL works, but zipStrings("_E_", "_L_") //> res1: String = _E_ doesn't. Maybe I'm invoking it incorrectly though.
@TheArchetypalPaul You misreaded the second example it's "--L-" and "-E--" => "-EL-".
yes, I missed that. But now I think it's really unclear what the requirement is. Reversed the downvote, but I think the various answers (including mine) show the futility of trying to guess what the OP meant...
This reminds me of a thing I saw sometime about overcomplicated solutions: synflood.at/blog/index.php?/archives/… :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.