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;
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
Pikachu the Parenthesis Wizard
2,17481629
2,17481629
asked May 29 at 11:05
jakstackjakstack
1,05621125
1,05621125
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
Yourcaseexpression is wrong -a::bbindsato the head andbto the tail soa == bis always false since you're comparing aStringto aList[String]. You can replace themap/containswith 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/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
);
);
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.3k860110
20.3k860110
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
1,333519
1,333519
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
Yourcaseexpression is wrong -a::bbindsato the head andbto the tail soa == bis always false since you're comparing aStringto aList[String]. You can replace themap/containswith 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
Yourcaseexpression is wrong -a::bbindsato the head andbto the tail soa == bis always false since you're comparing aStringto aList[String]. You can replace themap/containswith 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.
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
Yourcaseexpression is wrong -a::bbindsato the head andbto the tail soa == bis always false since you're comparing aStringto aList[String]. You can replace themap/containswith 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
Yourcaseexpression is wrong -a::bbindsato the head andbto the tail soa == bis always false since you're comparing aStringto aList[String]. You can replace themap/containswith 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