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;
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
add a comment
|
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
add a comment
|
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
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
scala functional-programming
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
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
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
add a comment
|
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
add a comment
|
val listA = List("Black","Black","Green","White")
listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)
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
Yourcase
expression is wrong -a::b
bindsa
to the head andb
to the tail soa == b
is always false since you're comparing aString
to aList[String]
. You can replace themap/contains
with exists i.e.listA.sliding(2).exists case a::b::_ => a == b
.
– Lee
May 29 at 11:33
add a comment
|
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment
|
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
add a comment
|
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
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
answered May 29 at 11:15
ziggystarziggystar
20.7k8 gold badges60 silver badges112 bronze badges
20.7k8 gold badges60 silver badges112 bronze badges
add a comment
|
add a comment
|
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
add a comment
|
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
add a comment
|
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
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
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
add a comment
|
add a comment
|
val listA = List("Black","Black","Green","White")
listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)
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
Yourcase
expression is wrong -a::b
bindsa
to the head andb
to the tail soa == b
is always false since you're comparing aString
to aList[String]
. You can replace themap/contains
with exists i.e.listA.sliding(2).exists case a::b::_ => a == b
.
– Lee
May 29 at 11:33
add a comment
|
val listA = List("Black","Black","Green","White")
listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)
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
Yourcase
expression is wrong -a::b
bindsa
to the head andb
to the tail soa == b
is always false since you're comparing aString
to aList[String]
. You can replace themap/contains
with exists i.e.listA.sliding(2).exists case a::b::_ => a == b
.
– Lee
May 29 at 11:33
add a comment
|
val listA = List("Black","Black","Green","White")
listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)
val listA = List("Black","Black","Green","White")
listA.sliding(2).mapcase a::b::_ if a == b => true else false.contains(true)
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
Yourcase
expression is wrong -a::b
bindsa
to the head andb
to the tail soa == b
is always false since you're comparing aString
to aList[String]
. You can replace themap/contains
with exists i.e.listA.sliding(2).exists case a::b::_ => a == b
.
– Lee
May 29 at 11:33
add a comment
|
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
Yourcase
expression is wrong -a::b
bindsa
to the head andb
to the tail soa == b
is always false since you're comparing aString
to aList[String]
. You can replace themap/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
add a comment
|
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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