Skip to content

Instantly share code, notes, and snippets.

@duanebester
Last active August 29, 2015 14:19
Show Gist options
  • Save duanebester/9f4fa004434ec93e54e5 to your computer and use it in GitHub Desktop.
Save duanebester/9f4fa004434ec93e54e5 to your computer and use it in GitHub Desktop.
Indexer Fun
class Indexer(sentences : List[String]) {
// Let's pre-process the words for thousands of calls
val sentenceList = sentences.map(words(_))
def find(key: String) {
for {
sentence <- sentenceList
count = sentence.count{ _.equalsIgnoreCase(key) } if count > 0
debug = sentence.mkString(" ")
} {
println(s"""${debug} ($count)""")
//println(debug + " (" + count + ")")
}
}
}
// Test words function
def words(sentence: String): List[String] = sentence.split(" ").toList
// ~-- Initial thoughts --
// def find(key: String) = {
// for(sentence <- sentences) {
// val x = words(sentence).filter(_.equalsIgnoreCase(key)).length
// if(x > 0) println(sentence + " (" + x + ")")
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment