PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How to concatenate Char type elements in Scala

 June 25, 2022     compiler-errors, scala     No comments   

Issue

I want to compute all the combination of size 3 from the letters of the alphabet (stored in vocabulary as Seq[Char]) and output each combination as a Seq[Char], while storing it in a sequence.

Here is the definition of the alphabet :

 val alphabet:Seq[Char] = Seq('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

I wrote this piece of code.

def enumProduct3(vocabulary: Seq[Char]): Seq[Seq[Char]] = {
    // vocabulary = alphabet
    val keys:Seq[Seq[Char]] = (for {x<-vocabulary;y<-vocabulary;z<-vocabulary} yield(x + y + z).toSeq).toSeq
  }

I get a type mismatch error :

[error]  found   : Unit
[error]  required: Seq[Seq[Char]]

I searched the Scala API to concatenate Char type elements but didn't find anything.


Solution

There are two problems in your enumProduct3 - yield should be Seq(x, y, z) and method should return value:

def enumProduct3(vocabulary: Seq[Char]): Seq[Seq[Char]] = {
  // vocabulary = alphabet
  val keys: Seq[Seq[Char]] = for {x <- vocabulary; y <- vocabulary; z <- vocabulary}
    yield Seq(x, y, z)
  keys
}

Or just:

def enumProduct3(vocabulary: Seq[Char]): Seq[Seq[Char]] = for {
  x <- vocabulary; y <- vocabulary; z <- vocabulary
} yield Seq(x, y, z)


Answered By - Guru Stron
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing