I hope it is okay if I post solved problems and ask for a more beautiful solution, so that I can see how it is done correctly
I wanted a list of all process names mapped with a ProcessID. Something like "notepad.exe" -> 4242
Ofc there can be multiple instances so it should be something like "notepad.exe" -> List(4242,7171)
I have a method which gives me a tuple
private def extractProcess(s: String): (String, Int) = {
val process = s.split(" ").filterNot(str => str == "")
(process(0), process(1).toInt)
}
output would be ("Notepad.exe",4242)
processList containt the raw string from windows tasklist which looks something like this
svchost.exe 4464 Services 0 47.656 K
Now I want to create a map with all processes and I have done it like this
val process: Map[String, List[Int]] = Map()
processList.drop(5).map(s => {
val element = extractProcess(s)
if (process contains element._1) {
val p = process get element._1
process(element._1) = p.get ::: List(element._2)
} else {
process(element._1) = List(element._2)
}
})
I droped the first 5 elements because they are not needed
Now the output would look like this
...
(tasklist.exe,List(5036))
(NLClientApp.exe,List(2812))
(wininit.exe,List(444))
(SearchFilterHost.exe,List(5476))
(svchost.exe,List(656, 732, 928, 964, 992, 1036, 1140, 1360, 2168, 4464, 4764, 5048))
...
Which is what I wanted to do.
Are there better ways of creating the map?
I always think more imperative than functional, it is hard to think different.
Could be made simpler like this: