on master help sign in
Add the always function

I want it, I do not want to rename constant, and I think it is a good name.

commit6a382825d8a4cb16d43db9bd6971e00e21348e53 authorEvan Czaplicki <info@elm-lang.org> dateFri 17 Jan 2014, 18:19 (13 years ago) parent996848c
1 file changed+17−1
libraries/Basics.elm+17−1
libraries/Basics.elm+17−1
@@ -43,7 +43,7 @@ which happen to be radians.
4343@docs fst, snd
4444
4545# Higher-Order Helpers
46−@docs id, (<|), (|>), (.), flip, curry, uncurry
46+@docs id, (<|), (|>), (.), always, flip, curry, uncurry
4747
4848-}
4949
@@ -313,6 +313,22 @@ infixl 0 |>
313313id : a -> a
314314id x = x
315315
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+
316332{-| Given a 2-tuple, returns the first value. -}
317333fst : (a,b) -> a
318334fst (a,_) = a