1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
-- PART 1.
-- Tests for Elm.Basics package. Operators that got imported by default. See http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Basics
-- Equality
a == b
a /= b
-- Comparison
a < b
a > b
a <= b
a >= b
-- Booleans
a && b
a || b
-- Mathematics
a + b
a - b
a * b
a / b
a ^ b
a // b
a % b
-- Strings and Lists
"hello" ++ "world" == "helloworld"
[1,1,2] ++ [3,5,8] == [1,1,2,3,5,8]
-- Higher-Order Helpers
leftAligned <| monospace <| fromString "code"
ngon 5 30
|> filled blue
|> move (10,10)
|> scale 2
not << isEven << sqrt
sqrt >> isEven >> not
-- PART 2.
-- elm-compiler test cases. See https://github.com/elm-lang/elm-compiler/tree/master/tests/test-files/good
-- LITERALS
-- Multiline
s = """
here's a quote: "
"""
-- Negatives
(-) x y = x
minus =
3 - 4
minusNegative =
3 - -4
negativeMinusNegative =
-3 - -4
negativeInParens =
(-2)
funcMinus =
(-) 3 4
-- Numbers
int = 12345
exp1 = 4e9
exp2 = 3E7
exp3 = 2e-9
exp4 = 1E+3
float1 = 3.14
float2 = 0.67
float3 = 6.022e26
float4 = 0.23E4
hex1 = 0xFFFFFFFF
hex2 = 0x0040
hex3 = 0xbeef
hex4 = 0xBEEF
hex5 = 0x0123789
-- Quotes and Comments
doubleQuote = '\"'
singleQuote = '\''
{- this is an {- embeded comment -} -}
commentStart = "{-"
-- RECORDS
-- Access
point =
{ x = 3, y = 4 }
x1 =
point.x
x2 =
.x point
x3 =
(point).x
x4 =
(.x) point
apply f x = f x
x5 =
apply .x point
-- Alias substitution
type alias Vec2Ext record =
{ record | x:Float, y:Float }
type alias Vec2 =
Vec2Ext {}
extractVec : Vec2Ext a -> Vec2
extractVec v =
{ x = v.x, y = v.y }
-- Non Homogeneous Records
type alias Thing =
{ x : Float
, y : Float
}
bindFields : Thing -> Thing
bindFields thing =
let
x = thing.x
y = thing.y
in
thing
-- SOUNDNESS
apply f = let g x = f x
in g
apply : (a -> b) -> a -> b
apply f = let g x = f x
in g
myId x = let y = x in y
myId : a -> a
myId x = let y = x in y
px : number -> String
px x =
"test"
-- LISTS
nonEmptyList = [1,4,5]
emptyList = []
-- DECLARATIONS
type alias Width = Float
type alias Height = Float
-- CRASH
import Debug
test x =
if x then
2
else
Debug.crash "unexpected value"
-- CASES
foo x =
case x of
'A' ->
"hello"
_ ->
"letter A was not seen"
type List a
= Nil
| Cons a (List a)
zip : List a -> List b -> List (a, b)
zip list1 list2 =
case (list1, list2) of
(Nil, _) ->
Nil
(_, Nil) ->
Nil
(Cons x xs, Cons y ys) ->
Cons (x, y) (zip xs ys)
-- PART 3.
-- Examples from the Elm syntax description. See http://elm-lang.org/docs/syntax
-- Comments
-- a single line comment
{- a multiline comment
{- can be nested -}
-}
{--}
add x y = x + y
--}
-- Literals
-- Boolean
True : Bool
False : Bool
42 : number -- Int or Float depending on usage
3.14 : Float
'a' : Char
"abc" : String
-- multi-line String
"""
This is useful for holding JSON or other
content that has "quotation marks".
"""
True && not (True || False)
(2 + 4) * (4^2 - 9)
"abc" ++ "def"
-- Lists
[1,2,3,4]
1 :: [2,3,4]
1 :: 2 :: 3 :: 4 :: []
-- Conditionals
if powerLevel > 9000 then "OVER 9000!!!" else "meh"
if key == 40 then
n + 1
else if key == 38 then
n - 1
else
n
case maybe of
Just xs -> xs
Nothing -> []
case xs of
hd::tl -> Just (hd,tl)
[] -> Nothing
case n of
0 -> 1
1 -> 1
_ -> fib (n-1) + fib (n-2)
-- Union Types
type List = Empty | Node Int List
-- Records
point = -- create a record
{ x = 3, y = 4 }
point.x -- access field
List.map .x [point,{x=0,y=0}] -- field access function
{ point | x = 6 } -- update a field
{ point | -- update many fields
x = point.x + 1,
y = point.y + 1
}
dist {x,y} = -- pattern matching on fields
sqrt (x^2 + y^2)
type alias Location = -- type aliases for records
{ line : Int
, column : Int
}
-- Functions
square n =
n^2
hypotenuse a b =
sqrt (square a + square b)
distance (a,b) (x,y) =
hypotenuse (a-x) (b-y)
square =
\n -> n^2
squares =
List.map (\n -> n^2) (List.range 1 100)
-- Infix Operators
(?) : Maybe a -> a -> a
(?) maybe default =
Maybe.withDefault default maybe
infixr 9 ?
viewNames1 names =
String.join ", " (List.sort names)
viewNames2 names =
names
|> List.sort
|> String.join ", "
-- (arg |> func) is the same as (func arg)
-- Just keep repeating that transformation!
-- Let Expressions
let
twentyFour =
3 * 8
sixteen =
4 ^ 2
in
twentyFour + sixteen
let
( three, four ) =
( 3, 4 )
hypotenuse a b =
sqrt (a^2 + b^2)
in
hypotenuse three four
let
name : String
name =
"Hermann"
increment : Int -> Int
increment n =
n + 1
in
increment 10
-- Applying Functions
-- alias for appending lists and two lists
append xs ys = xs ++ ys
xs = [1,2,3]
ys = [4,5,6]
-- All of the following expressions are equivalent:
a1 = append xs ys
a2 = xs ++ ys
b2 = (++) xs ys
c1 = (append xs) ys
c2 = ((++) xs) ys
23 + 19 : number
2.0 + 1 : Float
6 * 7 : number
10 * 4.2 : Float
100 // 2 : Int
1 / 2 : Float
(,) 1 2 == (1,2)
(,,,) 1 True 'a' [] == (1,True,'a',[])
-- Modules
module MyModule exposing (..)
-- qualified imports
import List -- List.map, List.foldl
import List as L -- L.map, L.foldl
-- open imports
import List exposing (..) -- map, foldl, concat, ...
import List exposing ( map, foldl ) -- map, foldl
import Maybe exposing ( Maybe ) -- Maybe
import Maybe exposing ( Maybe(..) ) -- Maybe, Just, Nothing
import Maybe exposing ( Maybe(Just) ) -- Maybe, Just
-- Type Annotations
answer : Int
answer =
42
factorial : Int -> Int
factorial n =
List.product (List.range 1 n)
distance : { x : Float, y : Float } -> Float
distance {x,y} =
sqrt (x^2 + y^2)
-- Type Aliases
type alias Name = String
type alias Age = Int
info : (Name,Age)
info =
("Steve", 28)
type alias Point = { x:Float, y:Float }
origin : Point
origin =
{ x = 0, y = 0 }
-- JavaScript Interop
-- incoming values
port prices : (Float -> msg) -> Sub msg
-- outgoing values
port time : Float -> Cmd msg
|