I'm trying to work with these methods with no success and i`ll be happy if someone can help me.
I'm using groovy and i have 2 maps of strings.
I want to match between the strings of the 2 maps with threads (using by gpars)
For example :
def firstMap = ["a":"A", "b":"B"]
def secondMap = ["c":"C", "a":A"]
The normal way to equale between the maps is to
fistMap.findAll().each { first ->
secondMap.findAll.each { second ->
if (first.key.equals(second.key) && (first.value.equlas(second.value))
//saveItIntoArray
}
}
I want to do it with gpars thread so i tried :
withPool(2) {
runForkJoin(firstMap) { task ->
task.each {
secondMap.each {
//equals
}
forChild(?)
}
}
}
I kind of new with this and i really don't know how to make it work.
I will appreciate any help.
Thanks,
Or.
What I'd suggest is using parallel collections:
def firstMap = ["a":"A", "b":"B"]
def secondMap = ["c":"C", "a":"A"].asImmutable()
withPool{
println firstMap.findAllParallel { fk, fv -> secondMap.findResult { sk, sv -> fk == sk && fv == sv ? [(fk):fv] : null } }
}
Related
I have a local cache where I store the runner's lap info, I need to show if the runner's current lap was better or worse than the current lap, while displaying the current lap information.
data class RunInfo(
val runnerId: String,
val lapTime: Double,
var betterThanLastLap: BETTERTHANLASTLAP
)
enum class BETTERTHANLASTLAP {
NA, YES, NO
}
object RunDB {
private var listOfRunners: MutableList<RunInfo> =
java.util.Collections.synchronizedList(mutableListOf())
private var previousList: MutableList<RunInfo> = mutableListOf()
fun save(runList: MutableList<RunInfo>) {
previousList = listOfRunners.toMutableList()
listOfRunners.clear()
listOfRunners.addAll(runList)
listOfRunners.forEach { runner ->
previousList.forEach { previousLap ->
if (runner.runnerId == previousLap.runnerId) {
runner.betterThanLastLap =
when {
previousLap.lapTime == 0.0 -> BETTERTHANLASTLAP.NA
runner.lapTime >= previousLap.lapTime -> BETTERTHANLASTLAP.YES
else -> BETTERTHANLASTLAP.NO
}
}
}
}
}
}
This seems to do the job, but often I get concurrent modification exception. Is there a better way of solving this problem?
I don't recommend combining mutable lists with read-write var properties. Making it mutable in two different ways creates ambiguity and is error prone. Since you're just clearing and replacing the list contents, I would make it a read-only list and a read-write property.
You need to synchronize the whole function so it can only be executed once at a time.
object RunDB {
private var listOfRunners: List<RunInfo> = listOf()
private var previousList: List<RunInfo> = listOf()
fun save(runList: List<RunInfo>) {
sychronized(this) {
previousList = listOfRunners.toList()
listOfRunners = runList.toList()
listOfRunners.forEach { runner ->
previousList.forEach { previousLap ->
if (runner.runnerId == previousLap.runnerId) {
runner.betterThanLastLap =
when {
previousLap.lapTime == 0.0 -> BETTERTHANLASTLAP.NA
runner.lapTime >= previousLap.lapTime -> BETTERTHANLASTLAP.YES
else -> BETTERTHANLASTLAP.NO
}
}
}
}
}
}
}
It also feels error prone to have a mutable data class in these lists that you're copying and shuffling around. I recommend making it immutable:
data class RunInfo(
val runnerId: String,
val lapTime: Double,
val betterThanLastLap: BETTERTHANLASTLAP
)
object RunDB {
private var listOfRunners: List<RunInfo> = listOf()
private var previousList: List<RunInfo> = listOf()
fun save(runList: List<RunInfo>) {
sychronized(this) {
previousList = listOfRunners.toList()
listOfRunners = runList.map { runner ->
val previousLap = previousList.find { runner.runnerId == previousLap.runnerId }
runner.copy(betterThanLastLap = when {
previousLap == null || previousLap.lapTime == 0.0 -> BETTERTHANLASTLAP.NA
runner.lapTime >= previousLap.lapTime -> BETTERTHANLASTLAP.YES
else -> BETTERTHANLASTLAP.NO
})
}
}
}
}
This code is based on Coroutines guide example: Fan-out
val inputProducer = produce<String>(CommonPool) {
(0..inputArray.size).forEach {
send(inputArray[it])
}
}
val resultChannel = Channel<Result>(10)
repeat(threadCount) {
launch(CommonPool) {
inputProducer.consumeEach {
resultChannel.send(getResultFromData(it))
}
}
}
What is the right way to create a Sequence<Result> that will provide results?
You can get the channel .iterator() from the ReceiveChannel and then wrap that channel iterator into a Sequence<T>, implementing its normal Iterator<T> that blocks waiting for the result on each request:
fun <T> ReceiveChannel<T>.asSequence(context: CoroutineContext) =
Sequence {
val iterator = iterator()
object : AbstractIterator<T>() {
override fun computeNext() = runBlocking(context) {
if (!iterator.hasNext())
done() else
setNext(iterator.next())
}
}
}
val resultSequence = resultChannel.asSequence(CommonPool)
I had the same problem, and in the end I came up with this rather unusual/convoluted solution:
fun Channel<T>.asSequence() : Sequence<T> {
val itr = this.iterator()
return sequence<Int> {
while ( runBlocking {itr.hasNext()} ) yield( runBlocking<T> { itr.next() } )
}
}
I do not think it is particular efficient (go with the one provided by #hotkey), but it has a certain appeal to me at least.
This code works, but the duplicate find seems less than optimal. Is it possible to implement the same functionality without the duplication?
def pattern = ~'some_regex'
def inFile = new File('in')
inFile.eachLine { String line ->
if (line.find(pattern)) {
line.find(pattern) { match ->
... // do something
}
}
else {
... // do something (else)
}
}
I'd suggest to use eachMatch()
inFile.eachLine { String line ->
String matched
line.eachMatch( pattern ){
matched = it[ 0 ]
doSomethingWithMatch matched
}
if( !matched ) doNoMatch()
}
I have a function like this:
private downloadAllFiles() {
sftpRetriever.listFiles().findAll {
filter.isResponse(it) || filter.isResponseTurned(it)
}.each { String fileName ->
log.info 'Downloading file: {}', fileName
sftpRetriever.downloadFile(fileName)
log.info 'File downloaded'
removeRemoteFile(fileName)
}
}
I am looking for a simple way of modyfing this closure inside of that function so if the size() of findAll is 0 it will simply log 'No more files to download' and .each won't be executed. Is there any simple way to make it in single closure? It is really simply task if I divide it in several parts, but trying to learn closures here and improve my expressiveness :) Thank you in advance for your help.
Take a look at creature below :) It works due the fact that each returns the collection on which it's invoked (+ elvis operator and quite nice Groovy's truth evaluation):
def printContents(Collection collection) {
collection.each {
println it
} ?: println('Collection is empty')
}
printContents([1,2,'hello'])
printContents([])
I don't like this syntax but it's the shorter version which came to my mind.
You can also use metaprogramming to add the method provided by Steinar. It must be added to metaClass before first use but you'll avoid an effort to make extension module:
Collection.metaClass.doIfEmpty { Closure ifEmptyClosure ->
if (delegate.empty) {
ifEmptyClosure()
}
return delegate
}
def printContents(Collection collection) {
collection.doIfEmpty {
println "Collection is empty"
}.each {
println it
}
}
printContents([1,2,'hello'])
printContents([])
One rather generic and reusable option is to extend Collection using an extension module. This is surprisingly easy to do and is even recognized in IDE's (at least in IntelliJ) so you get code completion, etc.
For example, write an the extension class for collections which will perform the closure if the collection is empty. In addtion, it should always return the collection to allow further chaining:
package stackoverflow
class CollectionExtension {
static <T extends Collection> T doIfEmpty(T self, Closure closure) {
if (self.empty) {
closure()
}
return self
}
}
You will also need to tell groovy that this file is an extension module. Add a property file as a resource on the classpath: META-INF/services/org.codehaus.groovy.runtime.ExtensionModule (note: this name and location is mandatory for extension modules, i.e. you cannot change it).
moduleName=stackoverflow-module
moduleVersion=1.0
extensionClasses=stackoverflow.CollectionExtension
Finally a simple test script to show how this can be used:
def printContents(Collection collection) {
collection.doIfEmpty {
println "Collection is empty"
}.each {
println it
}
}
printContents([1,2,'hello'])
printContents([])
Output:
1
2
hello
Collection is empty
You may try the following piece of code:
def l1 = [1,2,3,4]
def l2 = [5,6,7,8]
def m(list) {
list.findAll { it < 5}.with { l ->
size > 0 ?
l.each { e ->
println e
}
:
println('Zero elements found')
}
}
m(l1)
m(l2)
No better idea at the moment.
It would seem that GPathResult returns a list of nodes which is the same for each iteration. How can I insert nodes from one iteration and find them in the next, as shown in the example below?
def messageNodes = [] as HashSet
def inputRoot = new XmlSlurper().parse(xmlFile)
inputRoot.testsuite.list().each { suiteNode ->
suiteNode.children().list().each { caseNode ->
caseNode.children().list().each { messageNode ->
messageNodes << messageNode
}
}
}
inputRoot.testsuite.list().each { suiteNode ->
suiteNode.children().list().each { caseNode ->
caseNode.children().list().each { messageNode ->
assert messageNodes.contains(message)
}
}
}
That code isn't inserting nodes... But as it stands (trying to find nodes are the same between scans), it won't work either due to the way XmlSlurper works...
When you call children() on a GPathResult, you end up with an iterator that returns new instances of NodeChild. Because these are new instances, they cannot be compared to the last time you saw them (as their hashcode will be different)
If you change your code to use XmlParser like so:
def messageNodes = []
def inputRoot = new XmlParser().parse(xmlFile)
inputRoot.testsuite.'*'.'*'.each { messageNode ->
messageNodes << messageNode
}
inputRoot.testsuite.'*'.'*'.each { messageNode ->
println messageNode.hashCode()
assert messageNodes.contains(messageNode)
}
I believe it works as you'd expect (I had to guess at your xml format though, as you didn't show us what it was in the question)
As an addition, you can change the lines
inputRoot.testsuite.'*'.'*'.each { messageNode ->
messageNodes << messageNode
}
to
messageNodes.addAll( inputRoot.testsuite.'*'.'*' )
for the same functionality...
It should also be noted that XmlSlurper doesn't store internal state in a navigable way, so if you add nodes, you cannot find them unless you write the xml out and read it back in. If this is the sort of thing you're aiming for, XmlParser is probably also the better route