def exists(p: Char => Boolean): Boolean
def find(p: Char => Boolean): Option[Char]
I am not sure about the arguments declaration above. can you help give an example?
Thanks.
def exists(p: Char => Boolean): Boolean
def find(p: Char => Boolean): Option[Char]
I am not sure about the arguments declaration above. can you help give an example?
Thanks.
BTW, I want to achieve this purpose:
scala> implicit class foo(s:String) {
| def grep(x:String):Boolean = {
| val pat = x.r.unanchored
| if (pat.matches(s)) true else false
| }
| }
class foo
scala> "hello".grep("lo")
val res10: Boolean = true
but I don’t know what’s the usage of “find” and “exists”. So I am asking here.
As their signature shows, find
and exists
work on the Char
level, they can be used to check if any single character in the string fulfills a condition.
For your purpose, there isn’t a method doing exactly the same. String has a matches
method, that takes a regex string, but it isn’t interpreted as unanchored.
As your example, I would rather use contains
here.
scala> "hello".contains("lo")
val res3: Boolean = true
find
and exists
is applied to something like Seq
or List
.
scala> List(2,3,4).find(_ == 3)
val res5: Option[Int] = Some(3)
scala> List(2,3,4).find(_ == 55)
val res6: Option[Int] = None
scala> List(2,3,4).exists(_ == 4)
val res7: Boolean = true
scala> List(2,3,4).exists(_ == 555)
val res8: Boolean = false
i know contains method but which is not I am looking for.
scala> "data2world".grep("""\d""")
val res0: Boolean = true
String has also find() and exists() methods. Ok I know their usage now.
scala> "hello".exists(_ == 'o')
val res4: Boolean = true
scala> "hello".find(_ == 'o')
val res5: Option[Char] = Some(o)