2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
module Prelude where
|
|
|
|
|
2013-03-17 05:24:18 +00:00
|
|
|
import Native.Prelude as N
|
|
|
|
|
2013-05-16 20:10:50 +00:00
|
|
|
-- Convert radians to standard Elm angles (radians).
|
2013-03-16 08:21:08 +00:00
|
|
|
radians : Float -> Float
|
|
|
|
radians t = t
|
|
|
|
|
2013-05-16 20:10:50 +00:00
|
|
|
-- Convert degrees to standard Elm angles (radians).
|
2013-03-16 08:21:08 +00:00
|
|
|
degrees : Float -> Float
|
2013-03-23 20:20:30 +00:00
|
|
|
degrees d = d * Math.PI / 180
|
2013-03-16 08:21:08 +00:00
|
|
|
|
2013-05-16 20:10:50 +00:00
|
|
|
-- Convert turns to standard Elm angles (radians).
|
|
|
|
-- One turn is equal to 360°.
|
2013-03-23 20:20:30 +00:00
|
|
|
turns : Float -> Float
|
|
|
|
turns r = 2 * Math.PI * r
|
2013-03-16 08:21:08 +00:00
|
|
|
|
2013-05-16 20:10:50 +00:00
|
|
|
-- Start with polar coordinates (r,θ)
|
|
|
|
-- and get out cartesian coordinates (x,y).
|
|
|
|
fromPolar : (Float,Float) -> (Float,Float)
|
|
|
|
fromPolar (r,t) = (r * N.cos t, r * N.sin t)
|
|
|
|
|
|
|
|
-- Start with cartesian coordinates (x,y)
|
|
|
|
-- and get out polar coordinates (r,θ).
|
|
|
|
toPolar : (Float,Float) -> (Float,Float)
|
|
|
|
toPolar (x,y) = (N.sqrt (x^2 + y^2), N.atan2 y x)
|
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
(+) : Number a -> Number a -> Number a
|
|
|
|
(-) : Number a -> Number a -> Number a
|
|
|
|
(*) : Number a -> Number a -> Number a
|
2013-05-16 20:10:50 +00:00
|
|
|
|
|
|
|
-- Floating point division.
|
2013-04-08 08:49:44 +00:00
|
|
|
(/) : Float -> Float -> Float
|
|
|
|
|
2013-03-16 08:21:08 +00:00
|
|
|
-- Integer division, remainder is discarded.
|
|
|
|
div : Int -> Int -> Int
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
|
2013-03-16 08:21:08 +00:00
|
|
|
rem : Int -> Int -> Int
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Perform modular arithmetic: ``7 `mod` 2 == 1``
|
2013-03-16 08:21:08 +00:00
|
|
|
mod : Int -> Int -> Int
|
|
|
|
|
2013-05-16 20:10:50 +00:00
|
|
|
-- Exponentiation: `3^2 == 9`
|
|
|
|
(^) : Number a -> Number a -> Number a
|
|
|
|
|
2013-03-16 08:21:08 +00:00
|
|
|
cos : Float -> Float
|
|
|
|
sin : Float -> Float
|
|
|
|
tan : Float -> Float
|
|
|
|
acos : Float -> Float
|
|
|
|
asin : Float -> Float
|
2013-05-17 10:49:31 +00:00
|
|
|
|
|
|
|
-- You probably do not want to use this. Because it takes `(y/x)` as the argument
|
|
|
|
-- there is no way to know where the negative signs come from so the resulting
|
|
|
|
-- angle is always between π/2 and -π/2 (in quadrants I and IV).
|
2013-03-16 08:21:08 +00:00
|
|
|
atan : Float -> Float
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- This helps you find the angle of a cartesian coordinate.
|
|
|
|
-- You will almost certainly want to use this instead of `atan`.
|
|
|
|
-- So `atan2 y x` computes *atan(y/x)* but also keeps track of which
|
|
|
|
-- quadrant the angle should really be in. The result will be between
|
|
|
|
-- π and -π, giving you the full range of angles.
|
2013-03-16 08:21:08 +00:00
|
|
|
atan2 : Float -> Float -> Float
|
|
|
|
|
|
|
|
-- Take the square root of a number.
|
2013-04-10 05:15:06 +00:00
|
|
|
sqrt : Number a -> Number a
|
2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
-- Take the absolute value of a number.
|
2013-04-10 05:15:06 +00:00
|
|
|
abs : Number a -> Number a
|
2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
|
2013-04-10 05:15:06 +00:00
|
|
|
logBase : Number a -> Number a -> Number a
|
2013-03-16 08:21:08 +00:00
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Given two numbers, returns the smaller one.
|
2013-04-10 05:15:06 +00:00
|
|
|
min : Number a -> Number a -> Number a
|
2013-05-17 10:49:31 +00:00
|
|
|
|
|
|
|
-- Given two numbers, returns the larger one.
|
2013-04-10 05:15:06 +00:00
|
|
|
max : Number a -> Number a -> Number a
|
2013-03-16 08:21:08 +00:00
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Clamps a number within a given range. With the expression `clamp 100 200 x`
|
|
|
|
-- the results are as follows:
|
|
|
|
--
|
|
|
|
-- * `100 if x < 100`
|
|
|
|
-- * ` x if 100 <= x < 200`
|
|
|
|
-- * `200 if 200 <= x`
|
2013-04-10 05:15:06 +00:00
|
|
|
clamp : Number a -> Number a -> Number a -> Number a
|
2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
-- An approximation of pi.
|
|
|
|
pi : Float
|
|
|
|
|
|
|
|
-- An approximation of e.
|
|
|
|
e : Float
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Compare any two values for structural equality. Functions cannot be compared.
|
2013-04-28 12:01:41 +00:00
|
|
|
(==) : a -> a -> Bool
|
|
|
|
(/=) : a -> a -> Bool
|
2013-03-16 08:21:08 +00:00
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
(<) : Comparable a -> Comparable a -> Bool
|
|
|
|
(>) : Comparable a -> Comparable a -> Bool
|
|
|
|
(<=) : Comparable a -> Comparable a -> Bool
|
|
|
|
(>=) : Comparable a -> Comparable a -> Bool
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Compare any two comparable values. Comparable values include `String`, `Char`,
|
|
|
|
-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
|
|
|
|
-- These are also the only values that work as `Dict` keys or `Set` members.
|
2013-04-10 05:15:06 +00:00
|
|
|
compare : Comparable a -> Comparable a -> Order
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Represents the relative ordering of two things.
|
|
|
|
-- The relations are less than, equal to, and greater than.
|
2013-04-10 05:15:06 +00:00
|
|
|
data Order = LT | EQ | GT
|
2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
-- The and operator. True if both inputs are True.
|
|
|
|
(&&) : Bool -> Bool -> Bool
|
|
|
|
|
|
|
|
-- The or operator. True if one or both inputs are True.
|
|
|
|
(||) : Bool -> Bool -> Bool
|
|
|
|
|
|
|
|
-- The exclusive-or operator. True if exactly one input is True.
|
|
|
|
xor : Bool -> Bool -> Bool
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Negate a boolean value: `(not True == False)` and `(not False == True)`
|
2013-03-16 08:21:08 +00:00
|
|
|
not : Bool -> Bool
|
|
|
|
|
|
|
|
-- Equal to true. Useful as the last case of a multi-way-if.
|
|
|
|
otherwise : Bool
|
|
|
|
otherwise = True
|
|
|
|
|
|
|
|
|
|
|
|
-- Conversions
|
|
|
|
|
|
|
|
-- Round a number to the nearest integer.
|
|
|
|
round : Float -> Int
|
|
|
|
|
|
|
|
-- Truncate a decimal number, rounding towards zero.
|
|
|
|
truncate : Float -> Int
|
|
|
|
|
|
|
|
-- Floor function, rounding down.
|
|
|
|
floor : Float -> Int
|
|
|
|
|
|
|
|
-- Ceiling function, rounding up.
|
|
|
|
ceiling : Float -> Int
|
|
|
|
|
|
|
|
-- Convert an integer into a float.
|
|
|
|
toFloat : Int -> Float
|
|
|
|
|
|
|
|
-- Convert almost any value to its string representation.
|
|
|
|
show : a -> String
|
|
|
|
|
|
|
|
-- Read an integer from a string
|
|
|
|
readInt : String -> Maybe Int
|
|
|
|
|
|
|
|
-- Read a float from a string.
|
|
|
|
readFloat : String -> Maybe Float
|
|
|
|
|
|
|
|
-- Function Helpers
|
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Function composition: `(f . g == (\\x -> f (g x)))`
|
2013-03-16 08:21:08 +00:00
|
|
|
(.) : (b -> c) -> (a -> b) -> (a -> c)
|
|
|
|
|
2013-04-25 05:26:01 +00:00
|
|
|
-- Forward function application `x |> f == f x`. This function is useful
|
|
|
|
-- for avoiding parenthesis and writing code in a more natural way.
|
|
|
|
-- Consider the following code to create a pentagon:
|
|
|
|
--
|
2013-05-17 10:49:31 +00:00
|
|
|
-- scale 2 (move (10,10) (filled blue (ngon 5 30)))
|
2013-04-25 05:26:01 +00:00
|
|
|
--
|
|
|
|
-- This can also be written as:
|
|
|
|
--
|
2013-05-17 10:49:31 +00:00
|
|
|
-- ngon 5 30 |> filled blue
|
|
|
|
-- |> move (10,10)
|
|
|
|
-- |> scale 2
|
2013-04-25 05:26:01 +00:00
|
|
|
(|>) : a -> (a -> b) -> b
|
|
|
|
|
|
|
|
-- Function application `f <| x == f x`. This function is useful for avoiding
|
2013-03-16 08:21:08 +00:00
|
|
|
-- parenthesis. Consider the following code to create a text element:
|
2013-04-25 05:26:01 +00:00
|
|
|
--
|
2013-05-17 10:49:31 +00:00
|
|
|
-- text (monospace (toText "code"))
|
2013-04-25 05:26:01 +00:00
|
|
|
--
|
2013-03-16 08:21:08 +00:00
|
|
|
-- This can also be written as:
|
2013-04-25 05:26:01 +00:00
|
|
|
--
|
2013-05-17 10:49:31 +00:00
|
|
|
-- text . monospace <| toText "code"
|
2013-04-25 05:26:01 +00:00
|
|
|
(<|) : (a -> b) -> a -> b
|
2013-03-16 08:21:08 +00:00
|
|
|
|
|
|
|
-- Given a value, returns exactly the same value.
|
|
|
|
id : a -> a
|
|
|
|
|
|
|
|
-- Given a 2-tuple, returns the first value.
|
|
|
|
fst : (a,b) -> a
|
|
|
|
|
|
|
|
-- Given a 2-tuple, returns the second value.
|
|
|
|
snd : (a,b) -> b
|
|
|
|
|
|
|
|
-- Flips the order of the first two arguments to a function.
|
|
|
|
flip : (a -> b -> c) -> (b -> a -> c)
|
|
|
|
|
|
|
|
-- Change how arguments are passed to a function. This splits paired arguments
|
|
|
|
-- into two separate arguments.
|
|
|
|
curry : ((a,b) -> c) -> a -> b -> c
|
|
|
|
|
|
|
|
-- Change how arguments are passed to a function. This combines two arguments
|
|
|
|
-- into a sigle pair.
|
|
|
|
uncurry : (a -> b -> c) -> (a,b) -> c
|