Skip to content

tech

Custom Operator Example (Swift 4)

Example of creating a custom operator with protocol and method under Swift 4:

  • declare the RegularExpressionMatchable protocol, with a single match method
  • extend String to conform to the newly declared protocol
  • implement the generic ~= operator, which conform to the RegularExpressionMatchableProtocol
  • the user can either use the match method or the operator to perform regex matching on String
protocol RegularExpressionMatchable {
    func match(pattern: String, options: NSRegularExpression.Options) -> Bool
}

extension String: RegularExpressionMatchable {
    func match(pattern: String, options: NSRegularExpression.Options = []) -> Bool {
        let regex = try? NSRegularExpression(pattern: pattern, options: options)
        
        if let regex = regex {
            return regex.numberOfMatches(in: self, options: [], range: NSMakeRange(0, self.utf16.count)) != 0
        }
        
        return false
    }
}

precedencegroup RegularExpressionMatchablePrecedence {
    associativity: right
    higherThan: MultiplicationPrecedence
}

infix operator ~=: RegularExpressionMatchablePrecedence

func ~=<T: RegularExpressionMatchable> (left: T, right: String) -> Bool {
    return left.match(pattern: right, options: [])
}

let foo = "Foo"
print(foo.match(pattern: "foo", options: NSRegularExpression.Options.caseInsensitive))
print(foo.match(pattern: "bar", options: NSRegularExpression.Options.caseInsensitive))

 

Spotlight, search with tags

I am a die hard fan of Spotlight search. What is spotlight? Press ⌘ + Space on your Macintosh:

I have been using spotlight search since the day I have discovered it, and then I have always wonder how to search with “tag”. I don’t remember since which version macOS has started to support the tagging for files, folders, etc. For sure, I was very excited with this new feature, but soon I found that I have no way to search it from spotlight, thus stop using it.

And, finally I found the way of search with tags from spotlight 😃

To search with multiple tags (for narrow down your search result), just separate each tag with a space,

Now, I have a reason to tag all my files, folders etc. Keep tagging, keep searching ouch 😖

Swift == Haskell?

Ever since Swift came out, I keep seeing weird comments that Swift is a functional programming language. I’ve puzzled a bit over why anyone would say that, since there’s really very little “functional” about Swift. It’s a pretty traditional object oriented language with a focus on generic programming.

I’m saying that a taxonomy that groups together Swift and Haskell because they have map is no more useful than grouping Wangga, Opera, and Rock together because they have vocalists.

Functional programming is primarily concerned with decomposing problems into functions that accept and return immutable values. Its usual structure is a collection of functions that transform values into other values, and various ways to combine functions. It avoids mutable state and does not require that the evaluation of functions occur in any particular order. Functional programming treats a program as a math problem rather than a series of operations. When faced with a problem, the functional question is “what kinds of values need to be transformed in what ways to solve this?”

Cocoaphony: Swift is not functional