Anonymous user
Hash join: Difference between revisions
→{{header|Groovy}}
Line 444:
=={{header|Groovy}}==
Semi-imperative style:
<lang Groovy>
def hashJoin(
def hashed =
def q = [] as Set
def join = hashed[
join.each {
q <<
}
}
Line 462 ⟶ 463:
</lang>
More functional style:
<lang Groovy>
def hashJoin(table1, col1, table2, col2) {
def hashed = table1.groupBy { s -> s[col1] }
table2.collect { r ->
hashed[r[col2]].collect { s -> s.plus(r) }
}.flatten()
}
</lang>
Sample run (either version as the result is the same):
<lang Groovy>
def s = [[age: 27, name: 'Jonah'],
[age: 18, name: 'Alan'],
Line 476 ⟶ 489:
[name: 'Glory', nemesis: 'Buffy']]
hashJoin(s, "name", r, "name").sort {it.name}.each { println it }
</
produces:
|