Functional state management of web front-ends through Elm

  • Introduction
  • Front-ends and state management
    • Functional programming with Elm
  • Examples, Setup & Installation
  • Data types
  • Basics
  • Functions
  • Modules
  • Elm Architecture
  • Fetching data

Advanced Web Front-ends

COMP.CS.520, Spring 2023

Topic: An introduction to functional state management of web front-ends through Elm

Timo Nummenmaa

Prosper Evergreen, Janne Miilunpalo, Pia Niemelä, Jaakko Rajala

Front-end state management?

  • With modern web applications, it is common for the application state to be held by the front-end application.
  • However, handling the state is a complex matter and one of the major problems when developing the front-end. More on this viewpoint.

Front-end state management? continued

  • Many libraries and frameworks have rudimentary state management features with somewhat limited capabilities in handling global state.
  • These state management features are often enhanced through the use of libraries: e.g. Redux for React and VueX/Pinia for Vue.

Why? What is the issue?

The issue stems from complexity, inconsistency, unpredictability and issues with state placement.

Redux and VueX

These are popular solutions to the problem and try to help in the creation of consistently performing applications with centralized and transparent state management features.

It happens that these libraries take inspiration from Elm, so we will start from there before moving to more popular approaches. It is also recommended by the Redux site to check Elm out!

What is Elm?

Elm is a functional programming language designed by Evan Czaplicki in 2012. Elm focuses on building reactive frontend applications as it compiles to JavaScript. However, Elm can also be used for games, embedded applications etc. Elm is a strong and static typed language that takes advantages of both functional and reactive programming. Learn more.

Elm core features

  • Pure functions
  • Immutability
  • Static and strong types
  • Type inference
  • Module system
  • Interoperability with HTML, CSS, and JavaScript

Why use Elm?

The Elm Architecture

Model-View-Update

  • Model = store the state
  • View = turn state to HTML
  • Update = update the state based on messages
  • Unidirectional dataflow (data flows in one direction)

Purely Functional programming

  • Declarative
  • Construct programs by applying and composing pure functions
    • Return identical values for identical inputs
    • No side effects

Functional language

There are benefits of the functional style of programming; as a pure functional language elm can (according to elm) provide:

  • No runtime errors.
  • Friendly error messages.
  • Reliable refactoring.
  • Automatically enforced semantic versioning for all Elm packages.

Side effects in Elm

  • Ask the elm runtime to do something (e.g., send data to a remote server).
  • Get notified when something happens (e.g., listen to a socket)

Examples, Setup & Installation