Class Pair<FirstType, SecondType>

Represents a generic pair of two Values. There is no meaning attached to values in this class. It can be used for any purpose. Pair exhibits values semantics, i.e. two pairs are equal if both components are equal. However, if any of those two values are instance of class, you'd better make the class explicitly distinguishable by inheriting KoconutEquatable. Please, check the example of 'equalsTo' method

See

-- Base --
[KoconutPair](KoconutPair.html), [Entry](Entry.html), [KoconutEntry](KoconutEntry.html)

-- Protocol --
[KoconutEquatable](../interfaces/KoconutEquatable.html)

Type Parameters

  • FirstType

    The type of the first value.

  • SecondType

    The type of the second value.

Hierarchy

  • Pair

Implements

Constructors

Accessors

Methods

Constructors

  • Constructor of Pair.

    Type Parameters

    • FirstType

    • SecondType

    Parameters

    • firstElement: FirstType

      FirstType element.

    • secondElement: SecondType

      SecondType element.

    Returns Pair<FirstType, SecondType>

Accessors

Methods

  • Class Pair implements KoconutEquatable. The 'equalsTo' method of this basically check whether each individual element (first/second) is same or not. When the type of each element is child of KoconutEquatable, it'd be done by using its 'equalsTo' method. Otherwise, it'd be done simply by '==' operator.

    Returns

    Example

      // Case 1 -- All values are simply number or string
    const myPairCase1_01 = Pair.from([10, 20])
    const myPairCase1_02 = Pair.from([10, 20])
    console.log(`${myPairCase1_01.equalsTo(myPairCase1_02)}`)
    // ↑ true

    const myPairCase1_03 = Pair.from(["Apex", "Captain"])
    const myPairCase1_04 = Pair.from(["Apex", "Captain"])
    console.log(`${myPairCase1_03 == myPairCase1_04}`)
    // ↑ false
    console.log(`${myPairCase1_03.equalsTo(myPairCase1_04)}`)
    // ↑ true

    // Case 2 -- First Type is indistinguishable class
    class MyClass {
    private value : string
    constructor(value : string) {
    this.value = value
    }
    }
    const myPairCase2_01 = Pair.from([new MyClass("Apex"), "Captain"])
    const myPairCase2_02 = Pair.from([new MyClass("Apex"), "Captain"])
    console.log(`${myPairCase2_01.equalsTo(myPairCase2_02)}`)
    // ↑ false

    // Case 3 -- First Type is distinguishable class
    class MyDistinguishableClass implements KoconutEquatable {
    private value : string
    constructor(value : string) {
    this.value = value
    }
    equalsTo(other : MyDistinguishableClass) : boolean {
    return this.value == other.value
    }
    }
    const myPairCase3_01 = Pair.from([new MyDistinguishableClass("Apex"), "Captain"])
    const myPairCase3_02 = Pair.from([new MyDistinguishableClass("Apex"), "Captain"])
    console.log(`${myPairCase3_01.equalsTo(myPairCase3_02)}`)
    // ↑ true

    Parameters

    • other: Pair<FirstType, SecondType>

    Returns boolean | KoconutBoolean

  • Turns this Pair instance into a simple array.

    Returns

    Example

    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toArray())
    // ↑ [ 'Apex', 'Captain' ]

    Returns [FirstType, SecondType]

  • Turns this Pair instance into a simple Entry

    Returns

    Example

    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toEntry())
    // ↑ Entry { keyElement: 'Apex', valueElement: 'Captain' }

    Returns Entry<FirstType, SecondType>

  • Turns this Pair instance into a simple JSON object string.

    Returns

    Example

    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair.toString()) // Or, you can use console.log(`${myPair}`)
    // ↑ {"first":"Apex","second":"Captain"}

    Returns string

  • Create a Pair instance by iterable two values pair.

    Returns

    const myPair = Pair.from(["Apex","Captain"])
    console.log(myPair)
    // ↑ Pair { firstElement: 'Apex', secondElement: 'Captain' }

    Type Parameters

    • FirstType

    • SecondType

    Parameters

    • pair: [FirstType, SecondType]

      Values pair of first/second as iterable.

    Returns Pair<FirstType, SecondType>

Generated using TypeDoc