Purely functional frontend framework for building web applications
Purely functional frontend framework for building web applications
A purely functional frontend framework based on functional reactive programming. Experimental.
The JavaScript world is full of frameworks. So why another one? Because we want something different. We want something that is purely functional without compromises. Something that takes the best lessons from existing JavaScript frameworks and couples them with the powerful techniques found in functional languages like Haskell. We want a framework that is highly expressive. Because when functional programming is at its best it gives you more power, not less. Turbine is supposed to be approachable for typical JavaScript developers while still preserving the benefits that comes from embracing purely functional programming.
We have done our best to realize our goal. But we are not done yet. We hope you will find Turbine interesting, try it and maybe even help us making it even better.
See the example live and editable here.
const isValidEmail = (s: string) => /.+@.+\..+/i.test(s);
const app = component((on) => {
const isValid = on.email.map(isValidEmail);
return [
span("Please enter an email address: "),
input().use({ email: "value" }),
div(["The address is ", isValid.map((b) => (b ? "valid" : "invalid"))])
];
});
// `runComponent` is the only impure function in application code
runComponent("#mount", main);See the example live and editable here.
const counter = component((on, start) => {
const count = start(accum((n, m) => n + m, 0, on.delta));
return E.div([
"Counter ",
count,
E.button({ class: "btn btn-default" }, " + ").use(o => ({
delta: o.click.mapTo(1)
})),
E.button({ class: "btn btn-default" }, " - ").use(o => ({
delta: o.click.mapTo(-1)
}))
]);
});
// `runComponent` is the only impure function in application code
runComponent("#mount", counter);Here our some of our key features.
Here are some of the features we want to implement and goals we're working towards.
This section describes some of the key principles and ideas underlying the design of Turbine.
Turbine is purely functional. We mean that in the most strict sense of the term. In a Turbine app, every single expression is pure. This gives a huge benefit in how easy it is to understand and maintain a Turbine app is.
One benefit of the complete purity is that every function in Turbine supports what is called "referential transparency". This means that an expression can always be replaced with its value.
As a simple example, say you have the following code:
const view = div([
myComponent({ foo: "bar", something: 12 }),
myComponent({ foo: "bar", something: 12 })
]);One may notice that myComponent is called twice with the exact same
arguments. Since all functions in a Turbine app are pure myComponent
is no exception. Hence, we can make the following simple refactoring.
const component = myComponent({foo: "bar", something: 12}),
const view = div([
component,
component
]);Such refactorings can always be safely done in Turbine.
One significant challenge when writing an interactive frontend application is how to manage the data flow through an application.
In Turbine we have strived to create an architecture where the data flow is easy to follow and understand. For us, this means that when looking at any piece of code it should be possible to see what other parts of the application it affects and what other parts it is affected by.
One manifestation of this principle is that in Turbine it is very simple to see how the model affects the view and how the view affects the model. The figure below illustrates this.
The arrows represent data flow between the model and the view. Note how these "conceptual arrows" are clearly expressed in the code. For instance, by looking at the buttons we can see exactly what output they produce.
Imperative programming is about doing. Functional programming is about being. This mean that ideally a functional program should be about defining what things are. That property is what makes functional programs declarative.
Below is a model from the counters example.
Notice how the model consists of nothing but a series of const
statements.
function* counterModel({ incrementClick, decrementClick, deleteClick }) {
const increment = incrementClick.mapTo(1);
const decrement = decrementClick.mapTo(-1);
const deleteS = deleteClick.mapTo(id);
const count = yield accum(add, 0, combine(increment, decrement));
return { count, deleteS };
}Each line is a declaration of a piece of the state. All models in Turbine follows this pattern. This makes state in a Turbine app very easy to understand. One can look at a single definition and be certain that it tells everything there is to know about that specific piece of state.
This is in sharp contrast to frameworks that mutate state or frameworks where state is stepped forward by reducer functions. With such approaches a single piece of state can potentially be affected and changed in several places. That can make it hard to understand how the state evolves. The benefits of having a definition as a single source of truth is lost.
npm install @funkia/turbine @funkia/hareactiveHareactive is a peer dependency. It is the FRP library that that Turbine is based upon.
Alternatively, for quickly trying out Turbine you may want to see our Turbine starter kit.
Here is a series of examples that demonstrate how to use Turbine. Approximately listed in order of increasing complexity.
In this tutorial, we will build a simple application with a list of counters. The application will be simple but not completely trivial. Along the way, most of the key concepts in Turbine will be explained. We will see how to create HTML, how to create custom components, how a component can be nested and how it can share state with its parent.
Please open an issue if you have questions regarding the tutorial or ideas for improvements.
The final result and the intermediate states can be seen by cloning this git repository, going into the directory with the counters example and running webpack to serve the application.
git clone https://github.com/funkia/turbine/
cd turbine/examples/counters
npm run startTurbine builds on top of the FRP library Hareactive. The two key concepts from FRP are behavior and stream. They are documented in more detail in the Hareactive readme. But the most important things to understand are behavior and stream.
Behavior represents values that change over time. For instance,
the position of the mouse or the number of times a user has clicked
a button.Stream represents discrete events that happen over time. For
instance click events.ComponentOn top of the FRP primitives Turbine adds Component. Component is the
key concept in Turbine. Once you understand Component—and how to use
it—you understand Turbine. A Turbine app is just one big component.
Here is a high-level overview of what a component is.
IO-actions.A Component in Turbine is pure and immutable. A Component can be
thought of as a huge description of all of the above mentioned things.
For instance, a Component contains a description about what its DOM
look like. That part is a bit like virtual DOM. But, on top op that
the description also explain how the DOM changes over time. The
description also tells what output the Component contains. More on
that later.
Turbine includes functions for creating components that represent standard HTML-elements. When you create your own components they will be made of these.
The element functions accept two arguments, both of which are optional. The first is an object describing various things like attributes, classes, etc. The second argument is a child component. For instance, to create a div with a span child we would write.
const myDiv = div({ class: "foo" }, span("Some text"));The element functions are overloaded. So instead of giving span a
component as child we can give it a string. The element functions also
accept an array of child elements like this.
const myDiv = div({ class: "foo" }, [h1("A header"), p("Some text")]);Using this we can build arbitrarily complex HTML. As an example we will build a simple view for a counter in our counter-application.
import { elements, runComponent } from "@funkia/turbine";
const { br, div, button } = elements;
// CoNo open issues yet, or sync has not completed.