Another silly question about Stride

How to divide a value on two equal parts, if we only know about a value is Strideable.

func findMiddleBetweenTwo<Point: Strideable>(left: Point, right: Point) -> Point.Stride {
    let distance = left.distance(to: right)
    let half = distance.howToGetTwoEqualParts ????
}

You can’t do this directly because Point.Stride only conforms to Comparable and SignedNumeric, neither of which give you divide. I would solve this by requiring that the stride conform to BinaryInteger [1]:

func findMiddleBetweenTwo<Point: Strideable>(left: Point, right: Point) -> Point where
    Point.Stride: BinaryInteger
{
    let distance = left.distance(to: right)
    let half = distance / 2
    return left.advanced(by: half)
}

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

[1] Or FloatingPoint, depending on what you’re using this for.

Thank you. I supposed that maybe it is possible by iterating over distance and counting iteration steps. But with out dividing it is impossible I see.

Terms of Service

Privacy Policy

Cookie Policy