Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
|
|
@ -0,0 +1,15 @@
|
|||
import ballerina/io;
|
||||
|
||||
function mergeMaps(map<any> m1, map<any> m2) returns map<any> {
|
||||
map<any> m3 = {};
|
||||
foreach string key in m1.keys() { m3[key] = m1.get(key); }
|
||||
foreach string key in m2.keys() { m3[key] = m2.get(key); }
|
||||
return m3;
|
||||
}
|
||||
|
||||
public function main() {
|
||||
map<any> base = { "name": "Rocket Skates" , "price": 12.75, "color": "yellow" };
|
||||
map<any> update = { "price": 15.25, "color": "red", "year": 1974 };
|
||||
map<any> merged = mergeMaps(base, update);
|
||||
io:println(re `,`.replaceAll(merged.toString(), ", "));
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(defun merge-hash-tables (hashtable &rest other-hashtables)
|
||||
(let ((result (make-hash-table :test (hash-table-test hashtable))))
|
||||
(dolist (ht (list* hashtable other-hashtables))
|
||||
(maphash #'(lambda (k v) (setf (gethash k result) v)) ht))
|
||||
result))
|
||||
|
||||
;; aux functions
|
||||
(defun make-hash-table-from-alist (alist &key (test 'equal))
|
||||
(let ((result (make-hash-table :test test)))
|
||||
(loop for (k . v) in alist
|
||||
do (setf (gethash k result) v))
|
||||
result))
|
||||
(defun make-alist-from-hash-table (hashtable)
|
||||
(let ((result ()))
|
||||
(maphash #'(lambda (k v) (setf result (acons k v result))) hashtable)
|
||||
(nreverse result)))
|
||||
|
||||
;; solving the task
|
||||
(let ((base (make-hash-table-from-alist '(("name" . "Rocket Skates")
|
||||
("price" . 12.75)
|
||||
("color" . "yellow"))))
|
||||
(update (make-hash-table-from-alist '(("price" . 15.25)
|
||||
("color" . "red")
|
||||
("year" . 1974)))))
|
||||
(format t "base: ~a~%update: ~a~%merged: ~a~%"
|
||||
(make-alist-from-hash-table base)
|
||||
(make-alist-from-hash-table update)
|
||||
(make-alist-from-hash-table (merge-hash-tables base update))))
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
base$[][] = [ [ "name" "Rocket Skates" ] [ "price" 12.75 ] [ "color" "yellow" ] ]
|
||||
update$[][] = [ [ "price" 15.25 ] [ "color" "red" ] [ "year" 1974 ] ]
|
||||
proc update . a$[][] b$[][] .
|
||||
proc update &a$[][] &b$[][] .
|
||||
for b to len b$[][]
|
||||
for a to len a$[][]
|
||||
if a$[a][1] = b$[b][1]
|
||||
|
|
@ -8,9 +8,7 @@ proc update . a$[][] b$[][] .
|
|||
break 1
|
||||
.
|
||||
.
|
||||
if a > len a$[][]
|
||||
a$[][] &= b$[b][]
|
||||
.
|
||||
if a > len a$[][] : a$[][] &= b$[b][]
|
||||
.
|
||||
.
|
||||
update base$[][] update$[][]
|
||||
|
|
|
|||
|
|
@ -1,18 +1,19 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
const base = {
|
||||
"name": "Rocket Skates",
|
||||
"price": 12.75,
|
||||
"color": "yellow"
|
||||
};
|
||||
|
||||
console.log(JSON.stringify(
|
||||
Object.assign({}, // Fresh dictionary.
|
||||
{ // Base.
|
||||
"name": "Rocket Skates",
|
||||
"price": 12.75,
|
||||
"color": "yellow"
|
||||
}, { // Update.
|
||||
"price": 15.25,
|
||||
"color": "red",
|
||||
"year": 1974
|
||||
}
|
||||
),
|
||||
null, 2
|
||||
))
|
||||
})();
|
||||
const update = {
|
||||
"price": 15.25,
|
||||
"color": "red",
|
||||
"year": 1974
|
||||
};
|
||||
|
||||
// While ES6 destructuring may be cleaner, using Object.assign (provided in the original answer) instead is about 15-20% faster.
|
||||
// source: https://jsbench.me/jom7uh9o1t/1
|
||||
|
||||
const final = Object.assign(base, update);
|
||||
// Using ES6 destructuring method: const final = { ...base, ...update };
|
||||
|
||||
console.log(JSON.stringify(final, null, 4));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue