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

What is the source of 'Ma'alin bekodesh'?

Changing the PK column of a data extension without completely recreating it

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

Do Veracrypt encrypted volumes have any kind of brute force protection?

Is there a radar system monitoring the UK mainland border?

A life of PhD: is it feasible?

Identification: what type of connector does the pictured socket take?

Is it good practice to create tables dynamically?

What is the theme of analysis?

Was the Lonely Mountain, where Smaug lived, a volcano?

How to represent jealousy in a cute way?

Is it a good security practice to force employees hide their employer to avoid being targeted?

Table with varying step

Why is my Taiyaki (Cake that looks like a fish) too hard and dry?

Why does there seem to be an extreme lack of public trashcans in Taiwan?

Can I attach a DC blower to intake manifold of my 150CC Yamaha FZS FI engine?

Why is it bad to use your whole foot in rock climbing

How can religions without a hell discourage evil-doing?

Keeping track of theme when improvising

Does WiFi affect the quality of images downloaded from the internet?

What do you call the action of "describing events as they happen" like sports anchors do?

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

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

Can you open the door or die? v2



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
            7 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
            7 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
            7 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
            4 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
            7 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
            7 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
            7 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
            7 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
            7 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
            7 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 7 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
            7 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
            7 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
            7 hours ago












          • 2





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

            – Dmytro Mitin
            7 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
            7 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
            7 hours ago







          2




          2





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

          – Dmytro Mitin
          7 hours ago





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

          – Dmytro Mitin
          7 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
          7 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
          7 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
          7 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
          7 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 7 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
            4 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
            4 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 7 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
            4 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
            4 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
          4 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
          4 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

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її