From 34a413240386873c414fa332ed8bc5f958d70d16 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 16 Nov 2017 19:17:25 +0000 Subject: [PATCH] =?UTF-8?q?Add=20=E2=80=98ints=E2=80=99=20package=20for=20?= =?UTF-8?q?returning=20bounded=20number=20values=20from=20arguments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/ints/ints.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 util/ints/ints.go diff --git a/util/ints/ints.go b/util/ints/ints.go new file mode 100644 index 00000000..6c15bb64 --- /dev/null +++ b/util/ints/ints.go @@ -0,0 +1,58 @@ +// Copyright © 2016 Abcum Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ints + +func Min(min int, vals ...int) int { + for _, val := range vals { + if val < min { + min = val + } + } + return min +} + +func Max(max int, vals ...int) int { + for _, val := range vals { + if val > max { + max = val + } + } + return max +} + +func Below(cen, val int) int { + if val < cen { + return val + } + return cen +} + +func Above(cen, val int) int { + if val > cen { + return val + } + return cen +} + +func Between(min, max, val int) int { + switch { + case val < min: + return min + case val > max: + return max + default: + return val + } +}