on master help sign in
Implement "min", "max", and "clamp" in Elm
f098af5Evan Czaplicki
libraries/Basics.elm+6−3
@@ -181,7 +181,10 @@ logBase = Native.Basics.logBase
181 200 if 200 <= x
181 200 if 200 <= x
182-}
182-}
183clamp : number -> number -> number -> number
183clamp : 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
185
188
186{-| An approximation of pi. -}
189{-| An approximation of pi. -}
187pi : Float
190pi : Float
@@ -230,11 +233,11 @@ data Order = LT | EQ | GT
230
233
231{-| Given two comparables, returns the smaller one. -}
234{-| Given two comparables, returns the smaller one. -}
232min : comparable -> comparable -> comparable
235min : comparable -> comparable -> comparable
233−min = Native.Basics.min
236+min a b = if a < b then a else b
234
237
235{-| Given two comparables, returns the larger one. -}
238{-| Given two comparables, returns the larger one. -}
236max : comparable -> comparable -> comparable
239max : comparable -> comparable -> comparable
237−max = Native.Basics.max
240+max a b = if a > b then a else b
238
241
239{-| The and operator. True if both inputs are True.
242{-| The and operator. True if both inputs are True.
240This operator short-circuits if the first argument is False.
243This operator short-circuits if the first argument is False.
libraries/Native/Basics.js−7
@@ -12,10 +12,6 @@ Elm.Native.Basics.make = function(elm) {
12 function rem(a,b) { return a % b; }
12 function rem(a,b) { return a % b; }
13 var mod = Utils.mod;
13 var mod = Utils.mod;
14 function logBase(base,n) { return Math.log(n) / Math.log(base); }
14 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; }
19 function xor(a,b) { return a !== b; }
15 function xor(a,b) { return a !== b; }
20 function not(b) { return !b; }
16 function not(b) { return !b; }
21 function isInfinite(n) { return n === Infinity || n === -Infinity }
17 function isInfinite(n) { return n === Infinity || n === -Infinity }
@@ -61,9 +57,6 @@ Elm.Native.Basics.make = function(elm) {
61
57
62 sqrt:Math.sqrt,
58 sqrt:Math.sqrt,
63 logBase:F2(logBase),
59 logBase:F2(logBase),
64− min:F2(min),
65− max:F2(max),
66− clamp:F3(clamp),
67 compare:Utils.compare,
60 compare:Utils.compare,
68
61
69 xor:F2(xor),
62 xor:F2(xor),