on c19ccfe help sign in
elk/libraries/Basics.elm
263 lines 6.8 kB history raw
2module Basics where
4import Native.Basics as Native
6-- Convert radians to standard Elm angles (radians).
7radians : Float -> Float
8radians t = t
10-- Convert degrees to standard Elm angles (radians).
11degrees : Float -> Float
12degrees d = d * Native.pi / 180
14-- Convert turns to standard Elm angles (radians).
15-- One turn is equal to 360°.
16turns : Float -> Float
17turns r = 2 * Native.pi * r
19-- Start with polar coordinates (r,θ)
20-- and get out cartesian coordinates (x,y).
21fromPolar : (Float,Float) -> (Float,Float)
22fromPolar (r,t) = (r * Native.cos t, r * Native.sin t)
24-- Start with cartesian coordinates (x,y)
25-- and get out polar coordinates (r,θ).
26toPolar : (Float,Float) -> (Float,Float)
27toPolar (x,y) = (Native.sqrt (x^2 + y^2), Native.atan2 y x)
29(+) : number -> number -> number
30(+) = Native.add
32(-) : number -> number -> number
33(-) = Native.sub
35(*) : number -> number -> number
36(*) = Native.mul
38-- Floating point division.
39(/) : Float -> Float -> Float
40(/) = Native.floatDiv
42-- Integer division, remainder is discarded.
43div : Int -> Int -> Int
44div = Native.div
46-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
47rem : Int -> Int -> Int
48rem = Native.rem
50-- Perform modular arithmetic: ``7 `mod` 2 == 1``
51mod : Int -> Int -> Int
52mod = Native.mod
54-- Exponentiation: `3^2 == 9`
55(^) : number -> number -> number
56(^) = Native.exp
58cos : Float -> Float
59cos = Native.cos
61sin : Float -> Float
62sin = Native.sin
64tan : Float -> Float
65tan = Native.tan
67acos : Float -> Float
68acos = Native.acos
70asin : Float -> Float
71asin = Native.asin
73-- You probably do not want to use this. Because it takes `(y/x)` as the argument
74-- there is no way to know where the negative signs come from so the resulting
75-- angle is always between π/2 and -π/2 (in quadrants I and IV).
76atan : Float -> Float
77atan = Native.atan
79-- This helps you find the angle of a cartesian coordinate.
80-- You will almost certainly want to use this instead of `atan`.
81-- So `atan2 y x` computes *atan(y/x)* but also keeps track of which
82-- quadrant the angle should really be in. The result will be between
83-- π and -π, giving you the full range of angles.
84atan2 : Float -> Float -> Float
85atan2 = Native.atan2
87-- Take the square root of a number.
88sqrt : number -> number
89sqrt = Native.sqrt
91-- Take the absolute value of a number.
92abs : number -> number
93abs = Native.abs
95-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
96logBase : number -> number -> number
97logBase = Native.logBase
99-- Given two comparables, returns the smaller one.
100min : comparable -> comparable -> comparable
101min = Native.min
103-- Given two comparables, returns the larger one.
104max : comparable -> comparable -> comparable
105max = Native.max
107-- Clamps a number within a given range. With the expression `clamp 100 200 x`
108-- the results are as follows:
109--
110-- * `100 if x < 100`
111-- * ` x if 100 <= x < 200`
112-- * `200 if 200 <= x`
113clamp : number -> number -> number -> number
114clamp = Native.clamp
116-- An approximation of pi.
117pi : Float
118pi = Native.pi
120-- An approximation of e.
121e : Float
122e = Native.e
124-- Compare any two values for structural equality. Functions cannot be compared.
125(==) : a -> a -> Bool
126(==) = Native.eq
128(/=) : a -> a -> Bool
129(/=) = Native.neq
131(<) : comparable -> comparable -> Bool
132(<) = Native.lt
134(>) : comparable -> comparable -> Bool
135(>) = Native.gt
137(<=) : comparable -> comparable -> Bool
138(<=) = Native.le
140(>=) : comparable -> comparable -> Bool
141(>=) = Native.ge
143-- Compare any two comparable values. Comparable values include `String`, `Char`,
144-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
145-- These are also the only values that work as `Dict` keys or `Set` members.
146compare : comparable -> comparable -> Order
147compare = Native.compare
149-- Represents the relative ordering of two things.
150-- The relations are less than, equal to, and greater than.
151data Order = LT | EQ | GT
153-- The and operator. True if both inputs are True.
154(&&) : Bool -> Bool -> Bool
155(&&) = Native.and
157-- The or operator. True if one or both inputs are True.
158(||) : Bool -> Bool -> Bool
159(||) = Native.or
161-- The exclusive-or operator. True if exactly one input is True.
162xor : Bool -> Bool -> Bool
163xor = Native.xor
165-- Negate a boolean value: `(not True == False)` and `(not False == True)`
166not : Bool -> Bool
167not = Native.not
169-- Equal to true. Useful as the last case of a multi-way-if.
170otherwise : Bool
171otherwise = True
174-- Conversions
176-- Round a number to the nearest integer.
177round : Float -> Int
178round = Native.round
180-- Truncate a decimal number, rounding towards zero.
181truncate : Float -> Int
182truncate = Native.truncate
184-- Floor function, rounding down.
185floor : Float -> Int
186floor = Native.floor
188-- Ceiling function, rounding up.
189ceiling : Float -> Int
190ceiling = Native.ceiling
192-- Convert an integer into a float.
193toFloat : Int -> Float
194toFloat = Native.toFloat
196-- Convert almost any value to its string representation.
197show : a -> String
198show = Native.show
200-- Read an integer from a string
201readInt : String -> Maybe Int
202readInt = Native.readInt
204-- Read a float from a string.
205readFloat : String -> Maybe Float
206readFloat = Native.readFloat
208-- Function Helpers
210-- Function composition: `(f . g == (\\x -> f (g x)))`
211(.) : (b -> c) -> (a -> b) -> (a -> c)
212(.) f g x = f (g x)
214-- Forward function application `x |> f == f x`. This function is useful
215-- for avoiding parenthesis and writing code in a more natural way.
216-- Consider the following code to create a pentagon:
217--
218-- scale 2 (move (10,10) (filled blue (ngon 5 30)))
219--
220-- This can also be written as:
221--
222-- ngon 5 30 |> filled blue
223-- |> move (10,10)
224-- |> scale 2
225(|>) : a -> (a -> b) -> b
226x |> f = f x
228-- Function application `f <| x == f x`. This function is useful for avoiding
229-- parenthesis. Consider the following code to create a text element:
230--
231-- text (monospace (toText "code"))
232--
233-- This can also be written as:
234--
235-- text . monospace <| toText "code"
236(<|) : (a -> b) -> a -> b
237f <| x = f x
239-- Given a value, returns exactly the same value.
240id : a -> a
241id x = x
243-- Given a 2-tuple, returns the first value.
244fst : (a,b) -> a
245fst = Native.fst
247-- Given a 2-tuple, returns the second value.
248snd : (a,b) -> b
249snd = Native.snd
251-- Flips the order of the first two arguments to a function.
252flip : (a -> b -> c) -> (b -> a -> c)
253flip f b a = f a b
255-- Change how arguments are passed to a function. This splits paired arguments
256-- into two separate arguments.
257curry : ((a,b) -> c) -> a -> b -> c
258curry = Native.curry
260-- Change how arguments are passed to a function. This combines two arguments
261-- into a sigle pair.
262uncurry : (a -> b -> c) -> (a,b) -> c
263uncurry = Native.uncurry