Mapping over the range and pattern matching operators

⇐ Notes archive

(This is an entry in my technical notebook. There will likely be typos, mistakes, or wider logical leaps — the intent here is to “let others look over my shoulder while I figure things out.”)

Two tricks for the operator and point-free-leaning crowd:

  1. To quickly form ranges from a sequence of indices, you can use zip(indices, indices.dropFirst()).map(..<) (or subbing in the closed range operator — the one-sided variants are ambiguous in the point-free style)1.

     let odds = [1, 3, 5, 7]
    
     zip(odds, odds.dropFirst()).map(..<) // ⇒ [(1..<3), (3..<5), (5..<7)]
    
  2. And tangentially, to test if parallel sequences of ranges and values match against one another.

     // (…continuing from above.)
    	
     let odds = [1, 3, 5, 7]
     let oddRanges = zip(odds, odds.dropFirst()).map(..<)
    	
     let evens = [2, 4, 6]
     zip(oddRanges, evens).map(~=) // ⇒ [true, true, true]
    

  1. Ended up using this in a note on Collection.batchedSubscribe(by:)