Skip to content

Instantly share code, notes, and snippets.

@AlexanderNey
Last active August 29, 2015 14:02
Show Gist options
  • Save AlexanderNey/cce02e7d20a07353a58c to your computer and use it in GitHub Desktop.
Save AlexanderNey/cce02e7d20a07353a58c to your computer and use it in GitHub Desktop.
lower and upper limiter operator in swift
//
// LimitOperator.swift
//
// Created by Alexander Ney on 22/06/2014.
// Copyright (c) 2014 Alexander Ney. All rights reserved.
//
// Lower limit operator
operator infix |- {associativity right}
func |- <T : Comparable>(lowerLimit: T, value: T) -> T
{
return value < lowerLimit ? lowerLimit : value;
}
// Upper limit operator
operator infix -| {associativity right}
func -| <T : Comparable>(value: T, upperLimit: T) -> T
{
return value > upperLimit ? upperLimit : value;
}
@AlexanderNey
Copy link
Author

_Limiter Operators_

The will work like this

var limitedNumber = 0 |- x -| 100

limitedNumber will be always in the limits including 0 ... 100

You can use the operators alone to just define a lower or upper limit

var lowerLimitedNumber = 0 |- x
var upperLimitedNumber = x -| 100

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment