0

I have the following structure:

val s1 = Seq(1,2,3,4,5)
val s2 = Seq()
val s3 = Seq(6,7,8,9)
val seq = Seq(s1,s2,s3)

What I need is to validate that all the sequences in seq have at least one element. I tried to accomplish this with filter, but couldn't, any ideas?

0

4 Answers 4

1

You're probably looking for the forall function:

seq.forall(!_.isEmpty)

which translates into: Are all sequences in seq non-empty? and thus returns false with your example since s2 is empty.

Sign up to request clarification or add additional context in comments.

1 Comment

Or seq.forall(_.nonEmpty).
1

You can use below line of code.

val distinct=seq.filter(_.length>0)

Comments

1

This finds all the sequences with at least one element

seq.filterNot(_.isEmpty)

while this checks that all sequences have at least one element

seq.forAll(!_.isEmpty)

or

!seq.exists(_.isEmpty)

Comments

0

This should also work.

seq.filter(_.nonEmpty)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.