on master help sign in
Implement "min", "max", and "clamp" in Elm
commitf098af57cc7551cb57ccbbc122d80b4f90ad5b03 authorEvan Czaplicki <info@elm-lang.org> dateWed 20 Aug 2014, 12:51 (12 years ago) parent8a4496c
2 files changed+6−10
libraries/Basics.elm+6−3 libraries/Native/Basics.js−7
libraries/Basics.elm+6−3
@@ -181,7 +181,10 @@ logBase = Native.Basics.logBase
181181 200 if 200 <= x
182182-}
183183clamp : number -> number -> number -> number
184−clamp = Native.Basics.clamp
184+clamp lo hi n =
185+ if | n < lo -> lo
186+ | n > hi -> hi
187+ | otherwise -> n
185188
186189{-| An approximation of pi. -}
187190pi : Float
@@ -230,11 +233,11 @@ data Order = LT | EQ | GT
230233
231234{-| Given two comparables, returns the smaller one. -}
232235min : comparable -> comparable -> comparable
233−min = Native.Basics.min
236+min a b = if a < b then a else b
234237
235238{-| Given two comparables, returns the larger one. -}
236239max : comparable -> comparable -> comparable
237−max = Native.Basics.max
240+max a b = if a > b then a else b
238241
239242{-| The and operator. True if both inputs are True.
240243This operator short-circuits if the first argument is False.
libraries/Native/Basics.js−7
@@ -12,10 +12,6 @@ Elm.Native.Basics.make = function(elm) {
1212 function rem(a,b) { return a % b; }
1313 var mod = Utils.mod;
1414 function logBase(base,n) { return Math.log(n) / Math.log(base); }
15− function min(a,b) { return Utils.cmp(a,b) < 0 ? a : b; }
16− function max(a,b) { return Utils.cmp(a,b) > 0 ? a : b; }
17− function clamp(lo,hi,n) {
18− return Utils.cmp(n,lo) < 0 ? lo : Utils.cmp(n,hi) > 0 ? hi : n; }
1915 function xor(a,b) { return a !== b; }
2016 function not(b) { return !b; }
2117 function isInfinite(n) { return n === Infinity || n === -Infinity }
@@ -61,9 +57,6 @@ Elm.Native.Basics.make = function(elm) {
6157
6258 sqrt:Math.sqrt,
6359 logBase:F2(logBase),
64− min:F2(min),
65− max:F2(max),
66− clamp:F3(clamp),
6760 compare:Utils.compare,
6861
6962 xor:F2(xor),