Hash join: Difference between revisions

Line 444:
=={{header|Groovy}}==
 
Semi-imperative style:
<lang Groovy>
def hashJoin(stable1, rcol1, coltable2, col2) {
 
def hashed = stable1.groupBy { sItems -> sItems[colcol1] }
 
def q = [] as Set
 
rtable2.each { rItemr ->
def join = hashed[rItemr[colcol2]]
join.each { sItems ->
q << sItems.plus(rItemr)
}
}
Line 462 ⟶ 463:
</lang>
 
More functional style:
Sample run:
<lang Groovy>
<pre>
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 }
</prelang>
 
produces:
Anonymous user