This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathMain.elm
426 lines (312 loc) · 11.5 KB
/
Main.elm
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
port module Main exposing (Msg(..), Natural, main)
{-| The above declaration shows how to:
- declare a port module
- expose all the tags of a custom type (Msg)
- expose only the type (Natural)
-}
-- Shows how to import everthing from a module (Html). It is recommended to avoid this syntax.
-- Shows how to create an alias for a module name (Events)
-- Shows how to import multiple modules into a single namespace (Math). Use this with great care as it can create confusion about the source of a function.
import Bar
import Foo as F exposing (foo)
-- CUSTOM TYPES
{-| Shows how to define a single variant custom type.
Exposing only the type and not the tags ensures that the values of the type are created only through functions that can enforce constrains.
-}
type Natural
= Natural Int
{-| Shows how to define a function that creates a value for the above custom type
-}
fromInt : Int -> Natural
fromInt intValue =
-- Tags of the custom types are also acting as functions that create the custom type.
-- max function is defined in the Basics module from elm/core and is imported by default. See elm/core for a list of default imports.
Natural (max intValue 0)
{-| Shows how to unpack custom type parameters. Works only if the type has a single variant.
-}
toInt : Natural -> Int
toInt (Natural value) =
value
{-| Shows how to define a function in a pointfree style by composing two functions.
-}
toString : Natural -> String
toString =
-- String.fromInt shows how to use a module that is imported by default.
toInt >> String.fromInt
{-| Shows how to control the operations on the your custom type.
In this case the code makes sure you are not storing negative values inside the custom type
-}
addInt : Natural -> Int -> Natural
addInt natural intValue =
let
-- One ca unpack / destructure a custom type inside a let..in too
(Natural value) =
natural
in
fromInt (value + intValue)
{-| Adds 42 (the value is an Int written with HEX notation).
-}
addMeaning : Natural -> Natural
addMeaning (Natural value) =
fromInt (value + 0x2A)
{-| Shows how to create a type alias for a type that extends records. This alias will extend any other record with the field `name`.
-}
type alias Named a =
{ a | name : String }
{-| Shows how to use the above type alias.
-}
type alias NamedValue a =
Named { value : a }
{-| Shows how to use the values from an extensible record alias fields
-}
namedToHtml : Named a -> Html msg
namedToHtml { name } =
text name
namedNaturalToHtml : NamedValue Natural -> Html msg
namedNaturalToHtml namedValue =
div []
[ namedToHtml namedValue
, text ": "
, text (toString namedValue.value)
]
{-| Shows how to create a phantom type
-}
type Unit a
= Unit Int
{-| When adding two units, the type parameter must be the same.
-}
addUnit : Unit a -> Unit a -> Unit a
addUnit (Unit first) (Unit second) =
Unit (first + second)
{-| A type to be used with the above Unit type
-}
type Meter
= Meter
{-| A second type to be used with the above Unit type
-}
type Gram
= Gram
twoMeters : Unit Meter
twoMeters =
Unit 2
threeMeters : Unit Meter
threeMeters =
Unit 3
fewGrams : Unit Gram
fewGrams =
Unit 21
someMeters : Unit Meter
someMeters =
-- This works because the two units match
addUnit twoMeters threeMeters
{- This value will throw an error if uncommented
impossibleAdd : Unit Meter
impossibleAdd =
-- This doesn't work because the types don't match
addUnit fewGrams someMeters
-}
-- MODEL
{-| Shows how to tie a name to a record type.
-}
type alias Model =
{ count : Natural
, namedCount : NamedValue Natural
}
{-| Shows how to ignore a parameter you are not using
This purposefully shows a function without a type signature although top level functions and values should have type signatures.
-}
init _ =
( { count = Natural 0
, namedCount =
{ name = "Natural", value = Natural 0 }
}
, Cmd.none
)
-- UPDATE
{-| Shows how to give a new name to a more complex type
-}
type alias Naturals =
List Natural
{-| Shows how to define a custom type with multiple variants
-}
type Msg
= Increment
| Decrement
| AddNextTen
| OnSubscription (Result String Naturals)
{-| Shows how to unpack a record parameter while still keeping the full parameter.
You can use a subset of the fields in the record if you only need certain fields.
This function type signature has been purpusefully spread over multiple lines to show that complex signatures need not be single line.
-}
update :
Msg
-> Model
-> ( Model, Cmd msg )
update msg ({ count } as model) =
case msg of
Increment ->
( { model | count = addInt count 1 }, Cmd.none )
Decrement ->
-- Shows how to create a new scope with a let..in expression
let
-- values and function defined inside let..in can have type signatures although they usually don't
newValue : Natural
newValue =
addInt count -1
-- this is how to use the Debug.log to check for a value
_ =
Debug.log "newValue" newValue
-- this shows that you can declare multiple _ values without the compiler complaining.
-- attempting to use a named declaration multiple times will result in a compiler error
_ =
newValue
-- adding the next line at the end of a declaration with result in it being logged to the JS console
|> Debug.log "newValue"
in
if newValue == count then
-- Shows how to call a port
( model, reportError "There are no negative Natural numbers" )
else
( { model | count = newValue }, Cmd.none )
AddNextTen ->
let
addIntToCount =
-- Shows how to partially apply a function.
-- This is very useful in scopes where the first arguments stay the same
addInt count
intCount =
toInt count
addTen =
-- Shows how to use an infix operator as a prefix function.
-- This is useful when you want to partially apply the operator and use
-- the resulting function in a higher order function.
(+) 10
nextTen =
-- Shows how to use an infix operator as an argument in a higher order function.
List.foldl (+) 0 (List.range intCount (addTen intCount))
in
( { model | count = addIntToCount nextTen }, Cmd.none )
-- Shows how to unpack a variant by matching against the contained variants
OnSubscription (Ok naturals) ->
case naturals of
-- Shows how to pattern match on a List
[] ->
-- Shows how to recursively call update in order to avoid duplicating code.
update (OnSubscription (Err "Received an empty list")) model
-- Shows how to pattern match on a list with a fixed number of elements
[ first ] ->
( { model | count = first }, Cmd.none )
-- Shows how to pattern match on a list with at least two elements.
first :: second :: _ ->
( { model | count = first }, Cmd.none )
OnSubscription (Err error) ->
( model, reportError error )
-- VIEW
{-| Shows how to declare a String that spans multiple lines
-}
multiline : String
multiline =
"""
This is a multiline string.
It will be displayed by spliting the lines into separate paragraphs.
"""
{-| Shows how to define a tuple.
-}
initials : ( Char, Char )
initials =
-- Show how to declare a Char type.
( 'J', 'D' )
view : Model -> Html Msg
view model =
let
namedCount =
-- Shows how to access a field from a record by using a field accessor function
.namedCount model
-- Shows how to pattern match a tuple
( first, last ) =
initials
-- a helper function
named value =
{ name = value }
-- shows that record field accessors work on expressions too
nameFromExpression =
(named "Foo").name
in
main_ []
[ button [ onClick Increment ] [ text "+1" ]
-- Shows how to avoid parentheses by using the backwards pipe operator
, div [] [ text <| toString model.count ]
-- Shows how to used a function from a module without having to expose it in the import section.
, div [] [ button [ Events.onClick Decrement ] [ text "-1" ] ]
, button [ onClick AddNextTen ] [ text "Add Next Ten" ]
, div [] [ namedNaturalToHtml namedCount ]
, String.lines multiline
|> List.map (\line -> p [] [ text line ])
|> div []
, footer [] [ text (String.fromChar first), text (String.fromChar last) ]
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
-- Listen to the incomming port only if condition in the model is met
if toInt model.count < 5 then
-- Show how to use an anonymous function (lambda expression)
fromJS
(\value ->
case List.map String.toInt value of
[] ->
OnSubscription (Err "Received an empty list")
(Just int) :: _ ->
let
output =
-- This shows how to prepend an element to a list
fromInt int :: []
in
if int >= 0 then
OnSubscription (Ok output)
else
-- Shows how to structure a complex function application by using the "pipe" operator
("Received a negative number: " ++ String.fromInt int)
|> Err
|> OnSubscription
-- Shows how to catch all remaining variants. Watch out for this pattern as it can create troubles.
_ ->
OnSubscription (Err "Received a list that started with a non-integer ")
)
else
Sub.none
-- WIRING
{-| Signature for Browser.element is Program flags model msg.
The flags type argument here is the Unit type: ()
-}
main : Program () Model Msg
main =
Browser.element
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- PORTS
{-| Shows how to define an outgoing port
-}
port reportError : String -> Cmd msg
{-| Shows how to define an incomming port.
The first parameter is a function that takes the data received from JS and produces a message that the app understands.
-}
port fromJS : (List String -> msg) -> Sub msg
-- ADVANCED SYNTAX
{-| Elm also has special syntax for declaring WebGL shaders. See more about this at: <https://github.com/elm-explorations/webgl/>
-}
vertexShader : WebGL.Shader { a | coord : Math.Vec3, position : Math.Vec3 } { b | view : Math.Mat4 } { vcoord : Math.Vec2 }
vertexShader =
[glsl|
attribute vec3 position;
attribute vec3 coord;
uniform mat4 view;
varying vec2 vcoord;
void main () {
gl_Position = view * vec4(position, 1.0);
vcoord = coord.xy;
}
|]