Elm (programming language)
Paradigm | Functional |
---|---|
Designed by | Evan Czaplicki |
First appeared | 2012 |
Stable release | 0.18
/ November 14, 2016 |
Typing discipline | Static, Strong, Inferred |
License | Permissive (Revised BSD)[1] |
Filename extensions | .elm |
Website | elm-lang |
Influenced by | |
Haskell, Standard ML, OCaml, F# | |
Influenced | |
Redux,[2] Vue.js[3] |
Search Elm (programming language) on Amazon.
Elm is a domain-specific programming language for declaratively creating web browser-based graphical user interfaces. Elm is purely functional, and is developed with emphasis on usability, performance, and robustness. It advertises "no runtime exceptions in practice,"[4] made possible by the Elm compiler's static type checking.
History[edit]
Elm was initially designed by Evan Czaplicki as his thesis in 2012.[5] The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser.[6] Evan Czaplicki joined Prezi in 2013 to work on Elm,[7] and in 2016 moved to NoRedInk as an Open Source Engineer, also starting the Elm Software Foundation.[8]
The initial implementation of the Elm compiler targets HTML, CSS, and JavaScript.[9] The set of core tools has continued to expand, now including a REPL,[10] package manager,[11] time-traveling debugger,[12] and installers for Mac and Windows.[13] Elm also has an ecosystem of community created libraries[14] and an advanced online editor that allows saved work and inclusion of community libraries.[15]
Features[edit]
Elm has a small but expressive set of language constructs, including traditional if-expressions, let-expressions for local state, and case-expressions for pattern matching.[16] As a functional language, it supports anonymous functions, functions as arguments, and partial application (currying) by default. Its semantics include immutable values, stateless functions, and static typing with type inference. Elm programs render HTML through a virtual DOM, and may interoperate with other code by using "JavaScript as a service".
Immutability[edit]
All values in Elm are immutable, meaning that a value cannot be modified after it is created. Elm uses persistent data structures to implement its Array
, Dict
, and Set
libraries.[17]
Static types[edit]
Elm is statically typed. Type annotations are optional (due to type inference) but strongly encouraged. Annotations exist on the line above the definition (unlike C-family languages where types and names are interspersed). Elm uses a single colon to mean "has type".
Types include primitives like integers and strings, and basic data structures such as lists, tuples, and records. Functions have types written with arrows, for example round : Float -> Int
. Union types allow the programmer to create custom types to represent data in a way that matches the problem domain.[18]
Types can refer to other types, for example a List Int
. Types are always capitalized; lowercase names are type variables. For example, a List a
is a list of values of unknown type. It is the type of the empty list and of the argument to List.length
, which is agnostic to the list's elements. There are a few special types that programmers create to interact with the Elm runtime. For example, Html Msg
represents a (virtual) DOM tree whose event handlers all produce messages of type Msg
.
Rather than allow any value to be implicitly nullable (such a JavaScript's undefined
or a null pointer), Elm's standard library defines a Maybe a
type. Code that produces or handles an optional value does so explicitly using this type, and all other code is guaranteed a value of the claimed type is actually present.
Module system[edit]
Elm has a module system that allows users to break their code into smaller parts called modules. Modules can hide implementation details such as helper functions, and group related code together. Modules serve as a namespace for imported code, such as Bitwise.and
. Third party libraries (or packages) consist of one or more modules, and are available from the Elm Public Library. All libraries are versioned according to semver, which is enforced by the compiler and other tools. That is, removing a function or changing its type can only be done in a major release.
Interoperability with HTML, CSS, and JavaScript[edit]
Elm uses an abstraction called ports to communicate with JavaScript.[19] It allows values to flow in and out of Elm programs, making it possible to communicate between Elm and JavaScript.
Elm has a library called elm-html that a programmer can use to write HTML and CSS within Elm.[20] It uses a virtual DOM approach to make updates efficient.[21]
Limitations[edit]
Unlike Haskell or PureScript, Elm has no support for higher-kinded types, and thus cannot provide generic abstractions for many common operations.[22] For example, there is no generic map
, apply
, fold
, or filter
function. Instead, such names are used prefixed by their module, such as List.map
and Dict.map
.
Tools[edit]
- Elm Platform for installing the core tools locally
- Learning resources and examples
- Core Libraries and Community Libraries
- Online editor at ellie-app.com for easy experimentation
Example code[edit]
-- This is a single line comment
{- This is a multi-line comment.
It can span multiple lines.
-}
{- It is possible to {- nest -} multi-line comments -}
-- Here we define a value named ''greeting''. The type is inferred as a String.
greeting =
"Hello World!"
-- It is best to add type annotations to top-level declarations.
hello : String
hello =
"Hi there."
-- Functions are declared the same way, with arguments following the function name.
add x y =
x + y
-- Again, it is best to add type annotations.
hypotenuse : Float -> Float -> Float
hypotenuse a b =
sqrt (a^2 + b^2)
-- Functions are also curried; here we've curried the multiplication
-- infix operator with a `2`
multiplyBy2 : number -> number
multiplyBy2 =
(*) 2
-- If-expressions are used to branch on values
absoluteValue : number -> number
absoluteValue number =
if number < 0 then negate number else number
-- Records are used to hold values with named fields
book : { title : String, author : String, pages : Int }
book =
{ title = "Steppenwolf"
, author = "Hesse"
, pages = 237
}
-- Record access is done with `.`
title : String
title =
book.title
-- Record access `.` can also be used as a function
author : String
author =
.author book
-- We can create entirely new types with the `type` keyword.
-- The following value represents a binary tree.
type Tree a
= Empty
| Node a (Tree a) (Tree a)
-- It is possible to inspect these types with case-expressions.
depth : Tree a -> Int
depth tree =
case tree of
Empty ->
0
Node value left right ->
1 + max (depth left) (depth right)
See also[edit]
Some use of "" in your query was not closed by a matching "".Some use of "" in your query was not closed by a matching "".
- PureScript, a strongly-typed functional programming language that transpiles to JavaScript
- TypeScript, a strongly-typed programming language that transpiles to JavaScript
- Miso, a Haskell implementation of the Elm architecture, using GHCJS.
- TEA an OCaml implementation of the Elm architecture
- Elmish a F# implementation of the Elm architecture
- Oak a Clojure implementation of the Elm architecture.
References[edit]
- ↑ https://github.com/evancz/Elm/blob/master/LICENSE
- ↑ Redux Prior Art
- ↑ Elm Influenced Vue.js
- ↑ "Elm home page".
- ↑ Elm: Concurrent FRP for Functional GUIs
- ↑ Elm's Online Editor
- ↑ Elm joins Prezi
- ↑ New Adventures for Elm
- ↑ Elm compiler source code
- ↑ Elm REPL announcement
- ↑ Elm Package Manager announcement
- ↑ Elm's Time-Traveling Debugger
- ↑ Elm Platform
- ↑ Elm Public Libraries
- ↑ "Ellie, the Elm Live Editor".
- ↑ The Syntax of Elm
- ↑ Elm Standard Libraries
- ↑ "Model The Problem". Elm. Retrieved 4 May 2016.
- ↑ Ports
- ↑ elm-html documentation
- ↑ Blazing Fast Html
- ↑ "Higher-Kinded types Not Expressible? #396". github.com/elm-lang/elm-compiler. Retrieved 6 March 2015.
External links[edit]
This article "Elm (programming language)" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Elm (programming language). Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.
This page exists already on Wikipedia. |