on master help sign in
Add the always function
6a38282Evan Czaplicki
libraries/Basics.elm+17−1
@@ -43,7 +43,7 @@ which happen to be radians.
43@docs fst, snd
43@docs fst, snd
44
44
45# Higher-Order Helpers
45# Higher-Order Helpers
46−@docs id, (<|), (|>), (.), flip, curry, uncurry
46+@docs id, (<|), (|>), (.), always, flip, curry, uncurry
47
47
48-}
48-}
49
49
@@ -313,6 +313,22 @@ infixl 0 |>
313id : a -> a
313id : a -> a
314id x = x
314id x = x
315
315
316+{-| Creates a [constant function](http://en.wikipedia.org/wiki/Constant_function),
317+a function that *always* returns the same value regardless of what input you give.
318+It is defined simply as:
319+
320+ always a b = a
321+
322+It totally ignores the second argument, so `always 42` is a function that always
323+returns in 42. When you are dealing with higher-order functions, this comes in
324+handy more often than you might expect. For example, creating a zeroed out list
325+of length ten would be:
326+
327+ map (always 0) [0..9]
328+-}
329+always : a -> b -> a
330+always a _ = a
331+
316{-| Given a 2-tuple, returns the first value. -}
332{-| Given a 2-tuple, returns the first value. -}
317fst : (a,b) -> a
333fst : (a,b) -> a
318fst (a,_) = a
334fst (a,_) = a