Skip to content

Instantly share code, notes, and snippets.

@developer-sdk
Created March 4, 2019 14:35
Show Gist options
  • Select an option

  • Save developer-sdk/807caf5022762ce094887341b3967876 to your computer and use it in GitHub Desktop.

Select an option

Save developer-sdk/807caf5022762ce094887341b3967876 to your computer and use it in GitHub Desktop.
scala index of
object FuncThinking1 extends App {
def firstIndexOfAny(input: String, searchChars: Seq[Char]): Option[Int] = {
def indexedInput = (0 until input.length).zip(input)
val result = for (pair <- indexedInput; char <- searchChars; if (char == pair._2))
yield (pair._1)
if (result.isEmpty)
None
else
Some(result.head)
}
val result1 = firstIndexOfAny("123445asldkfjasdf", "a"))
println(result1)
def indexOfAny(input:String, searchChars:Seq[Char]):Seq[Int] = {
def indexedinput = (0 until input.length).zip(input)
for(pair <- indexedinput; char <- searchChars; if(char == pair._2))
yield pair._1
}
val result2 = indexOfAny("123445asldkfjasdf", "35")
println(result2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment