C. Keith Ray

C. Keith Ray writes about and develops software in multiple platforms and languages, including iOS® and Macintosh®.
Keith's Résumé (pdf)

Tuesday, October 10, 2017

Playing With Compare And Closures

// playing with compare() and closures

import Foundation

print("actOn start")
let one = "1"
let two = "2"

func actOn(_ result: ComparisonResult
        ifLess: ()->Void
        ifEqual: ()->Void
        ifGreater: ()->Void ) {
    switch result {
        case .orderedAscending:
            ifLess()
        case .orderedSame:
            ifEqual()
        case .orderedDescending:
            ifGreater()
    }
}

actOn(one.compare(two), ifLess: {print("    string 1 < 2")},
        ifEqual: {print("    string 1 == 2")},
        ifGreater: {print("    string 1 > 2")})

actOn(two.compare(two), ifLess: {print("    string 2 < 2")},
        ifEqual: {print("    string 2 == 2")},
        ifGreater: {print("    string 2 > 2")})

actOn(two.compare(one), ifLess: {print("    string 2 < 1")},
        ifEqual: {print("    string 2 == 1")},
        ifGreater: {print("    string 2 > 1")})

print("actOn end\n")

print("switch start")

switch one.compare(two) {
    case .orderedAscending:   print("    switch 1 < 2")
    case .orderedSame:        print("    switch 1 == 2")
    case .orderedDescendingprint("    switch 1 > 2")
}

switch two.compare(two) {
    case .orderedAscending:
        print("    switch 2 < 2")
    case .orderedSame:
        print("    switch 2 == 2")
    case .orderedDescending:
        print("    switch 2 > 2")
}

switch two.compare(one) {
    case .orderedAscending:
        print("    switch 2 < 1")
    case .orderedSame:
        print("    switch 2 == 1")
    case .orderedDescending:
        print("    switch 2 > 1")
}

print("switch end\n")

print("compare start")

func compareComparable
>(_ expr1: T, _ expr2: T, ifLess: ()->Void, ifEqual: ()->Void, ifGreater: ()->Void ) {
    if expr1 < expr2 {
        ifLess()
    }
    else if expr1 == expr2 {
        ifEqual()
    }
    else if expr1 > expr2 {
        ifGreater()
    }
    else {
       assertionFailure("\(expr1) isn't < or == or > \(expr2)")
    }
}

compare(1, 2,  ifLess:     {print("    1 < 2")},
               ifEqual:    {print("    1 == 2")},
               ifGreater:  {print("    1 > 2")})

compare(2, 2, ifLess: {print("    2 < 2")},
    ifEqual: {print("    2 == 2")},
    ifGreater: {print("    2 > 2")})

compare(2, 1, ifLess: {print("    2 < 1")},
    ifEqual: {print("    2 == 1")},
    ifGreater: {print("    2 > 1")})

// trigger assertion (as designed)
compare(Double.nan, Double.leastNonzeroMagnitude, ifLess: {print("NaN < n")},
    ifEqual: {print("    NaN == n")},
    ifGreater: {print("    NaN > n")})

print("compare end\n")



----------- output -------------------------------------------------------

actOn start
    string 1 < 2
    string 2 == 2
    string 2 > 1
actOn end

switch start
    switch 1 < 2
    switch 2 == 2
    switch 2 > 1
switch end

compare start
    1 < 2
    2 == 2
    2 > 1
fatal error: nan isn't < or == or > 4.94065645841247e-324: file forloop.playground, line 76


No comments:

Post a Comment