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

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

French citizen, did I need a visa in 2004 and 2006 when I visited the US as a child?

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

Nth term of Van Eck Sequence

Why is Skinner so awkward in Hot Fuzz?

Can Dive Down protect a creature against Pacifism?

Is there a term for when fiction refers to fiction

Print the phrase "And she said, 'But that's his.'" using only the alphabet

Manager wants to hire me; HR does not. How to proceed?

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

Why are backslashes included in this shell script?

What is the color associated with lukewarm?

What's the reason for the decade jump in the recent X-Men trilogy?

How can I find out about the game world without meta-influencing it?

Are athletes' college degrees discounted by employers and graduate school admissions?

Is this equation correct? And if so, is this famous?

Does every chapter have to "blow the reader away" so to speak?

Why do the “Shtei HaLechem” not play a prominent part in the davenning for Shavuos?

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

Dedicated bike GPS computer over smartphone

Background for black and white chart

New Site Design!

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

What do I need to do, tax-wise, for a sudden windfall?



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 height:90px;width:728px;box-sizing:border-box;








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









      Pikachu the Parenthesis Wizard

      2,17481629




      2,17481629










      asked May 29 at 11:05









      jakstackjakstack

      1,05621125




      1,05621125






















          3 Answers
          3






          active

          oldest

          votes


















          16














          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






























            7














            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/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%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









              16














              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



























                16














                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

























                  16












                  16








                  16







                  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.3k860110




                  20.3k860110























                      7














                      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





























                        7














                        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



























                          7












                          7








                          7







                          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

                          1,333519




                          1,333519





















                              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.

                              5161313




                              5161313







                              • 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

                              How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

                              What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

                              Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?