Elm (programming language)
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",[10] made possible by the Elm compiler's static type checking. HistoryElm was initially designed by Evan Czaplicki as his thesis in 2012.[11] The first release of Elm came with many examples and an online editor that made it easy to try out in a web browser.[12] Czaplicki joined Prezi in 2013 to work on Elm,[13] and in 2016 moved to NoRedInk as an Open Source Engineer, also starting the Elm Software Foundation.[14] The initial implementation of the Elm compiler targets HyperText Markup Language (HTML), Cascading Style Sheets (CSS), and JavaScript.[15] The set of core tools has continued to expand, now including a read–eval–print loop (REPL),[16] package manager,[17] time-travelling debugger,[18] and installers for macOS and Windows.[19] Elm also has an ecosystem of community created libraries,[20] and Ellie, an advanced online editor that allows saved work and including community libraries.[21] FeaturesElm has a small set of language constructs, including traditional if-expressions, let-expressions for storing local values, and case-expressions for pattern matching.[22] As a functional language, it supports anonymous functions, functions as arguments, and functions can return functions, the latter often by partial application of curried functions. Functions are called by value. 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". ImmutabilityAll values in Elm are immutable, meaning that a value cannot be modified after it is created. Elm uses persistent data structures to implement its arrays, sets, and dictionaries in the standard library.[23] Static typesElm 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 Types can refer to other types, for example a Rather than allow any value to be implicitly nullable (such as JavaScript's Elm provides a limited number of built-in type classes: Module systemElm 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 Interoperability with HTML, CSS, and JavaScriptElm uses an abstraction called ports to communicate with JavaScript.[25] 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.[26] It uses a virtual DOM approach to make updates efficient.[27] BackendElm does not officially support server-side development. Czaplicki does consider it a primary goal at this point, but public progress on this front has been slow. Nevertheless, there are several independent projects which attempt to explore Elm on the backend. The primary production-ready full-stack Elm platform is Lamdera, an open-core "unfork" of Elm.[28][29][30] Czaplicki has also teased Elm Studio, a potential alternative to Lamdera, but it isn't available to the public yet.[31] Current speculation is that Elm Studio will use a future version of Elm that targets C, uses Emscripten to compile to WASM, and supports type-safe Postgres table generation.[32][33] For full-stack frameworks, as opposed to BaaS products, elm-pages is perhaps the most popular fully open-source option.[34] It does not extend the Elm language, but just runs the compiled JS on Node.js. It also supports scripting. There is also Pine, an Elm to .NET compiler, which allows safe interop with C#, F#, and other CLR languages.[35] There were also some attempts in Elm versions prior to 0.19.0 to use the BEAM (Erlang virtual machine) to run Elm, but they are stuck due to the removal of native code in 0.19.0 and changes to the package manager. One of the projects executed Elm directly on the environment,[36] while another one compiled it to Elixir.[37] Finally, the Gren programming language started out a fork of Elm primarily focused on backend support, although its goals have since shifted. The Elm Architecture (TEA pattern)The Elm Architecture is a software design pattern and as a TLA called TEA pattern for building interactive web applications. Elm applications are naturally constructed in that way, but other projects may find the concept useful. An Elm program is always split into three parts:
Those are the core of the Elm Architecture. For example, imagine an application that displays a number and a button that increments the number when pressed.[38] In this case, all we need to store is one number, so our model can be as simple as In the Elm Architecture, sending messages to LimitsElm does not support higher-kinded polymorphism,[39] which related languages Haskell, Scala and PureScript offer, nor does Elm support the creation of type classes. This means that, for example, Elm does not have a generic Another outcome is a large amount of boilerplate code in medium to large size projects as illustrated by the author of "Elm in Action," a former Elm core team member, in his single page application example[42] with almost identical fragments being repeated in update, view, subscriptions, route parsing and building functions. Example code-- This is a single line comment.
{-
This is a multi-line comment.
It is {- nestable. -}
-}
-- 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)
-- We can create lambda functions with the `\[arg] -> [expression]` syntax.
hello : String -> String
hello = \s -> "Hi, " ++ s
-- Function declarations may have the anonymous parameter names denoted by `_`,
-- which are matched but not used in the body.
const : a -> b -> a
const k _ = k
-- 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 `Bool` 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 tagged unions 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 _ left right ->
1 + max (depth left) (depth right)
See also
References
External links |
Portal di Ensiklopedia Dunia