on master help sign in
Make Basics compile
7968fa5Evan Czaplicki
libraries/Basics.elm+71−10
@@ -1,7 +1,7 @@
1
1
2−module Prelude where
2+module Basics where
3
3
4−import Native.Prelude as N
4+import Native.Basics as Native
5
5
6-- Convert radians to standard Elm angles (radians).
6-- Convert radians to standard Elm angles (radians).
7radians : Float -> Float
7radians : Float -> Float
@@ -9,52 +9,72 @@ radians t = t
9
9
10-- Convert degrees to standard Elm angles (radians).
10-- Convert degrees to standard Elm angles (radians).
11degrees : Float -> Float
11degrees : Float -> Float
12−degrees d = d * Math.PI / 180
12+degrees d = d * Native.pi / 180
13
13
14-- Convert turns to standard Elm angles (radians).
14-- Convert turns to standard Elm angles (radians).
15-- One turn is equal to 360°.
15-- One turn is equal to 360°.
16turns : Float -> Float
16turns : Float -> Float
17−turns r = 2 * Math.PI * r
17+turns r = 2 * Native.pi * r
18
18
19-- Start with polar coordinates (r,θ)
19-- Start with polar coordinates (r,θ)
20-- and get out cartesian coordinates (x,y).
20-- and get out cartesian coordinates (x,y).
21fromPolar : (Float,Float) -> (Float,Float)
21fromPolar : (Float,Float) -> (Float,Float)
22−fromPolar (r,t) = (r * N.cos t, r * N.sin t)
22+fromPolar (r,t) = (r * Native.cos t, r * Native.sin t)
23
23
24-- Start with cartesian coordinates (x,y)
24-- Start with cartesian coordinates (x,y)
25-- and get out polar coordinates (r,θ).
25-- and get out polar coordinates (r,θ).
26toPolar : (Float,Float) -> (Float,Float)
26toPolar : (Float,Float) -> (Float,Float)
27−toPolar (x,y) = (N.sqrt (x^2 + y^2), N.atan2 y x)
27+toPolar (x,y) = (Native.sqrt (x^2 + y^2), Native.atan2 y x)
28
28
29(+) : number -> number -> number
29(+) : number -> number -> number
30+(+) = Native.add
31+
30(-) : number -> number -> number
32(-) : number -> number -> number
33+(-) = Native.sub
34+
31(*) : number -> number -> number
35(*) : number -> number -> number
36+(*) = Native.mul
32
37
33-- Floating point division.
38-- Floating point division.
34(/) : Float -> Float -> Float
39(/) : Float -> Float -> Float
40+(/) = Native.floatDiv
35
41
36-- Integer division, remainder is discarded.
42-- Integer division, remainder is discarded.
37div : Int -> Int -> Int
43div : Int -> Int -> Int
44+div = Native.div
38
45
39-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
46-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
40rem : Int -> Int -> Int
47rem : Int -> Int -> Int
48+rem = Native.rem
41
49
42-- Perform modular arithmetic: ``7 `mod` 2 == 1``
50-- Perform modular arithmetic: ``7 `mod` 2 == 1``
43mod : Int -> Int -> Int
51mod : Int -> Int -> Int
52+mod = Native.mod
44
53
45-- Exponentiation: `3^2 == 9`
54-- Exponentiation: `3^2 == 9`
46(^) : number -> number -> number
55(^) : number -> number -> number
56+(^) = Native.exp
47
57
48cos : Float -> Float
58cos : Float -> Float
59+cos = Native.cos
60+
49sin : Float -> Float
61sin : Float -> Float
62+sin = Native.sin
63+
50tan : Float -> Float
64tan : Float -> Float
65+tan = Native.tan
66+
51acos : Float -> Float
67acos : Float -> Float
68+acos = Native.acos
69+
52asin : Float -> Float
70asin : Float -> Float
71+asin = Native.asin
53
72
54-- You probably do not want to use this. Because it takes `(y/x)` as the argument
73-- You probably do not want to use this. Because it takes `(y/x)` as the argument
55-- there is no way to know where the negative signs come from so the resulting
74-- there is no way to know where the negative signs come from so the resulting
56-- angle is always between π/2 and -π/2 (in quadrants I and IV).
75-- angle is always between π/2 and -π/2 (in quadrants I and IV).
57atan : Float -> Float
76atan : Float -> Float
77+atan = Native.atan
58
78
59-- This helps you find the angle of a cartesian coordinate.
79-- This helps you find the angle of a cartesian coordinate.
60-- You will almost certainly want to use this instead of `atan`.
80-- You will almost certainly want to use this instead of `atan`.
@@ -62,21 +82,27 @@ atan : Float -> Float
62-- quadrant the angle should really be in. The result will be between
82-- quadrant the angle should really be in. The result will be between
63-- π and -π, giving you the full range of angles.
83-- π and -π, giving you the full range of angles.
64atan2 : Float -> Float -> Float
84atan2 : Float -> Float -> Float
85+atan2 = Native.atan2
65
86
66-- Take the square root of a number.
87-- Take the square root of a number.
67sqrt : number -> number
88sqrt : number -> number
89+sqrt = Native.sqrt
68
90
69-- Take the absolute value of a number.
91-- Take the absolute value of a number.
70abs : number -> number
92abs : number -> number
93+abs = Native.abs
71
94
72-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
95-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
73logBase : number -> number -> number
96logBase : number -> number -> number
97+logBase = Native.logBase
74
98
75−-- Given two numbers, returns the smaller one.
99+-- Given two comparables, returns the smaller one.
76−min : number -> number -> number
100+min : comparable -> comparable -> comparable
101+min = Native.min
77
102
78−-- Given two numbers, returns the larger one.
103+-- Given two comparables, returns the larger one.
79−max : number -> number -> number
104+max : comparable -> comparable -> comparable
105+max = Native.max
80
106
81-- Clamps a number within a given range. With the expression `clamp 100 200 x`
107-- Clamps a number within a given range. With the expression `clamp 100 200 x`
82-- the results are as follows:
108-- the results are as follows:
@@ -85,26 +111,40 @@ max : number -> number -> number
85-- * ` x if 100 <= x < 200`
111-- * ` x if 100 <= x < 200`
86-- * `200 if 200 <= x`
112-- * `200 if 200 <= x`
87clamp : number -> number -> number -> number
113clamp : number -> number -> number -> number
114+clamp = Native.clamp
88
115
89-- An approximation of pi.
116-- An approximation of pi.
90pi : Float
117pi : Float
118+pi = Native.pi
91
119
92-- An approximation of e.
120-- An approximation of e.
93e : Float
121e : Float
122+e = Native.e
94
123
95-- Compare any two values for structural equality. Functions cannot be compared.
124-- Compare any two values for structural equality. Functions cannot be compared.
96(==) : a -> a -> Bool
125(==) : a -> a -> Bool
126+(==) = Native.eq
127+
97(/=) : a -> a -> Bool
128(/=) : a -> a -> Bool
129+(/=) = Native.neq
98
130
99(<) : comparable -> comparable -> Bool
131(<) : comparable -> comparable -> Bool
132+(<) = Native.lt
133+
100(>) : comparable -> comparable -> Bool
134(>) : comparable -> comparable -> Bool
135+(>) = Native.gt
136+
101(<=) : comparable -> comparable -> Bool
137(<=) : comparable -> comparable -> Bool
138+(<=) = Native.le
139+
102(>=) : comparable -> comparable -> Bool
140(>=) : comparable -> comparable -> Bool
141+(>=) = Native.ge
103
142
104-- Compare any two comparable values. Comparable values include `String`, `Char`,
143-- Compare any two comparable values. Comparable values include `String`, `Char`,
105-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
144-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
106-- These are also the only values that work as `Dict` keys or `Set` members.
145-- These are also the only values that work as `Dict` keys or `Set` members.
107compare : comparable -> comparable -> Order
146compare : comparable -> comparable -> Order
147+compare = Native.compare
108
148
109-- Represents the relative ordering of two things.
149-- Represents the relative ordering of two things.
110-- The relations are less than, equal to, and greater than.
150-- The relations are less than, equal to, and greater than.
@@ -112,15 +152,19 @@ data Order = LT | EQ | GT
112
152
113-- The and operator. True if both inputs are True.
153-- The and operator. True if both inputs are True.
114(&&) : Bool -> Bool -> Bool
154(&&) : Bool -> Bool -> Bool
155+(&&) = Native.and
115
156
116-- The or operator. True if one or both inputs are True.
157-- The or operator. True if one or both inputs are True.
117(||) : Bool -> Bool -> Bool
158(||) : Bool -> Bool -> Bool
159+(||) = Native.or
118
160
119-- The exclusive-or operator. True if exactly one input is True.
161-- The exclusive-or operator. True if exactly one input is True.
120xor : Bool -> Bool -> Bool
162xor : Bool -> Bool -> Bool
163+xor = Native.xor
121
164
122-- Negate a boolean value: `(not True == False)` and `(not False == True)`
165-- Negate a boolean value: `(not True == False)` and `(not False == True)`
123not : Bool -> Bool
166not : Bool -> Bool
167+not = Native.not
124
168
125-- Equal to true. Useful as the last case of a multi-way-if.
169-- Equal to true. Useful as the last case of a multi-way-if.
126otherwise : Bool
170otherwise : Bool
@@ -131,32 +175,41 @@ otherwise = True
131
175
132-- Round a number to the nearest integer.
176-- Round a number to the nearest integer.
133round : Float -> Int
177round : Float -> Int
178+round = Native.round
134
179
135-- Truncate a decimal number, rounding towards zero.
180-- Truncate a decimal number, rounding towards zero.
136truncate : Float -> Int
181truncate : Float -> Int
182+truncate = Native.truncate
137
183
138-- Floor function, rounding down.
184-- Floor function, rounding down.
139floor : Float -> Int
185floor : Float -> Int
186+floor = Native.floor
140
187
141-- Ceiling function, rounding up.
188-- Ceiling function, rounding up.
142ceiling : Float -> Int
189ceiling : Float -> Int
190+ceiling = Native.ceiling
143
191
144-- Convert an integer into a float.
192-- Convert an integer into a float.
145toFloat : Int -> Float
193toFloat : Int -> Float
194+toFloat = Native.toFloat
146
195
147-- Convert almost any value to its string representation.
196-- Convert almost any value to its string representation.
148show : a -> String
197show : a -> String
198+show = Native.show
149
199
150-- Read an integer from a string
200-- Read an integer from a string
151readInt : String -> Maybe Int
201readInt : String -> Maybe Int
202+readInt = Native.readInt
152
203
153-- Read a float from a string.
204-- Read a float from a string.
154readFloat : String -> Maybe Float
205readFloat : String -> Maybe Float
206+readFloat = Native.readFloat
155
207
156-- Function Helpers
208-- Function Helpers
157
209
158-- Function composition: `(f . g == (\\x -> f (g x)))`
210-- Function composition: `(f . g == (\\x -> f (g x)))`
159(.) : (b -> c) -> (a -> b) -> (a -> c)
211(.) : (b -> c) -> (a -> b) -> (a -> c)
212+(.) f g x = f (g x)
160
213
161-- Forward function application `x |> f == f x`. This function is useful
214-- Forward function application `x |> f == f x`. This function is useful
162-- for avoiding parenthesis and writing code in a more natural way.
215-- for avoiding parenthesis and writing code in a more natural way.
@@ -170,6 +223,7 @@ readFloat : String -> Maybe Float
170-- |> move (10,10)
223-- |> move (10,10)
171-- |> scale 2
224-- |> scale 2
172(|>) : a -> (a -> b) -> b
225(|>) : a -> (a -> b) -> b
226+x |> f = f x
173
227
174-- Function application `f <| x == f x`. This function is useful for avoiding
228-- Function application `f <| x == f x`. This function is useful for avoiding
175-- parenthesis. Consider the following code to create a text element:
229-- parenthesis. Consider the following code to create a text element:
@@ -180,23 +234,30 @@ readFloat : String -> Maybe Float
180--
234--
181-- text . monospace <| toText "code"
235-- text . monospace <| toText "code"
182(<|) : (a -> b) -> a -> b
236(<|) : (a -> b) -> a -> b
237+f <| x = f x
183
238
184-- Given a value, returns exactly the same value.
239-- Given a value, returns exactly the same value.
185id : a -> a
240id : a -> a
241+id x = x
186
242
187-- Given a 2-tuple, returns the first value.
243-- Given a 2-tuple, returns the first value.
188fst : (a,b) -> a
244fst : (a,b) -> a
245+fst = Native.fst
189
246
190-- Given a 2-tuple, returns the second value.
247-- Given a 2-tuple, returns the second value.
191snd : (a,b) -> b
248snd : (a,b) -> b
249+snd = Native.snd
192
250
193-- Flips the order of the first two arguments to a function.
251-- Flips the order of the first two arguments to a function.
194flip : (a -> b -> c) -> (b -> a -> c)
252flip : (a -> b -> c) -> (b -> a -> c)
253+flip f b a = f a b
195
254
196-- Change how arguments are passed to a function. This splits paired arguments
255-- Change how arguments are passed to a function. This splits paired arguments
197-- into two separate arguments.
256-- into two separate arguments.
198curry : ((a,b) -> c) -> a -> b -> c
257curry : ((a,b) -> c) -> a -> b -> c
258+curry = Native.curry
199
259
200-- Change how arguments are passed to a function. This combines two arguments
260-- Change how arguments are passed to a function. This combines two arguments
201-- into a sigle pair.
261-- into a sigle pair.
202uncurry : (a -> b -> c) -> (a,b) -> c
262uncurry : (a -> b -> c) -> (a,b) -> c
263+uncurry = Native.uncurry
libraries/Native/Basics.js+9−24
@@ -1,7 +1,7 @@
1
1
2−Elm.Native.Prelude = function(elm) {
2+Elm.Native.Basics = function(elm) {
3 'use strict';
3 'use strict';
4− if (elm.Native.Prelude) return elm.Native.Prelude;
4+ if (elm.Native.Basics) return elm.Native.Basics;
5
5
6 var JS = Elm.JavaScript(elm);
6 var JS = Elm.JavaScript(elm);
7 var Maybe = Elm.Maybe(elm);
7 var Maybe = Elm.Maybe(elm);
@@ -13,16 +13,15 @@ Elm.Native.Prelude = function(elm) {
13 var mod = Utils.mod;
13 var mod = Utils.mod;
14 function abs(x) { return x < 0 ? -x : x; }
14 function abs(x) { return x < 0 ? -x : x; }
15 function logBase(base,n) { return Math.log(n) / Math.log(base); }
15 function logBase(base,n) { return Math.log(n) / Math.log(base); }
16− function min(a,b) { return a < b ? a : b; }
16+ function min(a,b) { return Utils.cmp(a,b) < 0 ? a : b; }
17− function max(a,b) { return a > b ? a : b; }
17+ function max(a,b) { return Utils.cmp(a,b) > 0 ? a : b; }
18− function clamp(lo,hi,n) { return n < lo ? lo : n > hi ? hi : n; }
18+ function clamp(lo,hi,n) {
19+ return Utils.cmp(n,lo) < 0 ? lo : Utils.cmp(n,hi) > 0 ? hi : n; }
19 function xor(a,b) { return a !== b; }
20 function xor(a,b) { return a !== b; }
20 function not(b) { return !b; }
21 function not(b) { return !b; }
21
22
22 function truncate(n) { return n|0; }
23 function truncate(n) { return n|0; }
23
24
24− function id(n) { return n; }
25− function flip(f,a,b) { return A2(f,b,a); }
26 function curry(f,a,b) { return f(Utils.Tuple2(a,b)); }
25 function curry(f,a,b) { return f(Utils.Tuple2(a,b)); }
27 function uncurry(f,v) { return A2(f,v._0,v._1); }
26 function uncurry(f,v) { return A2(f,v._0,v._1); }
28 function fst(t) { return t._0; }
27 function fst(t) { return t._0; }
@@ -64,7 +63,7 @@ Elm.Native.Prelude = function(elm) {
64 return Maybe.Just(parseFloat(s));
63 return Maybe.Just(parseFloat(s));
65 }
64 }
66
65
67− var prelude = {
66+ var basics = {
68 div:F2(div),
67 div:F2(div),
69 rem:F2(rem),
68 rem:F2(rem),
70 mod:mod,
69 mod:mod,
@@ -89,35 +88,21 @@ Elm.Native.Prelude = function(elm) {
89
88
90 xor:F2(xor),
89 xor:F2(xor),
91 not:not,
90 not:not,
92− otherwise:true,
93
91
94 truncate:truncate,
92 truncate:truncate,
95 ceiling:Math.ceil,
93 ceiling:Math.ceil,
96 floor:Math.floor,
94 floor:Math.floor,
97 round:Math.round,
95 round:Math.round,
98− toFloat:id,
96+ toFloat:function(x) { return x; },
99
97
100 readInt:readInt,
98 readInt:readInt,
101 readFloat:readFloat,
99 readFloat:readFloat,
102
100
103− id:id,
104− flip:F3(flip),
105 curry:F3(curry),
101 curry:F3(curry),
106 uncurry:F2(uncurry),
102 uncurry:F2(uncurry),
107 fst:fst,
103 fst:fst,
108 snd:snd
104 snd:snd
109 };
105 };
110
106
111− function add(Module) {
112− var M = Module(elm);
113− for (var k in M) { prelude[k] = M[k]; }
114− }
115− add(Elm.Native.Show);
116− add(Elm.Signal);
117− add(Elm.List);
118− add(Elm.Maybe);
119− add(Elm.Time);
120− add(Elm.Graphics.Element);
121−
122− return elm.Native.Prelude = prelude;
107+ return elm.Native.Basics = basics;
123};
108};
libraries/Native/Utils.js+2−2
@@ -19,7 +19,7 @@ Elm.Native.Utils = function(elm) {
19 return x === y;
19 return x === y;
20 }
20 }
21
21
22− var EQ = 0, LT = 1, GT = 2, ord = ['EQ','LT','GT'];
22+ var EQ = 0, LT = -1, GT = 1, ord = ['EQ','LT','GT'];
23 function compare(x,y) { return { ctor: ord[cmp(x,y)] } }
23 function compare(x,y) { return { ctor: ord[cmp(x,y)] } }
24 function cmp(x,y) {
24 function cmp(x,y) {
25 var ord;
25 var ord;
@@ -140,7 +140,7 @@ Elm.Native.Utils = function(elm) {
140
140
141 return elm.Native.Utils = {
141 return elm.Native.Utils = {
142 eq:eq,
142 eq:eq,
143− cmp:compare,
143+ cmp:cmp,
144 compare:F2(compare),
144 compare:F2(compare),
145 Tuple0:Tuple0,
145 Tuple0:Tuple0,
146 Tuple2:Tuple2,
146 Tuple2:Tuple2,