def path_eq(path, equals_to, value): """Determines whether a nested path on an object has a specific value, in R.equals terms. Most likely used to filter a list""" try: return equals(_path(path, value), equals_to) except KeyError: return False
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""" try: return equals(object[property], value) except KeyError: return False
def index_of(y, xs): """Returns the position of the first occurrence of an item in an array, or -1 if the item is not included in the array. R.equals is used to determine equality""" eq_y = equals(y) for i, x in enumerate(xs): if eq_y(x): return i return -1
def contains(x, xs): return any(equals(x), xs)
from ramda.private.asserts import * from ramda.cond import cond from ramda.always import always from ramda.equals import equals from ramda.T import T f = cond([ (equals(0), always("water freezes at 0°C")), (equals(100), always("water boils at 100°C")), (T, lambda temp: "nothing special happens at " + str(temp) + "°C"), ]) def test_cond(): assert_equal(f(0), "water freezes at 0°C") assert_equal(f(100), "water boils at 100°C") assert_equal(f(50), "nothing special happens at 50°C") assert_equal(cond([], 10), None)
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 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))