We're planting a tree for every job application! Click here to learn more

Making the Leap from JavaScript to PureScript

Alex Kelley

2 Mar 2018

5 min read

Making the Leap from JavaScript to PureScript
  • JavaScript

Note: This is the introduction to the “Make the Leap from JavaScript to PureScript” tutorial series. I’ll be publishing a new tutorial approximately once-per-week. So come back often, there’s a lot more of this to come! Tutorial 1

Introduction


"Make the Leap from JavaScript to PureScript" takes some of the most popular functional programming (FP) abstractions in JavaScript and demonstrates how to implement them in PureScript. I borrowed the series outline and JavaScript code samples with permission from the egghead.io course Professor Frisby Introduces Composable Functional JavaScript by Brian Lonsdorf. I want to thank Brian (an enthusiastic coder of PureScript) for supporting this project.

Motivation for this series

Over the last few months, I've noticed a few curious JavaScript coders popping in on the PureScript forums (e.g., gitter.im) to ask about FP and PureScript. In response, seasoned functional programmers have been highly recommending Brian's course, as a first step in becoming acquainted with FP. Then, once they understand the basic abstractions from the comfort of JavaScript, they're encouraged to come back and begin working with PureScript. I can't agree more with this recommendation! It is hard enough to learn the FP abstractions AND a new programming language at the same time. But, in time, if you stick with FP in JavaScript, then you'll likely find that it is easy to stray, and then you're right back to your old and imperative ways. Alternatively, PureScript is a true blue FP language that compiles to JavaScript! So I've written these tutorials to help you to cross over from JavaScript to PureScript by showing you how to implement the same concepts taught in Brian's course.

Navigating your way

If you're new to FP or you haven't tackled Brian's course, then I highly recommend that you do that first (I certainly did, and it helped me a lot). Then, once you understand the basic abstractions, come back and I'll show you them in PureScript. I've done my best to explain the concepts, but Brian is a master at teaching them. Also, these tutorials are not a replacement for a good old fashioned PureScript primer. So take the time to learn a little PureScript syntax before diving head first into these tutorials.

Advantages of using PureScript in place of JavaScript


You can breathe easy knowing that JavaScript supports some of the most important features of FP, including first class & anonymous functions and closures. However, JavaScript has to serve more than one master, including object-oriented and imperative programming. As a consequence, there are limitations and compromises when using JavaScript for FP. In particular, it is missing a static type system, enforced purity, and immutability. Some of these gaps can be mitigated by adding a static type checker (see Flow), immutable collections (see Immutable.js), and FP abstraction libraries (see RamdaJS). Still, FP in JavaScript is mostly accomplished by convention, often cobbled together with the 'band-aids' I mentioned above. A functional JavaScript programmer must stay sharp at all times in order to create pure functions that avoid side effects. But I believe this puts too much cognitive load on the programmer and ultimately it interferes with coding your application. Thankfully there are proper FP languages that eliminate this friction while compiling to optimized JavaScript - did anyone say PureScript?

PureScript has been architected solely as an FP language. It is a small, strongly typed language that compiles to human readable CommonJS, and other languages too. So you've got both client and server side applications covered in one language - which doesn't get any better IMHO! You'll also find a representation of all the FP language constructs that you've either heard or read about; including currying, pattern matching, tail call optimization, higher order and higher kinded types. Finally, PureScript has no runtime system to add to your download footprint, and plus (drum roll please) there is an uncomplicated but very capable FFI to and from JavaScript! So if you don't find support yet for functions from your favorite JavaScript module, then it is not hard to include them yourself (and I'll show you some examples in the tutorials).

Tutorial Layout


I’ve created a github repository with the markdown versions of these stories (i.e., README.md) together with the code samples. You can clone it here and fetch upstream for future updates.

Each tutorial has been placed in a separate folder named 'tut##', where ## is a number (e.g., 'tut01') that corresponds to video## from Brian's course. I have set up the folders so that you can run the PureScript code samples for the first time with bower update && pulp run, then pulp run from then on (explained below). You will also find the accompanying tutorial markdown (e.g., tut01/README.md) which illustrates the abstraction from Brian's corresponding video (e.g. video1) and how to implement it in PureScript.

Get up and running in PureScript


You can skip this section if you've already installed and are using PureScript on your computer.

Install PureScript and its supporting actors

See Getting Started with PureScript

TL;DR

Seriously? Don't you have 10-minutes to review the career changing Getting Started with PureScript? Well then I wash my hands or (as Japanese would have it, my feet) of all responsibility for the results. All kidding aside:

npm install -g purescript pulp bower

Run your first PureScript program

Assuming you've installed PureScript, Pulp, and Bower, then it doesn't get any easier than this to start your adventure in functional programming with PureScript:

$ mkdir purescript-hello
$ cd purescript-hello
$ pulp init
$ pulp run

Pulp is an excellent build tool with plenty of helper options; such as watching for source file updates and re-compiling them automatically. As you add more library dependencies to your program, you can install them with bower (e.g., bower install purescript-lists --save)

My Favorite PureScript tools & references

  1. PureScript by Example by Phil Freeman the author of the PureScript language
  2. Pursuit is the home of PureScript documentation; soon to become your best friend
  3. Try PureScript allows you to try key examples of PureScript in the browser. You can also create your own.
  4. psc-ide (distributed with the compiler) provides editor support, including atom, emacs, vim, and visual studio

Onward!


Tutorial 1

I'll add additional links as I write the tutorials. But If you would like to look ahead, then the majority of the code samples from Brian's videos are available on Github. But I may amend them as I write the accompanying tutorial markdown.

Original source


If you're passionate about Front End development, check out the JavaScript Works job-board here! or Purescript jobs on the Functional Works job board.

Did you like this article?

Related jobs

See all

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Related articles

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

12 Sep 2021

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

12 Sep 2021

WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works
hello@works-hub.com

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

108 E 16th Street, New York, NY 10003

Subscribe to our newsletter

Join over 111,000 others and get access to exclusive content, job opportunities and more!

© 2024 WorksHub

Privacy PolicyDeveloped by WorksHub