Skip to content

Commit

Permalink
Merge pull request #130 from dn-m/refine-sum-product
Browse files Browse the repository at this point in the history
Make Sum and Product types conform the protocols of their respective wrapped types
  • Loading branch information
jsbean authored Aug 9, 2018
2 parents fb666ef + ef3efac commit b552efc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
23 changes: 19 additions & 4 deletions Sources/Algebra/Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
//

/// Multiplicative monoidal view of a `Multiplicative`-conforming type.
public struct Product <T: Multiplicative>: MonoidView {
public struct Product <Value: Multiplicative>: MonoidView {

// MARK: - Type Properties

/// - Returns: The multiplicative identity wrapped in a `MultiplicativeMonoid`.
public static var identity: Product {
return Product(T.one)
return Product(Value.one)
}

// MARK: - Type Methods
Expand All @@ -26,16 +26,31 @@ public struct Product <T: Multiplicative>: MonoidView {
// MARK: - Instance Properties

/// Value wrapped by `MultiplativeMonoid`.
public let value: T
public let value: Value

// MARK: - Initializers

/// Creates a `MultiplicativeMonoid` with the given `value.`
public init(_ value: T) {
public init(_ value: Value) {
self.value = value
}
}

extension Product: Multiplicative {

// MARK: - Multiplicative

/// - Returns: The wrapped-up `one` type property of the wrapped type.
public static var one: Product<Value> {
return Product(Value.one)
}

/// - Returns: The wrapped-up product of the two given values.
public static func * (lhs: Product<Value>, rhs: Product<Value>) -> Product<Value> {
return Product(lhs.value * rhs.value)
}
}

extension Multiplicative {

/// - Returns: A `Product` monoidal view of `self`.
Expand Down
25 changes: 20 additions & 5 deletions Sources/Algebra/Sum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//
//

/// Multiplicative monoidal view of a `Additive`-conforming type.
public struct Sum <T: Additive>: MonoidView {
/// Additive monoidal view of a `Additive`-conforming type.
public struct Sum <Value: Additive>: MonoidView {

// MARK: - Type Properties

/// - Returns: The additive identity wrapped in a `AdditiveMonoid`.
public static var identity: Sum {
return Sum(T.zero)
return Sum(Value.zero)
}

// MARK: - Type Methods
Expand All @@ -26,16 +26,31 @@ public struct Sum <T: Additive>: MonoidView {
// MARK: - Instance Properties

/// Value wrapped by `AdditiveMonoid`.
public let value: T
public let value: Value

// MARK: - Initializers

/// Creates a `AdditiveMonoid` with the given `value.`
public init(_ value: T) {
public init(_ value: Value) {
self.value = value
}
}

extension Sum: Additive {

// MARK: - Additive

/// - Returns: The wrapped-up `zero` type property of the wrapped type.
public static var zero: Sum<Value> {
return Sum(Value.zero)
}

/// - Returns: The wrapped-up sum of the two given values.
public static func + (lhs: Sum<Value>, rhs: Sum<Value>) -> Sum<Value> {
return Sum(lhs.value + rhs.value)
}
}

extension Additive {

/// - Returns: A `Sum` monoidal view of `self`.
Expand Down

0 comments on commit b552efc

Please sign in to comment.