def merge_all(objects): """Merges a list of objects together into one object""" result = clone(objects[0]) for object in objects[1:]: for k, v in object.items(): result[k] = v return result
def assoc(key, value, object): """Makes a shallow clone of an object, setting or overriding the specified property with the given value. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference""" o = clone(object) o[key] = value return o
def merge(object1, object2): """Create a new object with the own properties of the first object merged with the own properties of the second object. If a key exists in both objects, the value from the second object will be used""" result = clone(object1) for k, v in object2.items(): result[k] = v return result
def spec_applier(*args): out = clone(spec) for k, v in spec.items(): try: v.items() out[k] = apply_spec(v)(*args) except AttributeError: out[k] = v(*args) return out
def merge_with(function, object1, object2): """Creates a new object with the own properties of the two provided objects. If a key exists in both objects, the provided function is applied to the values associated with the key in each object, with the result being used as the value associated with the key in the returned object""" out = clone(object1) for k, v in object2.items(): out[k] = v if k not in out else function(out[k], v) return out
def dissoc_path(path, object): """Makes a shallow clone of an object, omitting the property at the given path. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference""" _o = o = clone(object) for k in path[:-1]: _o = _o[k] del _o[path[-1]] return o
def assoc_path(path, value, object): """Makes a shallow clone of an object, setting or overriding the nodes required to create the given path, and placing the specific value at the tail end of that path. Note that this copies and flattens prototype properties onto the new object as well. All non-primitive properties are copied by reference""" o = _o = clone(object) for k in path[:-1]: if not (k in _o and isinstance(_o[k], dict)): _o[k] = {} _o = _o[k] _o[path[-1]] = value return o
def apply_spec(spec): """Given a spec object recursively mapping properties to functions, creates a function producing an object of the same structure, by mapping each property to the result of calling its associated function with the supplied arguments""" out = clone(spec) def spec_applier(*args): for k, v in spec.items(): try: v.items() out[k] = apply_spec(v)(*args) except AttributeError: out[k] = v(*args) return out return spec_applier
def evolve(transformations, object): """Creates a new object by recursively evolving a shallow copy of object, according to the transformation functions. All non-primitive properties are copied by reference. A transformation function will not be invoked if its corresponding key does not exist in the evolved object""" o = clone(object) for k, v in transformations.items(): try: if isinstance(v, dict): o[k] = evolve(v, o[k]) else: o[k] = v(o[k]) except KeyError: continue return o
def dissoc(key, object): """Returns a new object that does not contain a prop property""" o = clone(object) del o[key] return o