How to make this Scala method return the same generic as the input?Create Generic method constraining T to an...

Part of my house is inexplicably gone

How to remove the empty page that is placed after the ToC, List of figures and List of tables

Is it true that "only photographers care about noise"?

Is the first of the 10 Commandments considered a mitzvah?

My mom's return ticket is 3 days after I-94 expires

Why didn't all the iron and heavier elements find their way to the center of the accretion disc in the early solar system?

Approach sick days in feedback meeting

Must I use my personal social media account for work?

List of interesting Quantitative Finance podcasts

Is tuition reimbursement a good idea if you have to stay with the job

Print "N NE E SE S SW W NW"

In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?

How do I properly use a function under a class?

In Pandemic, why take the extra step of eradicating a disease after you've cured it?

Can I use 220 V outlets on a 15 ampere breaker and wire it up as 110 V?

How to import .txt file with missing data?

Is it possible to have battery technology that can't be duplicated?

Fully extended TQFT and lattice models

What's the relation between у.е. to USD?

Dedicated bike GPS computer over smartphone

Parsing text written the millitext font

Why did the AvroCar fail to fly above 3 feet?

ISP is not hashing the password I log in with online. Should I take any action?

Is Jesus the last Prophet?



How to make this Scala method return the same generic as the input?


Create Generic method constraining T to an EnumHow do I use reflection to call a generic method?How can I return NULL from a generic method in C#?How do I make the method return type generic?How to create a generic array in Java?How to get the type of T from a member of a generic class or method?Scala: Abstract types vs genericsHow to get a class instance of generics type TUnderstanding Scala's flatmap type conversionshow to make Scala canBuildFrom to build collection type from Seq to Set






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







6















I want to split a string delimited by commas and use the result as either a Seq or a Set:



def splitByComma(commaDelimited: String): Array[String]
= commaDelimited.trim.split(',')

def splitByCommaAsSet(commaDelimited: String): Set[String]
= splitByComma(commaDelimited).toSet

def splitByCommaAsSeq(commaDelimited: String): Seq[String]
= splitByComma(commaDelimited).toSeq

val foods = "sushi,taco,burrito"
val foodSet = splitByCommaAsSet(foods)
// foodSet: scala.collection.immutable.Set[String] = Set(sushi, taco, burrito)
val foodSeq = splitByCommaAsSeq(foods)
// foodSeq: Seq[String] = List(sushi, taco, burrito)


However, this is quite repetitive. Ideally, I would want to have something like genericSplitByComma[Set](foods) that just returns a Set when I pass Set in, and returns a Seq when I pass Seq.










