def pluck(key, xs): """Returns a new list by plucking the same named property off all objects in the list supplied. pluck will work on any functor in addition to arrays, as it is equivalent to R.map(R.prop(k), f)""" return list(map(prop(key), xs))
def path(keys, dict): """Retrieve the value at a given path""" if not keys: raise ValueError("Expected at least one key, got {0}".format(keys)) current_value = dict for key in keys: current_value = prop(key, current_value) return current_value
def where(spec, object): """Takes a spec object and a test object; returns true if the test satisfies the spec. Each of the spec's own properties must be a predicate function. Each predicate is applied to the value of the corresponding property of the test object. where returns true if all the predicates return true, false otherwise. where is well suited to declaratively expressing constraints for other functions such as filter and find""" for k, f in spec.items(): if not f(prop(k, object)): return False return True
def test_index_by(): list = [{"id": "xyz", "title": "A"}, {"id": "abc", "title": "B"}] out = { "abc": { "id": "abc", "title": "B" }, "xyz": { "id": "xyz", "title": "A" } } assert_equal(index_by(prop("id"), list), out)
def prop_or(default, property, object): """If the given, non-null object has an own property with the specified name, returns the value of that property. Otherwise returns the provided default value""" return default_to(default, prop(property, object))
def prop_satisfies(predicate, property, object): """Returns true if the specified object property satisfies the given predicate; false otherwise. You can test multiple properties with R.where""" return predicate(prop(property, object))
def prop_eq(property, value, object): """Returns true if the specified object property is equal, in R.equals terms, to the given value; false otherwise. You can test multiple properties with R.where""" return equals(prop(property, object), value)
def props(properties, object): """Acts as multiple prop: array of keys in, array of values out. Preserves order""" return [prop(p, object) for p in properties]
def eq_props(property, object1, object2): """Reports whether two objects have the same value, in R.equals terms, for the specified property. Useful as a curried predicate""" return equals(prop(property, object1), prop(property, object2))