Scala list with same adjacent valuesDoes functional programming replace GoF design patterns?Scala vs. Groovy vs. ClojureIs the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in Scalalist comprehension vs. lambda + filterLarge-scale design in Haskell?Scala list concatenation, ::: vs ++How can a time function exist in functional programming?What are all the uses of an underscore in Scala?Scala udf check if df column value is in a list

Why are Buddhist concepts so difficult?

How are astronauts in the ISS protected from electric shock?

Coworkers accusing me of "cheating" for working more efficiently

Is there a high level reason why the inverse square law of gravitation yields periodic orbits without precession?

Can I create a one way symlink?

My cat gets angry and scared at me if I stand

A word/phrase means "a small amount" (of a color)

For the Single Entry Schengen visa, do the microstates (Monaco, San Marino and the Vatican City) count?

What elements would be created in a star composed entirely of gold?

Why do the new Star Trek series have so few episodes in each season?

Logical all/any in bash

What does "crank old Sabbath" refer to?

Functions where the sum of its partial derivatives is zero

Siginificant speed difference of Integrate between 9.0 and 12.0

You see a boat filled with people

Why is Elastic Net called Elastic Net?

How can I increase the rate of regeneration in humans without the possibility of tumors developing?

Length-terminated sequences

Does a troll die if its maximum hit points is zero?

Would the minimum payment or full CC amount be considered monthly debt?

Weakness of double encryption

Are these 2 resistors in parallel?

LeetCode 65: Valid Number (Python)

How do I get softer pictures in sunlight, like in this commercial?



Scala list with same adjacent values


Does functional programming replace GoF design patterns?Scala vs. Groovy vs. ClojureIs the Scala 2.8 collections library a case of “the longest suicide note in history”?Difference between object and class in Scalalist comprehension vs. lambda + filterLarge-scale design in Haskell?Scala list concatenation, ::: vs ++How can a time function exist in functional programming?What are all the uses of an underscore in Scala?Scala udf check if df column value is in a list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









12

















I have this list:



List("Black","Black","Green","White")


How can I check if a list has two adjacent values which are the same? Like so:



List("Black","Black","Green","White") true

List("Black","Yellow","Green","White") false

List("Black","Yellow","Black","Yellow") false









