Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Ranges mapping playground
// Playground containing the process I did today of looking for a generic way to get an object associated with a range, using Swift
// for example, given a mapping of range 0..<2 to a string "first range" and 2..<4 to a string "second range", we want to get the string for the value 3.4
// I started thinking about this because in my work I had to map rating ranges to certain background colors. So for example if a user gives a 5.9 rating (very good!) this would be in the range 4.5 - 6.0 which has to be displayed with a dark green background, etc. Sadly I had to do this using objc. I wondered how I would do it with Swift.
// The most simple way:
let n = 3.4
let found1:String? = {if n < 2 {
return "first"
} else if n < 4 {
return "second"
} else {
return nil
}}()
println("found: \(found1)")
// it's also possible to use a switch case. Rather weird (like in "don't do it"), but we are only playing.
let found2:String? = {switch n {
case _ where (0..<2) ~= n: return "first"
case _ where (2..<4) ~= n: return "second"
default: return nil
}
}()
println("found: \(found2)")
// these approaches are a bit unflexible. And the worst: NOT FANCY. We explore further.
// intro: get (first) containing range for a value
let result:HalfOpenInterval<Float>? = {
for r:HalfOpenInterval<Float> in [(0..<1), (1..<2), (2..<3)] {
if r ~= 2 {
return r
}
}
return nil
}()
// good! Now, we will make a helper class that holds the range and the object:
struct RangedEntry<T> {
let interval:HalfOpenInterval<Float>
let obj:T
init(_ interval:HalfOpenInterval<Float>, _ obj:T) {
self.interval = interval
self.obj = obj
}
}
// now we can iterate through entries to find our match:
for re in [
RangedEntry((0..<1), "a"),
RangedEntry((1..<2), "b"),
RangedEntry((2..<3), "c")
] {
if re.interval ~= 1.9 {
println("found: \(re.obj)")
break;
}
}
// alternatively, we can just use tuples:
for r:(HalfOpenInterval<Float>, String) in [
((0..<1), "a"),
((1..<2), "b"),
((2..<3), "c")
] {
if r.0 ~= 1.9 {
println("found: \(r.1)")
}
}
// what about more functional constructs?
// let's try out filter (we will continue using tuples):
let value:String? = [
((0..<1), "a"),
((1..<2), "b"),
((2..<3), "c")
].filter{(tuple:(HalfOpenInterval<Float>, String)) -> Bool in
return tuple.0 ~= 1.9
}.first?.1
println("found: \(value)")
// this is O(n), since we will examine the whole array each time. Not so good. Coming back later to this. Also note we assume that the ranges don't overlap.
// what if we want to generate the ranges dynamically, say, divide 1...7 in 16 equal parts?
// since in this case the ranges are continuos, we can represent them just as a succession of numbers.
// we can use strides for this:
let start:Float = 1
let end:Float = 7
let sections = 16
let intervalLength:Float = (end - start) / Float(sections)
let s = stride(from: start, through: end, by: intervalLength)
Array(s) // see contents in playground
// generate tuples with dummy value objects. The start of the range represents the range, and we associate the value object with it.
let tuples:[(Float, String)] = Array(s).map {(val:Float) -> (Float, String) in
return (val, "range starts:\(val) ends: \(val + intervalLength)")
}
// now let's try out an alternative approach to =~. Since in this case we know the ranges are sorted in increasing order, we can use this information to find the value.
// the number we will search from now on
let search:Float = 4.12
// with this we get retrieve the ranges witch a start value smaller than our searched value
let filtered = tuples.filter {
return $0.0 < search
}
// the range we are looking for is the rangest with the biggest starting value from all the ranges with a starting value smaller than the searched value
// since we know the ranges are sorted increasingly, we can just pick the last one from the filter results:
let val:String? = filtered.last?.1
println("found: \(val)")
// if here we didn't know that the values are sorted, we could determine it using e.g. reduce:
let val1:String? = {
filtered.isEmpty ? nil : filtered.reduce(filtered.first!, {
return $0.0 > $1.0 ? $0 : $1
}).1
}()
println("found: \(val1)")
// but the option with the best performance is a loop, that exits when we find the range.
// since there's no filtering step, we have to add logic to look ahead in the next range. If there's no next, or if its starting value is bigger than searched value, we know we are in the searched range.
let myresult:String? = {
for (index, tup) in enumerate(tuples) {
if tup.0 <= search && (index + 1 == tuples.count || tuples[index + 1].0 > search) {
return tup.1
}
}
return nil
}()
println("found: \(myresult)")
// talking about indices and look-ahead, here is a similar approach using filter (just demonstrative purpose - would not use it):
let val2:String? = Array(enumerate(tuples)).filter { (index:Int, element) -> Bool in
return element.0 <= search && (index + 1 == tuples.count || tuples[index + 1].0 > search)
}.first?.1.1
println("found: \(val2)")
// so is there a way in which we use a functional construct with the performance of for loop?
// we need something similar to a find function. This is an example of a find function:
// (this is btw a nice to have in array extension)
func find<T>(arr:Array<T>, pred:(element:T)->Bool) -> T? {
for i in 0..<arr.count {
let element = arr[i]
if pred(element: element) {
return element
}
}
return nil
}
// in our current case we need that the predicate, besides of the current element, considers also the next one (note nextElement is optional - when we are examining the last element, there's no next element), so we adjust find a bit:
func findWithNextElement<T>(arr:Array<T>, pred:(element:T, nextElement:T?)->Bool) -> T? {
for i in 0..<arr.count {
let element = arr[i]
let nextElement:T? = i + 1 < arr.count ? arr[i + 1] : nil
if pred(element: element, nextElement: nextElement) {
return element
}
}
return nil
}
// we would call it like this:
let val4:String? = findWithNextElement(tuples, { (element, nextElement:(Float, String)?) -> Bool in
return (element.0 <= search) && (nextElement?.0 > search ?? true)
})?.1
println("found: \(val4)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment