on master help sign in
Make Basics compile
commit7968fa5bffd869ed2f037389f8f7459a74fc8c13 authorEvan Czaplicki <info@elm-lang.org> dateThu 25 Jul 2013, 23:52 (13 years ago) parentc4880ca
3 files changed+82−36
libraries/Basics.elm+71−10 libraries/Native/Basics.js+9−24 libraries/Native/Utils.js+2−2
libraries/Basics.elm+71−10
@@ -1,7 +1,7 @@
11
2−module Prelude where
2+module Basics where
33
4−import Native.Prelude as N
4+import Native.Basics as Native
55
66-- Convert radians to standard Elm angles (radians).
77radians : Float -> Float
@@ -9,52 +9,72 @@ radians t = t
99
1010-- Convert degrees to standard Elm angles (radians).
1111degrees : Float -> Float
12−degrees d = d * Math.PI / 180
12+degrees d = d * Native.pi / 180
1313
1414-- Convert turns to standard Elm angles (radians).
1515-- One turn is equal to 360&deg;.
1616turns : Float -> Float
17−turns r = 2 * Math.PI * r
17+turns r = 2 * Native.pi * r
1818
1919-- Start with polar coordinates (r,&theta;)
2020-- and get out cartesian coordinates (x,y).
2121fromPolar : (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)
2323
2424-- Start with cartesian coordinates (x,y)
2525-- and get out polar coordinates (r,&theta;).
2626toPolar : (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)
2828
2929(+) : number -> number -> number
30+(+) = Native.add
31+
3032(-) : number -> number -> number
33+(-) = Native.sub
34+
3135(*) : number -> number -> number
36+(*) = Native.mul
3237
3338-- Floating point division.
3439(/) : Float -> Float -> Float
40+(/) = Native.floatDiv
3541
3642-- Integer division, remainder is discarded.
3743div : Int -> Int -> Int
44+div = Native.div
3845
3946-- Finds the remainder after dividing one number by another: ``4 `rem` 3 == 1``
4047rem : Int -> Int -> Int
48+rem = Native.rem
4149
4250-- Perform modular arithmetic: ``7 `mod` 2 == 1``
4351mod : Int -> Int -> Int
52+mod = Native.mod
4453
4554-- Exponentiation: `3^2 == 9`
4655(^) : number -> number -> number
56+(^) = Native.exp
4757
4858cos : Float -> Float
59+cos = Native.cos
60+
4961sin : Float -> Float
62+sin = Native.sin
63+
5064tan : Float -> Float
65+tan = Native.tan
66+
5167acos : Float -> Float
68+acos = Native.acos
69+
5270asin : Float -> Float
71+asin = Native.asin
5372
5473-- You probably do not want to use this. Because it takes `(y/x)` as the argument
5574-- there is no way to know where the negative signs come from so the resulting
5675-- angle is always between &pi;/2 and -&pi;/2 (in quadrants I and IV).
5776atan : Float -> Float
77+atan = Native.atan
5878
5979-- This helps you find the angle of a cartesian coordinate.
6080-- You will almost certainly want to use this instead of `atan`.
@@ -62,21 +82,27 @@ atan : Float -> Float
6282-- quadrant the angle should really be in. The result will be between
6383-- &pi; and -&pi;, giving you the full range of angles.
6484atan2 : Float -> Float -> Float
85+atan2 = Native.atan2
6586
6687-- Take the square root of a number.
6788sqrt : number -> number
89+sqrt = Native.sqrt
6890
6991-- Take the absolute value of a number.
7092abs : number -> number
93+abs = Native.abs
7194
7295-- Calculate the logarithm of a number with a given base: `logBase 10 100 == 2`
7396logBase : number -> number -> number
97+logBase = Native.logBase
7498
75−-- Given two numbers, returns the smaller one.
76−min : number -> number -> number
99+-- Given two comparables, returns the smaller one.
100+min : comparable -> comparable -> comparable
101+min = Native.min
77102
78−-- Given two numbers, returns the larger one.
79−max : number -> number -> number
103+-- Given two comparables, returns the larger one.
104+max : comparable -> comparable -> comparable
105+max = Native.max
80106
81107-- Clamps a number within a given range. With the expression `clamp 100 200 x`
82108-- the results are as follows:
@@ -85,26 +111,40 @@ max : number -> number -> number
85111-- * ` x if 100 <= x < 200`
86112-- * `200 if 200 <= x`
87113clamp : number -> number -> number -> number
114+clamp = Native.clamp
88115
89116-- An approximation of pi.
90117pi : Float
118+pi = Native.pi
91119
92120-- An approximation of e.
93121e : Float
122+e = Native.e
94123
95124-- Compare any two values for structural equality. Functions cannot be compared.
96125(==) : a -> a -> Bool
126+(==) = Native.eq
127+
97128(/=) : a -> a -> Bool
129+(/=) = Native.neq
98130
99131(<) : comparable -> comparable -> Bool
132+(<) = Native.lt
133+
100134(>) : comparable -> comparable -> Bool
135+(>) = Native.gt
136+
101137(<=) : comparable -> comparable -> Bool
138+(<=) = Native.le
139+
102140(>=) : comparable -> comparable -> Bool
141+(>=) = Native.ge
103142
104143-- Compare any two comparable values. Comparable values include `String`, `Char`,
105144-- `Int`, `Float`, `Time`, or a list or tuple containing comparable values.
106145-- These are also the only values that work as `Dict` keys or `Set` members.
107146compare : comparable -> comparable -> Order
147+compare = Native.compare
108148
109149-- Represents the relative ordering of two things.
110150-- The relations are less than, equal to, and greater than.
@@ -112,15 +152,19 @@ data Order = LT | EQ | GT
112152
113153-- The and operator. True if both inputs are True.
114154(&&) : Bool -> Bool -> Bool
155+(&&) = Native.and
115156
116157-- The or operator. True if one or both inputs are True.
117158(||) : Bool -> Bool -> Bool
159+(||) = Native.or
118160
119161-- The exclusive-or operator. True if exactly one input is True.
120162xor : Bool -> Bool -> Bool
163+xor = Native.xor
121164
122165-- Negate a boolean value: `(not True == False)` and `(not False == True)`
123166not : Bool -> Bool
167+not = Native.not
124168
125169-- Equal to true. Useful as the last case of a multi-way-if.
126170otherwise : Bool
@@ -131,32 +175,41 @@ otherwise = True
131175
132176-- Round a number to the nearest integer.
133177round : Float -> Int
178+round = Native.round
134179
135180-- Truncate a decimal number, rounding towards zero.
136181truncate : Float -> Int
182+truncate = Native.truncate
137183
138184-- Floor function, rounding down.
139185floor : Float -> Int
186+floor = Native.floor
140187
141188-- Ceiling function, rounding up.
142189ceiling : Float -> Int
190+ceiling = Native.ceiling
143191
144192-- Convert an integer into a float.
145193toFloat : Int -> Float
194+toFloat = Native.toFloat
146195
147196-- Convert almost any value to its string representation.
148197show : a -> String
198+show = Native.show
149199
150200-- Read an integer from a string
151201readInt : String -> Maybe Int
202+readInt = Native.readInt
152203
153204-- Read a float from a string.
154205readFloat : String -> Maybe Float
206+readFloat = Native.readFloat
155207
156208-- Function Helpers
157209
158210-- Function composition: `(f . g == (\\x -> f (g x)))`
159211(.) : (b -> c) -> (a -> b) -> (a -> c)
212+(.) f g x = f (g x)
160213
161214-- Forward function application `x |> f == f x`. This function is useful
162215-- for avoiding parenthesis and writing code in a more natural way.
@@ -170,6 +223,7 @@ readFloat : String -> Maybe Float
170223-- |> move (10,10)
171224-- |> scale 2
172225(|>) : a -> (a -> b) -> b
226+x |> f = f x
173227
174228-- Function application `f <| x == f x`. This function is useful for avoiding
175229-- parenthesis. Consider the following code to create a text element:
@@ -180,23 +234,30 @@ readFloat : String -> Maybe Float
180234--
181235-- text . monospace <| toText "code"
182236(<|) : (a -> b) -> a -> b
237+f <| x = f x
183238
184239-- Given a value, returns exactly the same value.
185240id : a -> a
241+id x = x
186242
187243-- Given a 2-tuple, returns the first value.
188244fst : (a,b) -> a
245+fst = Native.fst
189246
190247-- Given a 2-tuple, returns the second value.
191248snd : (a,b) -> b
249+snd = Native.snd
192250
193251-- Flips the order of the first two arguments to a function.
194252flip : (a -> b -> c) -> (b -> a -> c)
253+flip f b a = f a b
195254
196255-- Change how arguments are passed to a function. This splits paired arguments
197256-- into two separate arguments.
198257curry : ((a,b) -> c) -> a -> b -> c
258+curry = Native.curry
199259
200260-- Change how arguments are passed to a function. This combines two arguments
201261-- into a sigle pair.
202262uncurry : (a -> b -> c) -> (a,b) -> c
263+uncurry = Native.uncurry
libraries/Native/Basics.js+9−24
@@ -1,7 +1,7 @@
11
2−Elm.Native.Prelude = function(elm) {
2+Elm.Native.Basics = function(elm) {
33 'use strict';
4− if (elm.Native.Prelude) return elm.Native.Prelude;
4+ if (elm.Native.Basics) return elm.Native.Basics;
55
66 var JS = Elm.JavaScript(elm);
77 var Maybe = Elm.Maybe(elm);
@@ -13,16 +13,15 @@ Elm.Native.Prelude = function(elm) {
1313 var mod = Utils.mod;
1414 function abs(x) { return x < 0 ? -x : x; }
1515 function logBase(base,n) { return Math.log(n) / Math.log(base); }
16− function min(a,b) { return a < b ? a : b; }
17− function max(a,b) { return a > b ? a : b; }
18− function clamp(lo,hi,n) { return n < lo ? lo : n > hi ? hi : n; }
16+ function min(a,b) { return Utils.cmp(a,b) < 0 ? a : b; }
17+ function max(a,b) { return Utils.cmp(a,b) > 0 ? a : b; }
18+ function clamp(lo,hi,n) {
19+ return Utils.cmp(n,lo) < 0 ? lo : Utils.cmp(n,hi) > 0 ? hi : n; }
1920 function xor(a,b) { return a !== b; }
2021 function not(b) { return !b; }
2122
2223 function truncate(n) { return n|0; }
2324
24− function id(n) { return n; }
25− function flip(f,a,b) { return A2(f,b,a); }
2625 function curry(f,a,b) { return f(Utils.Tuple2(a,b)); }
2726 function uncurry(f,v) { return A2(f,v._0,v._1); }
2827 function fst(t) { return t._0; }
@@ -64,7 +63,7 @@ Elm.Native.Prelude = function(elm) {
6463 return Maybe.Just(parseFloat(s));
6564 }
6665
67− var prelude = {
66+ var basics = {
6867 div:F2(div),
6968 rem:F2(rem),
7069 mod:mod,
@@ -89,35 +88,21 @@ Elm.Native.Prelude = function(elm) {
8988
9089 xor:F2(xor),
9190 not:not,
92− otherwise:true,
9391
9492 truncate:truncate,
9593 ceiling:Math.ceil,
9694 floor:Math.floor,
9795 round:Math.round,
98− toFloat:id,
96+ toFloat:function(x) { return x; },
9997
10098 readInt:readInt,
10199 readFloat:readFloat,
102100
103− id:id,
104− flip:F3(flip),
105101 curry:F3(curry),
106102 uncurry:F2(uncurry),
107103 fst:fst,
108104 snd:snd
109105 };
110106
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;
123108};
libraries/Native/Utils.js+2−2
@@ -19,7 +19,7 @@ Elm.Native.Utils = function(elm) {
1919 return x === y;
2020 }
2121
22− var EQ = 0, LT = 1, GT = 2, ord = ['EQ','LT','GT'];
22+ var EQ = 0, LT = -1, GT = 1, ord = ['EQ','LT','GT'];
2323 function compare(x,y) { return { ctor: ord[cmp(x,y)] } }
2424 function cmp(x,y) {
2525 var ord;
@@ -140,7 +140,7 @@ Elm.Native.Utils = function(elm) {
140140
141141 return elm.Native.Utils = {
142142 eq:eq,
143− cmp:compare,
143+ cmp:cmp,
144144 compare:F2(compare),
145145 Tuple0:Tuple0,
146146 Tuple2:Tuple2,