share|improve this question


































    12

















    I have this list:



    List("Black","Black","Green","White")


    How can I check if a list has two adjacent values which are the same? Like so:



    List("Black","Black","Green","White") true

    List("Black","Yellow","Green","White") false

    List("Black","Yellow","Black","Yellow") false









    share|improve this question






























      12












      12








      12


      1






      I have this list:



      List("Black","Black","Green","White")


      How can I check if a list has two adjacent values which are the same? Like so:



      List("Black","Black","Green","White") true

      List("Black","Yellow","Green","White") false

      List("Black","Yellow","Black","Yellow") false









      share|improve this question

















      I have this list:



      List("Black","Black","Green","White")


      How can I check if a list has two adjacent values which are the same? Like so:



      List("Black","Black","Green","White") true

      List("Black","Yellow","Green","White") false

      List("Black","Yellow","Black","Yellow") false






      scala functional-programming






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 29 at 20:00









      Pika the Wizard of the Whales

      2,2168 gold badges18 silver badges30 bronze badges




      2,2168 gold badges18 silver badges30 bronze badges










      asked May 29 at 11:05









      jakstackjakstack

      1,1592 gold badges11 silver badges26 bronze badges




      1,1592 gold badges11 silver badges26 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          18


















          In addition to Valy Dia's solution, you can also write:



          list.sliding(2).exists(_.distinct.size == 1)


          REPL Session



          scala> def check[A](l: Seq[A]): Boolean = l.sliding(2).exists(_.distinct.size == 1)
          check: [A](l: Seq[A])Boolean

          scala> check("A" :: "B" :: Nil)
          res0: Boolean = false

          scala> check("A" :: "B" :: "B" ::Nil)
          res1: Boolean = true

          scala> check("A" :: "B" :: "C":: "B" ::Nil)
          res2: Boolean = false





          share|improve this answer

































            8


















            You can try:



            def check[A](l: List[A]): Boolean = 
            l.zip(l.tail).exists case (x,y) => x == y

            check(List("Black","Black","Green","White"))
            //res5: Boolean = true

            check(List("Black","Yellow","Green","White"))
            //res6: Boolean = false

            check(List("Black","Yellow","Black","Yellow"))
            //res7: Boolean = false





            share|improve this answer



































              4


















              val listA = List("Black","Black","Green","White")

              listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)





              share|improve this answer























              • 1





                I don´t think that is what he asked, "two adjacent values"

                – Pedro Correia Luís
                May 29 at 11:10











              • updated the question with another example at the end

                – jakstack
                May 29 at 11:10






              • 2





                Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                – Lee
                May 29 at 11:33













              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/4.0/"u003ecc by-sa 4.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%2f56359114%2fscala-list-with-same-adjacent-values%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









              18


















              In addition to Valy Dia's solution, you can also write:



              list.sliding(2).exists(_.distinct.size == 1)


              REPL Session



              scala> def check[A](l: Seq[A]): Boolean = l.sliding(2).exists(_.distinct.size == 1)
              check: [A](l: Seq[A])Boolean

              scala> check("A" :: "B" :: Nil)
              res0: Boolean = false

              scala> check("A" :: "B" :: "B" ::Nil)
              res1: Boolean = true

              scala> check("A" :: "B" :: "C":: "B" ::Nil)
              res2: Boolean = false





              share|improve this answer






























                18


















                In addition to Valy Dia's solution, you can also write:



                list.sliding(2).exists(_.distinct.size == 1)


                REPL Session



                scala> def check[A](l: Seq[A]): Boolean = l.sliding(2).exists(_.distinct.size == 1)
                check: [A](l: Seq[A])Boolean

                scala> check("A" :: "B" :: Nil)
                res0: Boolean = false

                scala> check("A" :: "B" :: "B" ::Nil)
                res1: Boolean = true

                scala> check("A" :: "B" :: "C":: "B" ::Nil)
                res2: Boolean = false





                share|improve this answer




























                  18














                  18










                  18









                  In addition to Valy Dia's solution, you can also write:



                  list.sliding(2).exists(_.distinct.size == 1)


                  REPL Session



                  scala> def check[A](l: Seq[A]): Boolean = l.sliding(2).exists(_.distinct.size == 1)
                  check: [A](l: Seq[A])Boolean

                  scala> check("A" :: "B" :: Nil)
                  res0: Boolean = false

                  scala> check("A" :: "B" :: "B" ::Nil)
                  res1: Boolean = true

                  scala> check("A" :: "B" :: "C":: "B" ::Nil)
                  res2: Boolean = false





                  share|improve this answer














                  In addition to Valy Dia's solution, you can also write:



                  list.sliding(2).exists(_.distinct.size == 1)


                  REPL Session



                  scala> def check[A](l: Seq[A]): Boolean = l.sliding(2).exists(_.distinct.size == 1)
                  check: [A](l: Seq[A])Boolean

                  scala> check("A" :: "B" :: Nil)
                  res0: Boolean = false

                  scala> check("A" :: "B" :: "B" ::Nil)
                  res1: Boolean = true

                  scala> check("A" :: "B" :: "C":: "B" ::Nil)
                  res2: Boolean = false






                  share|improve this answer













                  share|improve this answer




                  share|improve this answer










                  answered May 29 at 11:15









                  ziggystarziggystar

                  20.7k8 gold badges60 silver badges112 bronze badges




                  20.7k8 gold badges60 silver badges112 bronze badges


























                      8


















                      You can try:



                      def check[A](l: List[A]): Boolean = 
                      l.zip(l.tail).exists case (x,y) => x == y

                      check(List("Black","Black","Green","White"))
                      //res5: Boolean = true

                      check(List("Black","Yellow","Green","White"))
                      //res6: Boolean = false

                      check(List("Black","Yellow","Black","Yellow"))
                      //res7: Boolean = false





                      share|improve this answer
































                        8


















                        You can try:



                        def check[A](l: List[A]): Boolean = 
                        l.zip(l.tail).exists case (x,y) => x == y

                        check(List("Black","Black","Green","White"))
                        //res5: Boolean = true

                        check(List("Black","Yellow","Green","White"))
                        //res6: Boolean = false

                        check(List("Black","Yellow","Black","Yellow"))
                        //res7: Boolean = false





                        share|improve this answer






























                          8














                          8










                          8









                          You can try:



                          def check[A](l: List[A]): Boolean = 
                          l.zip(l.tail).exists case (x,y) => x == y

                          check(List("Black","Black","Green","White"))
                          //res5: Boolean = true

                          check(List("Black","Yellow","Green","White"))
                          //res6: Boolean = false

                          check(List("Black","Yellow","Black","Yellow"))
                          //res7: Boolean = false





                          share|improve this answer
















                          You can try:



                          def check[A](l: List[A]): Boolean = 
                          l.zip(l.tail).exists case (x,y) => x == y

                          check(List("Black","Black","Green","White"))
                          //res5: Boolean = true

                          check(List("Black","Yellow","Green","White"))
                          //res6: Boolean = false

                          check(List("Black","Yellow","Black","Yellow"))
                          //res7: Boolean = false






                          share|improve this answer















                          share|improve this answer




                          share|improve this answer








                          edited May 29 at 11:22

























                          answered May 29 at 11:12









                          Valy DiaValy Dia

                          2,1612 gold badges7 silver badges24 bronze badges




                          2,1612 gold badges7 silver badges24 bronze badges
























                              4


















                              val listA = List("Black","Black","Green","White")

                              listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)





                              share|improve this answer























                              • 1





                                I don´t think that is what he asked, "two adjacent values"

                                – Pedro Correia Luís
                                May 29 at 11:10











                              • updated the question with another example at the end

                                – jakstack
                                May 29 at 11:10






                              • 2





                                Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                                – Lee
                                May 29 at 11:33
















                              4


















                              val listA = List("Black","Black","Green","White")

                              listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)





                              share|improve this answer























                              • 1





                                I don´t think that is what he asked, "two adjacent values"

                                – Pedro Correia Luís
                                May 29 at 11:10











                              • updated the question with another example at the end

                                – jakstack
                                May 29 at 11:10






                              • 2





                                Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                                – Lee
                                May 29 at 11:33














                              4














                              4










                              4









                              val listA = List("Black","Black","Green","White")

                              listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)





                              share|improve this answer
















                              val listA = List("Black","Black","Green","White")

                              listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)






                              share|improve this answer















                              share|improve this answer




                              share|improve this answer








                              edited May 29 at 11:34

























                              answered May 29 at 11:07









                              Dionysis Nt.Dionysis Nt.

                              7451 gold badge4 silver badges13 bronze badges




                              7451 gold badge4 silver badges13 bronze badges










                              • 1





                                I don´t think that is what he asked, "two adjacent values"

                                – Pedro Correia Luís
                                May 29 at 11:10











                              • updated the question with another example at the end

                                – jakstack
                                May 29 at 11:10






                              • 2





                                Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                                – Lee
                                May 29 at 11:33













                              • 1





                                I don´t think that is what he asked, "two adjacent values"

                                – Pedro Correia Luís
                                May 29 at 11:10











                              • updated the question with another example at the end

                                – jakstack
                                May 29 at 11:10






                              • 2





                                Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                                – Lee
                                May 29 at 11:33








                              1




                              1





                              I don´t think that is what he asked, "two adjacent values"

                              – Pedro Correia Luís
                              May 29 at 11:10





                              I don´t think that is what he asked, "two adjacent values"

                              – Pedro Correia Luís
                              May 29 at 11:10













                              updated the question with another example at the end

                              – jakstack
                              May 29 at 11:10





                              updated the question with another example at the end

                              – jakstack
                              May 29 at 11:10




                              2




                              2





                              Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                              – Lee
                              May 29 at 11:33






                              Your case expression is wrong - a::b binds a to the head and b to the tail so a == b is always false since you're comparing a String to a List[String]. You can replace the map/contains with exists i.e. listA.sliding(2).exists case a::b::_ => a == b .

                              – Lee
                              May 29 at 11:33



















                              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%2f56359114%2fscala-list-with-same-adjacent-values%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

                              Tamil (spriik) Luke uk diar | Nawigatjuun

                              Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                              Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?