share|improve this question































    6















    I want to split a string delimited by commas and use the result as either a Seq or a Set:



    def splitByComma(commaDelimited: String): Array[String]
    = commaDelimited.trim.split(',')

    def splitByCommaAsSet(commaDelimited: String): Set[String]
    = splitByComma(commaDelimited).toSet

    def splitByCommaAsSeq(commaDelimited: String): Seq[String]
    = splitByComma(commaDelimited).toSeq

    val foods = "sushi,taco,burrito"
    val foodSet = splitByCommaAsSet(foods)
    // foodSet: scala.collection.immutable.Set[String] = Set(sushi, taco, burrito)
    val foodSeq = splitByCommaAsSeq(foods)
    // foodSeq: Seq[String] = List(sushi, taco, burrito)


    However, this is quite repetitive. Ideally, I would want to have something like genericSplitByComma[Set](foods) that just returns a Set when I pass Set in, and returns a Seq when I pass Seq.










    share|improve this question



























      6












      6








      6


      3






      I want to split a string delimited by commas and use the result as either a Seq or a Set:



      def splitByComma(commaDelimited: String): Array[String]
      = commaDelimited.trim.split(',')

      def splitByCommaAsSet(commaDelimited: String): Set[String]
      = splitByComma(commaDelimited).toSet

      def splitByCommaAsSeq(commaDelimited: String): Seq[String]
      = splitByComma(commaDelimited).toSeq

      val foods = "sushi,taco,burrito"
      val foodSet = splitByCommaAsSet(foods)
      // foodSet: scala.collection.immutable.Set[String] = Set(sushi, taco, burrito)
      val foodSeq = splitByCommaAsSeq(foods)
      // foodSeq: Seq[String] = List(sushi, taco, burrito)


      However, this is quite repetitive. Ideally, I would want to have something like genericSplitByComma[Set](foods) that just returns a Set when I pass Set in, and returns a Seq when I pass Seq.










      share|improve this question
















      I want to split a string delimited by commas and use the result as either a Seq or a Set:



      def splitByComma(commaDelimited: String): Array[String]
      = commaDelimited.trim.split(',')

      def splitByCommaAsSet(commaDelimited: String): Set[String]
      = splitByComma(commaDelimited).toSet

      def splitByCommaAsSeq(commaDelimited: String): Seq[String]
      = splitByComma(commaDelimited).toSeq

      val foods = "sushi,taco,burrito"
      val foodSet = splitByCommaAsSet(foods)
      // foodSet: scala.collection.immutable.Set[String] = Set(sushi, taco, burrito)
      val foodSeq = splitByCommaAsSeq(foods)
      // foodSeq: Seq[String] = List(sushi, taco, burrito)


      However, this is quite repetitive. Ideally, I would want to have something like genericSplitByComma[Set](foods) that just returns a Set when I pass Set in, and returns a Seq when I pass Seq.







      scala generics type-conversion polymorphism scala-collections






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago









      jwvh

      30.7k52242




      30.7k52242










      asked 8 hours ago









      yhylordyhylord

      1167




      1167
























          3 Answers
          3






          active

          oldest

          votes


















          8














          @KrzysztofAtłasik's answer works great for Scala 2.12.

          This is a solution for 2.13. (Not completely sure if this is the best way).



          import scala.collection.Factory
          import scala.language.higherKinds

          def splitByComma[C[_]](commaDelimited: String)(implicit f: Factory[String, C[String]]): C[String] =
          f.fromSpecific(commaDelimited.split(","))
          // Or, as Dmytro stated, which I have to agree looks better.
          commaDelimited.split(",").to(f)


          Which you can use like this:



          splitByComma[Array]("hello,world!")
          // res: Array[String] = Array(hello, world!)

          splitByComma[Set]("hello,world!")
          // res: Set[String] = Set(hello, world!)

          splitByComma[List]("hello,world!")
          // res: List[String] = List(hello, world!)





          share|improve this answer





















          • 2





            Or commaDelimited.split(",").to(f).

            – Dmytro Mitin
            8 hours ago






          • 1





            Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

            – som-snytt
            8 hours ago






          • 2





            @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

            – Luis Miguel Mejía Suárez
            8 hours ago



















          8














          There's method in Scala called to which can transform arbitrary collection to another as long as there is typeclass called CanBuildFrom in scope.



          import scala.collection.generic.CanBuildFrom
          import scala.languageFeature.higherKinds

          def genericSplitByComma[S[_]](s: String)(implicit cbf: CanBuildFrom[Nothing, String, S[String]]): S[String] = {
          s.split(",").to[S]
          }

          genericSplitByComma[Set]("Hello, hello") //Set(Hello, hello)
          genericSplitByComma[List]("Hello, hello") //List(Hello, hello)
          genericSplitByComma[Array]("Hello, hello") //Array(hello, world!)


          We don't need to constrain S[_] because this function won't compile if there is no suitable CanBuildFrom in scope. For example, this will fail:



          genericSplitByComma[Option]("Hello, hello")


          Below will also fail because our type constructor S[_] accepts only one type argument and the map expects two:



          genericSplitByComma[Map]("Hello, hello")


          As Luis Miguel Mejía Suárez and Dmytro Mitin noticed, there was major refactor in collections in just-released Scala 2.13, so it will work up to Scala 2.12.






          share|improve this answer





















          • 3





            In 2.13 this changed.

            – Dmytro Mitin
            8 hours ago



















          4














          There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.



          def simpleSplitByComma(coll :Iterable[String]) =
          coll.flatMap(_.trim.split(","))

          simpleSplitByComma(Set("hello,world")) //res0: Set(hello, world)
          simpleSplitByComma(Seq("bellow,world")) //res1: List(bellow, world)
          simpleSplitByComma(Array("fellow,old")) //res2: ArrayBuffer(fellow, old)
          simpleSplitByComma(Stream("end,of,the,world")) //res3: Stream(end, ?)





          share|improve this answer
























          • Now we must wait and see where yhylord places the green check!

            – som-snytt
            7 hours ago











          • I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

            – yhylord
            5 hours ago














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56551015%2fhow-to-make-this-scala-method-return-the-same-generic-as-the-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          8














          @KrzysztofAtłasik's answer works great for Scala 2.12.

          This is a solution for 2.13. (Not completely sure if this is the best way).



          import scala.collection.Factory
          import scala.language.higherKinds

          def splitByComma[C[_]](commaDelimited: String)(implicit f: Factory[String, C[String]]): C[String] =
          f.fromSpecific(commaDelimited.split(","))
          // Or, as Dmytro stated, which I have to agree looks better.
          commaDelimited.split(",").to(f)


          Which you can use like this:



          splitByComma[Array]("hello,world!")
          // res: Array[String] = Array(hello, world!)

          splitByComma[Set]("hello,world!")
          // res: Set[String] = Set(hello, world!)

          splitByComma[List]("hello,world!")
          // res: List[String] = List(hello, world!)





          share|improve this answer





















          • 2





            Or commaDelimited.split(",").to(f).

            – Dmytro Mitin
            8 hours ago






          • 1





            Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

            – som-snytt
            8 hours ago






          • 2





            @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

            – Luis Miguel Mejía Suárez
            8 hours ago
















          8














          @KrzysztofAtłasik's answer works great for Scala 2.12.

          This is a solution for 2.13. (Not completely sure if this is the best way).



          import scala.collection.Factory
          import scala.language.higherKinds

          def splitByComma[C[_]](commaDelimited: String)(implicit f: Factory[String, C[String]]): C[String] =
          f.fromSpecific(commaDelimited.split(","))
          // Or, as Dmytro stated, which I have to agree looks better.
          commaDelimited.split(",").to(f)


          Which you can use like this:



          splitByComma[Array]("hello,world!")
          // res: Array[String] = Array(hello, world!)

          splitByComma[Set]("hello,world!")
          // res: Set[String] = Set(hello, world!)

          splitByComma[List]("hello,world!")
          // res: List[String] = List(hello, world!)





          share|improve this answer





















          • 2





            Or commaDelimited.split(",").to(f).

            – Dmytro Mitin
            8 hours ago






          • 1





            Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

            – som-snytt
            8 hours ago






          • 2





            @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

            – Luis Miguel Mejía Suárez
            8 hours ago














          8












          8








          8







          @KrzysztofAtłasik's answer works great for Scala 2.12.

          This is a solution for 2.13. (Not completely sure if this is the best way).



          import scala.collection.Factory
          import scala.language.higherKinds

          def splitByComma[C[_]](commaDelimited: String)(implicit f: Factory[String, C[String]]): C[String] =
          f.fromSpecific(commaDelimited.split(","))
          // Or, as Dmytro stated, which I have to agree looks better.
          commaDelimited.split(",").to(f)


          Which you can use like this:



          splitByComma[Array]("hello,world!")
          // res: Array[String] = Array(hello, world!)

          splitByComma[Set]("hello,world!")
          // res: Set[String] = Set(hello, world!)

          splitByComma[List]("hello,world!")
          // res: List[String] = List(hello, world!)





          share|improve this answer















          @KrzysztofAtłasik's answer works great for Scala 2.12.

          This is a solution for 2.13. (Not completely sure if this is the best way).



          import scala.collection.Factory
          import scala.language.higherKinds

          def splitByComma[C[_]](commaDelimited: String)(implicit f: Factory[String, C[String]]): C[String] =
          f.fromSpecific(commaDelimited.split(","))
          // Or, as Dmytro stated, which I have to agree looks better.
          commaDelimited.split(",").to(f)


          Which you can use like this:



          splitByComma[Array]("hello,world!")
          // res: Array[String] = Array(hello, world!)

          splitByComma[Set]("hello,world!")
          // res: Set[String] = Set(hello, world!)

          splitByComma[List]("hello,world!")
          // res: List[String] = List(hello, world!)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 8 hours ago









          Luis Miguel Mejía SuárezLuis Miguel Mejía Suárez

          3,83121123




          3,83121123








          • 2





            Or commaDelimited.split(",").to(f).

            – Dmytro Mitin
            8 hours ago






          • 1





            Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

            – som-snytt
            8 hours ago






          • 2





            @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

            – Luis Miguel Mejía Suárez
            8 hours ago














          • 2





            Or commaDelimited.split(",").to(f).

            – Dmytro Mitin
            8 hours ago






          • 1





            Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

            – som-snytt
            8 hours ago






          • 2





            @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

            – Luis Miguel Mejía Suárez
            8 hours ago








          2




          2





          Or commaDelimited.split(",").to(f).

          – Dmytro Mitin
          8 hours ago





          Or commaDelimited.split(",").to(f).

          – Dmytro Mitin
          8 hours ago




          1




          1





          Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

          – som-snytt
          8 hours ago





          Happy 2.13! I also wondered. def splitByComma[C](s: String)(implicit f: Factory[String, C]): C = s.split(",").to(f)

          – som-snytt
          8 hours ago




          2




          2





          @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

          – Luis Miguel Mejía Suárez
          8 hours ago





          @DmytroMitin Oh, so that was the change of to[List] to to(List), because it now receives a factory an every companion object is now a factory, I finally get it :D

          – Luis Miguel Mejía Suárez
          8 hours ago













          8














          There's method in Scala called to which can transform arbitrary collection to another as long as there is typeclass called CanBuildFrom in scope.



          import scala.collection.generic.CanBuildFrom
          import scala.languageFeature.higherKinds

          def genericSplitByComma[S[_]](s: String)(implicit cbf: CanBuildFrom[Nothing, String, S[String]]): S[String] = {
          s.split(",").to[S]
          }

          genericSplitByComma[Set]("Hello, hello") //Set(Hello, hello)
          genericSplitByComma[List]("Hello, hello") //List(Hello, hello)
          genericSplitByComma[Array]("Hello, hello") //Array(hello, world!)


          We don't need to constrain S[_] because this function won't compile if there is no suitable CanBuildFrom in scope. For example, this will fail:



          genericSplitByComma[Option]("Hello, hello")


          Below will also fail because our type constructor S[_] accepts only one type argument and the map expects two:



          genericSplitByComma[Map]("Hello, hello")


          As Luis Miguel Mejía Suárez and Dmytro Mitin noticed, there was major refactor in collections in just-released Scala 2.13, so it will work up to Scala 2.12.






          share|improve this answer





















          • 3





            In 2.13 this changed.

            – Dmytro Mitin
            8 hours ago
















          8














          There's method in Scala called to which can transform arbitrary collection to another as long as there is typeclass called CanBuildFrom in scope.



          import scala.collection.generic.CanBuildFrom
          import scala.languageFeature.higherKinds

          def genericSplitByComma[S[_]](s: String)(implicit cbf: CanBuildFrom[Nothing, String, S[String]]): S[String] = {
          s.split(",").to[S]
          }

          genericSplitByComma[Set]("Hello, hello") //Set(Hello, hello)
          genericSplitByComma[List]("Hello, hello") //List(Hello, hello)
          genericSplitByComma[Array]("Hello, hello") //Array(hello, world!)


          We don't need to constrain S[_] because this function won't compile if there is no suitable CanBuildFrom in scope. For example, this will fail:



          genericSplitByComma[Option]("Hello, hello")


          Below will also fail because our type constructor S[_] accepts only one type argument and the map expects two:



          genericSplitByComma[Map]("Hello, hello")


          As Luis Miguel Mejía Suárez and Dmytro Mitin noticed, there was major refactor in collections in just-released Scala 2.13, so it will work up to Scala 2.12.






          share|improve this answer





















          • 3





            In 2.13 this changed.

            – Dmytro Mitin
            8 hours ago














          8












          8








          8







          There's method in Scala called to which can transform arbitrary collection to another as long as there is typeclass called CanBuildFrom in scope.



          import scala.collection.generic.CanBuildFrom
          import scala.languageFeature.higherKinds

          def genericSplitByComma[S[_]](s: String)(implicit cbf: CanBuildFrom[Nothing, String, S[String]]): S[String] = {
          s.split(",").to[S]
          }

          genericSplitByComma[Set]("Hello, hello") //Set(Hello, hello)
          genericSplitByComma[List]("Hello, hello") //List(Hello, hello)
          genericSplitByComma[Array]("Hello, hello") //Array(hello, world!)


          We don't need to constrain S[_] because this function won't compile if there is no suitable CanBuildFrom in scope. For example, this will fail:



          genericSplitByComma[Option]("Hello, hello")


          Below will also fail because our type constructor S[_] accepts only one type argument and the map expects two:



          genericSplitByComma[Map]("Hello, hello")


          As Luis Miguel Mejía Suárez and Dmytro Mitin noticed, there was major refactor in collections in just-released Scala 2.13, so it will work up to Scala 2.12.






          share|improve this answer















          There's method in Scala called to which can transform arbitrary collection to another as long as there is typeclass called CanBuildFrom in scope.



          import scala.collection.generic.CanBuildFrom
          import scala.languageFeature.higherKinds

          def genericSplitByComma[S[_]](s: String)(implicit cbf: CanBuildFrom[Nothing, String, S[String]]): S[String] = {
          s.split(",").to[S]
          }

          genericSplitByComma[Set]("Hello, hello") //Set(Hello, hello)
          genericSplitByComma[List]("Hello, hello") //List(Hello, hello)
          genericSplitByComma[Array]("Hello, hello") //Array(hello, world!)


          We don't need to constrain S[_] because this function won't compile if there is no suitable CanBuildFrom in scope. For example, this will fail:



          genericSplitByComma[Option]("Hello, hello")


          Below will also fail because our type constructor S[_] accepts only one type argument and the map expects two:



          genericSplitByComma[Map]("Hello, hello")


          As Luis Miguel Mejía Suárez and Dmytro Mitin noticed, there was major refactor in collections in just-released Scala 2.13, so it will work up to Scala 2.12.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 8 hours ago

























          answered 8 hours ago









          Krzysztof AtłasikKrzysztof Atłasik

          8,82221442




          8,82221442








          • 3





            In 2.13 this changed.

            – Dmytro Mitin
            8 hours ago














          • 3





            In 2.13 this changed.

            – Dmytro Mitin
            8 hours ago








          3




          3





          In 2.13 this changed.

          – Dmytro Mitin
          8 hours ago





          In 2.13 this changed.

          – Dmytro Mitin
          8 hours ago











          4














          There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.



          def simpleSplitByComma(coll :Iterable[String]) =
          coll.flatMap(_.trim.split(","))

          simpleSplitByComma(Set("hello,world")) //res0: Set(hello, world)
          simpleSplitByComma(Seq("bellow,world")) //res1: List(bellow, world)
          simpleSplitByComma(Array("fellow,old")) //res2: ArrayBuffer(fellow, old)
          simpleSplitByComma(Stream("end,of,the,world")) //res3: Stream(end, ?)





          share|improve this answer
























          • Now we must wait and see where yhylord places the green check!

            – som-snytt
            7 hours ago











          • I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

            – yhylord
            5 hours ago


















          4














          There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.



          def simpleSplitByComma(coll :Iterable[String]) =
          coll.flatMap(_.trim.split(","))

          simpleSplitByComma(Set("hello,world")) //res0: Set(hello, world)
          simpleSplitByComma(Seq("bellow,world")) //res1: List(bellow, world)
          simpleSplitByComma(Array("fellow,old")) //res2: ArrayBuffer(fellow, old)
          simpleSplitByComma(Stream("end,of,the,world")) //res3: Stream(end, ?)





          share|improve this answer
























          • Now we must wait and see where yhylord places the green check!

            – som-snytt
            7 hours ago











          • I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

            – yhylord
            5 hours ago
















          4












          4








          4







          There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.



          def simpleSplitByComma(coll :Iterable[String]) =
          coll.flatMap(_.trim.split(","))

          simpleSplitByComma(Set("hello,world")) //res0: Set(hello, world)
          simpleSplitByComma(Seq("bellow,world")) //res1: List(bellow, world)
          simpleSplitByComma(Array("fellow,old")) //res2: ArrayBuffer(fellow, old)
          simpleSplitByComma(Stream("end,of,the,world")) //res3: Stream(end, ?)





          share|improve this answer













          There's a simple workaround for this. Not exactly the requested syntax but just as concise and it should be 2.13 compatible.



          def simpleSplitByComma(coll :Iterable[String]) =
          coll.flatMap(_.trim.split(","))

          simpleSplitByComma(Set("hello,world")) //res0: Set(hello, world)
          simpleSplitByComma(Seq("bellow,world")) //res1: List(bellow, world)
          simpleSplitByComma(Array("fellow,old")) //res2: ArrayBuffer(fellow, old)
          simpleSplitByComma(Stream("end,of,the,world")) //res3: Stream(end, ?)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 8 hours ago









          jwvhjwvh

          30.7k52242




          30.7k52242













          • Now we must wait and see where yhylord places the green check!

            – som-snytt
            7 hours ago











          • I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

            – yhylord
            5 hours ago





















          • Now we must wait and see where yhylord places the green check!

            – som-snytt
            7 hours ago











          • I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

            – yhylord
            5 hours ago



















          Now we must wait and see where yhylord places the green check!

          – som-snytt
          7 hours ago





          Now we must wait and see where yhylord places the green check!

          – som-snytt
          7 hours ago













          I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

          – yhylord
          5 hours ago







          I really like this one and will probably use something like this, but I feel like I have to accept another answer that exactly matches what I asked :)

          – yhylord
          5 hours ago




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56551015%2fhow-to-make-this-scala-method-return-the-same-generic-as-the-input%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

          Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...