Last active
May 11, 2020 18:04
-
-
Save eMdOS/3861a37de2762bcede0f045fdb83dbd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// Associative Law | |
/// | |
/// **Associativity** is the condition that the 2 ways to use a "binary composition" of morphisms to compose a sequence of 3 morphisms "are equal". | |
/// | |
/// - Associative through **Addition**: `a + (b + c) = (a + b) + c` | |
/// - Associative through **Multiplication**: `a * (b * c) = (a * b) * c` | |
/// | |
public enum AssociativeLaw<Element: Equatable> {} | |
public extension AssociativeLaw { | |
/// | |
/// It verifies if the set of elements are **associative**. | |
/// | |
/// Given a set of 3 elements, and a binary operation, the associativity "verification" happens. | |
/// | |
/// - Parameters: | |
/// - a: The first element | |
/// - b: The second element | |
/// - c: The third element | |
/// - operation: The associative binary operation to evaluate on | |
/// | |
static func verify(a: Element, b: Element, c: Element, operation: (Element, Element) -> Element) -> Bool { | |
// a <> (b <> c) = (a <> b) <> c | |
operation(a, operation(b, c)) == operation(operation(a, b), c) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment