Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,33 @@
LeftRec := RECORD
UNSIGNED1 Age;
STRING6 Name;
END;
LeftFile := DATASET([{27,'Jonah'},{18,'Alan'},{28,'Glory'},{18,'Popeye'},{28,'Alan'}],LeftRec);
RightRec := RECORD
STRING6 Name;
STRING7 Nemesis;
END;
RightFile := DATASET([{'Jonah','Whales'},{'Jonah','Spiders'},{'Alan','Ghosts'},{'Alan','Zombies'},{'Glory','Buffy'}],
RightRec);
HashJoin := JOIN(LeftFile,RightFile,Left.Name = RIGHT.Name,HASH);
HashJoin;
//The HASH JOIN is built-in to the ECL JOIN by using the HASH JOIN Flag
/*
OUTPUT:
Age Name Nemesis
18 Alan Ghosts
18 Alan Zombies
28 Alan Ghosts
28 Alan Zombies
28 Glory Buffy
27 Jonah Whales
27 Jonah Spiders
*/

View file

@ -0,0 +1,27 @@
(define ages '((27 "Jonah") (18 "Alan") (28 "Glory") (18 "Popeye") (28 "Alan")))
(define nemesis '(("Jonah" "Whales") ("Jonah" "Spiders") ("Alan" "Ghosts") ("Alan" "Zombies") ("Glory" "Buffy")))
;; table: table name
;; source : input list
;; key-proc : procedure returning the join value ('name' in this task)
(define (table-hash table source key-proc )
(local-make-store table)
(for ((r source))
(local-put-value
(key-proc r)
(append (list r) (local-get-value (key-proc r) table)) table)))
;; build the two tables
(define-syntax-rule (second record) (cadr record))
(define (key-name-age record) (second record))
(table-hash 'AGES ages key-name-age)
(define (key-nemesis-name record) (first record))
(table-hash 'NEMESIS nemesis key-nemesis-name)
;; join
(for* ((k (local-keys 'AGES))
(a (local-get-value k 'AGES))
(n (local-get-value k 'NEMESIS)))
(writeln a n))

View file

@ -0,0 +1,7 @@
(28 "Alan") ("Alan" "Zombies")
(28 "Alan") ("Alan" "Ghosts")
(18 "Alan") ("Alan" "Zombies")
(18 "Alan") ("Alan" "Ghosts")
(28 "Glory") ("Glory" "Buffy")
(27 "Jonah") ("Jonah" "Spiders")
(27 "Jonah") ("Jonah" "Whales")

View file

@ -0,0 +1,23 @@
(defun hash (column table)
(lists:foldl
(lambda (x acc)
(orddict:append (proplists:get_value column x) x acc))
'()
table))
(defun get-hash (col hash-table)
(proplists:get_value
(proplists:get_value col r)
hashed))
(defun merge (row-1 row-2)
(orddict:merge
(lambda (k v1 v2) v2)
(lists:sort row-1)
(lists:sort row-2)))
(defun hash-join (table-1 col-1 table-2 col-2)
(let ((hashed (hash col-1 table-1)))
(lc ((<- r table-2))
(lc ((<- s (get-hash col-2 hashed)))
(merge r s)))))

View file

@ -0,0 +1,11 @@
> (set ss '((#(age 27) #(name "Jonah"))
(#(age 18) #(name "Alan"))
(#(age 28) #(name "Glory"))
(#(age 18) #(name "Popeye"))
(#(age 28) #(name "Alan"))))
> (set rs '((#(nemesis "Whales") #(name "Jonah"))
(#(nemesis "Spiders") #(name "Jonah"))
(#(nemesis "Ghosts") #(name "Alan"))
(#(nemesis "Zombies") #(name "Alan"))
(#(nemesis "Buffy") #(name "Glory"))))

View file

@ -0,0 +1,8 @@
> (hash-join ss 'name rs 'name)
(((#(age 27) #(name "Jonah") #(nemesis "Whales")))
((#(age 27) #(name "Jonah") #(nemesis "Spiders")))
((#(age 18) #(name "Alan") #(nemesis "Ghosts"))
(#(age 28) #(name "Alan") #(nemesis "Ghosts")))
((#(age 18) #(name "Alan") #(nemesis "Zombies"))
(#(age 28) #(name "Alan") #(nemesis "Zombies")))
((#(age 28) #(name "Glory") #(nemesis "Buffy"))))

View file

@ -0,0 +1,42 @@
Table1 = [[27, "Jonah"], [18, "Alan"], [28, "Glory"], [18, "Popeye"], [28, "Alan"]]
Table2 = [["Jonah", "Whales"], ["Jonah", "Spiders"], ["Alan", "Ghosts"], ["Alan", "Zombies"], ["Glory", "Buffy"]]
hTable = []
Qtable = []
for a in table1
h = hashing(a[2])
add(htable,[h , a])
next
for b in table2
h = hashing(b[1])
for sh in htable
if sh[1] = h
add(qtable, sh[2] + b[2])
ok
next
next
print(qtable)
#===============End of Execution=========
func print lst
see "---------------------------------------------------
Age | Name || Name | Nemesis
---------------------------------------------------
"
for l in lst
see string(l[1]) + char(9) + "| " + l[2] + copy(char(9),2) + "|| " + l[2] + " " + char(9) + "| " + l[3] + nl
next
func Hashing str
r = 0
if len(str) > 4
r = (ascii(str[1]) + ascii(str[len(str)]) + ascii(str[ceil(len(str) * 0.25)]) + ascii(str[ceil(len(str) * 0.75)]))
else
for s in str
r += ascii(s)
next
ok
return r

View file

@ -0,0 +1,30 @@
func hashJoin(table1, index1, table2, index2) {
var a = []
var h = Hash()
# hash phase
table1.each { |s|
h{s[index1]} := [] << s
}
# join phase
table2.each { |r|
a += h{r[index2]}.map{[_,r]}
}
return a
}
var t1 = [[27, "Jonah"],
[18, "Alan"],
[28, "Glory"],
[18, "Popeye"],
[28, "Alan"]]
var t2 = [["Jonah", "Whales"],
["Jonah", "Spiders"],
["Alan", "Ghosts"],
["Alan", "Zombies"],
["Glory", "Buffy"]]
hashJoin(t1, 1, t2, 0).each { .say }

View file

@ -0,0 +1,54 @@
LOCAL i As Integer, n As Integer
CLOSE DATABASES ALL
*!* Create and populate the hash tables
CREATE CURSOR people_ids(id I, used L DEFAULT .F.)
INDEX ON id TAG id COLLATE "Machine"
INDEX ON used TAG used BINARY COLLATE "Machine"
SET ORDER TO 0
CREATE CURSOR nem_ids(id I, used L DEFAULT .F.)
INDEX ON id TAG id COLLATE "Machine"
INDEX ON used TAG used BINARY COLLATE "Machine"
SET ORDER TO 0
n = 100
FOR i = 1 TO n
INSERT INTO people_ids (id) VALUES (i)
INSERT INTO nem_ids (id) VALUES (i)
ENDFOR
CREATE CURSOR people (age I, name V(16), id I)
INDEX ON id TAG id COLLATE "Machine"
INDEX ON name TAG name COLLATE "Machine"
SET ORDER TO 0
INSERT INTO people (age, name) VALUES (27, "Jonah")
INSERT INTO people (age, name) VALUES (18, "Alan")
INSERT INTO people (age, name) VALUES (28, "Glory")
INSERT INTO people (age, name) VALUES (18, "Popeye")
INSERT INTO people (age, name) VALUES (28, "Alan")
REPLACE id WITH HashMe("people_ids") ALL
*!* The plural of nemesis is nemeses
CREATE CURSOR nemeses (name V(16), nemesis V(16), p_id I, id I)
INDEX ON id TAG id COLLATE "Machine"
INDEX ON p_id TAG p_id COLLATE "Machine"
INDEX ON name TAG name COLLATE "Machine"
SET ORDER TO 0
INSERT INTO nemeses (name, nemesis) VALUES ("Jonah", "Whales")
INSERT INTO nemeses (name, nemesis) VALUES ("Jonah", "Spiders")
INSERT INTO nemeses (name, nemesis) VALUES ("Alan", "Ghosts")
INSERT INTO nemeses (name, nemesis) VALUES ("Alan", "Zombies")
INSERT INTO nemeses (name, nemesis) VALUES ("Glory", "Buffy")
REPLACE id WITH HashMe("nem_ids") ALL
UPDATE nemeses SET p_id = people.id FROM people ;
WHERE nemeses.name = people.name
*!* Show the join
SELECT pe.age, pe.name, ne.nemesis FROM people pe ;
JOIN nemeses ne ON pe.id = ne.p_id TO FILE "hashjoin.txt"
FUNCTION HashMe(cTable As String) As Integer
LOCAL ARRAY a[1]
SELECT MIN(id) FROM (cTable) WHERE NOT used INTO ARRAY a
UPDATE (cTable) SET used = .T. WHERE id = a[1]
RETURN a[1]
ENDFUNC

View file

@ -0,0 +1,35 @@
# hashJoin(table1; key1; table2; key2) expects the two tables to be
# arrays, either of JSON objects, or of arrays.
# In the first case, that is, if the table's rows are represented as
# objects, then key1 should be the key of the join column of table1,
# and similarly for key2; if the join columns have different names,
# then they will both be included in the resultant objects.
# In the second case, that is, if the rows are arrays, then the
# 0-based indices of the join columns should be specified, and the
# rows are simply pasted together, resulting in duplication of the
# join columns.
#
def hashJoin(table1; key1; table2; key2):
# collision-free hash function:
def h:
if type == "object" then with_entries(.value = (.value|h)) | tostring
elif type == "array" then map(h)|tostring
else (type[0:1]+tostring)
end;
# hash phase:
reduce table1[] as $row
({};
($row[key1]|h) as $key
| . + { ($key): (.[$key] + [$row]) } )
| . as $hash
# join phase
| reduce table2[] as $row
([];
($row[key2]|h) as $key
| if $hash|has($key) then
reduce $hash[$key][] as $r (.; . + [ $row + $r ] )
else . end)
;

View file

@ -0,0 +1,39 @@
def table1:
[ {"age": 27, "name": "Jonah"},
{"age": 18, "name": "Alan"},
{"age": 28, "name": "Glory"},
{"age": 18, "name": "Popeye"},
{"age": 28, "name": "Alan"} ]
;
def table2:
[ {"name": "Jonah", "nemesis": "Whales"},
{"name": "Jonah", "nemesis": "Spiders"},
{"name": "Alan", "nemesis": "Ghosts"},
{"name": "Alan", "nemesis": "Zombies"},
{"name": "Glory", "nemesis": "Buffy"} ]
;
def table1a:
[[27, "Jonah"],
[18, "Alan"],
[28, "Glory"],
[18, "Popeye"],
[28, "Alan"] ]
;
def table2a:
[["Jonah", "Whales"],
["Jonah", "Spiders"],
["Alan", "Ghosts"],
["Alan", "Zombies"],
["Glory", "Buffy"],
["Holmes", "Moriarty"] ]
;
def pp:
reduce .[] as $row (""; . + "\n" + ($row|tostring));
( hashJoin(table1; "name"; table2; "name"),
hashJoin(table1a; 1; table2a; 0)
) | pp

View file

@ -0,0 +1,13 @@
$ jq -c -r -n -f HashJoin.jq
{"age":27,"name":"Jonah","nemesis":"Whales"}
{"age":27,"name":"Jonah","nemesis":"Spiders"}
{"age":28,"name":"Alan","nemesis":"Ghosts"}
{"age":28,"name":"Alan","nemesis":"Zombies"}
{"age":28,"name":"Glory","nemesis":"Buffy"}
[27,"Jonah","Jonah","Whales"]
[27,"Jonah","Jonah","Spiders"]
[28,"Alan","Alan","Ghosts"]
[28,"Alan","Alan","Zombies"]
[28,"Glory","Glory","Buffy"]

View file

@ -0,0 +1,27 @@
# The tables should be arrays of arrays;
# index1 and index2 should be the 0-based indices of the join columns.
#
def hashJoinArrays(table1; index1; table2; index2):
# collision-free hash function:
def h:
if type == "object" then with_entries(.value = (.value|h)) | tostring
elif type == "array" then map(h)|tostring
else (type[0:1]+tostring)
end;
# hash phase:
reduce table1[] as $row
({};
($row[index1]|h) as $key
| . + (.[$key] += [ $row ]) )
| . as $hash
# join phase
| reduce table2[] as $row
([];
($row[index2]|h) as $key
| if $hash|has($key) then
reduce $hash[$key][] as $r
(.;
. + [ $r + $row[0:index2] + $row[index2+1:] ] )
else . end)
;

View file

@ -0,0 +1 @@
hashJoinArrays(table1; 1; table2; 0) | pp

View file

@ -0,0 +1,7 @@
$ jq -c -r -n -f HashJoinArrays.jq
[27,"Jonah","Whales"]
[27,"Jonah","Spiders"]
[28,"Alan","Ghosts"]
[28,"Alan","Zombies"]
[28,"Glory","Buffy"]