• Stars
    star
    175
  • Rank 218,059 (Top 5 %)
  • Language PureScript
  • Created about 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Purescript for Elm devs. ๐Ÿš€

Purescript for Elm developers


Data types

-- Elm
type Direction  = Up | Down
type alias Time = Int

-- Purescript
data Direction  = Up | Down
type Time       = Int

Lists

Constructing lists.

-- Elm
list    = [1,2,3]
newList = 5 :: list

-- In Purescript, the quickest way to create
-- a list is from a Foldable structure
-- (an Array in this case)
list    = List.fromFoldable [1,2,3]
newList = 5 : list

Pattern matching on lists is almost the same.

-- Elm
case xs of
  []        -> ...
  x :: rest -> ...

-- Purescript
case xs of
  Nil      -> ...
  x : rest -> ...

Be careful! [1,2,3] is syntactic sugar for List Int in Elm but Array Int in Purescript.

Should you use List or Array in Purescript? They perform differently but it shouldn't matter in most cases. Use whatever you like more.

Records

Define a record.

-- Elm
type alias Person = { name : String,  age : Int  }
-- This will also define a function Person in Elm
-- not so in Purescript

-- Purescript
type Person       = { name :: String, age :: Int }

Create a new record.

-- Elm
p = { name = "Bob", age = 30 }
p = Person "bob" 30

--Purescript
p = { name: "Bob", age: 30 }

Edit a record.

-- Elm
edited = { p | name = "Alice" }

-- Purescript
edited = p { name = "Alice" }

Accessing properties is the same in both languages.

p.name

Destructuring is the same.

-- Elm
personName : Person -> String
personName { name } = name

-- Purescript
personName :: Person -> String
personName { name } = name

Aliases for function arguments. In both examples, p still references the whole record.

-- Elm
bumpAge : Person -> Person
bumpAge { age } as p =
	{ p | age = age + 1 }

-- Purescript
bumpAge :: Person -> Person
bumpAge p@{ age } =
	p { age = age + 1 }

Check out Updating records for more info.

Tuples

-- Elm
coords = (10, 20)
(x, y) = coords

-- Purescript
import Data.Tuple

coords    = Tuple 10 20
Tuple x y = coords

Tuples are part of the language in Elm, while they're just a data type in Purescript. This is not that big of a deal, use records when possible.

Imports

In Elm, imports are qualified by default while in Purescript everything from the module is imported.

-- Elm
import Set

a : Set.Set Int
a = Set.empty


-- Purescript
import Data.Set

a :: Set Int
a = empty -- note that empty is coming from the module (Data.Set)

I actually like qualified imports more, so I often alias my imports. This works the same in both languages.

import Data.Set as Set
a :: Set.Set Int
a = Set.empty

It's a little bit cumbersome if you want to have data types and constructors in scope though

import Data.Maybe (Maybe(..))
import Data.Maybe as Maybe

a :: Maybe Int -- note that this is not Maybe.Maybe Int
a = Just 5

-- this does not work in Purescript, wish it did :(
-- that is, you can't import stuff and alias the module
-- in the same import statement
import Data.Maybe as Maybe exposing (Maybe(..))

Importing specific things is the same.

-- Elm
import Set exposing (empty)

-- Purescript
import Data.Set (empty)

-- Set is not in scope in both cases

Default imports

-- In Elm, the Basics package is imported by default
-- it's like every file has this implicit declaration
import Basics exposing (..)

-- In Purescript nothing is imported by default
-- so you have to explicitly import the Prelude
import Prelude

The Prelude isn't imported by the compiler automatically so that it's easier for a team to decide to use something different than the stock one. I get why this choice was made, but it's one more thing to keep in mind that you don't necessarily want to deal with. ๐Ÿ˜›

Also, the default Prelude is rather lightweight, meaning it does not import anything immediately useful (compared to the default imports in Elm). I find myself almost always importing this stuff at the very least.

import Data.Maybe
import Data.Either
import Data.Array as Array
import Data.List (List(..))
import Data.List as List

import Control.Monad.Eff.Console as Console
import Debug.Trace as Debug

This is not a critic, just me being lazy. I guess a more relaxed Prelude will be released and maintained at some point, but nothing stops you from defining your own. By not having a default, you effectively solve the Prelude hell that there is in Haskell, where unsafe functions and obscure choices made 20 years ago are still around to this day for backwards compatibility.

(Full and up to date list of modules automatically imported by Elm can be found here. It's a bit more than just the Basics package)

Type signatures

Type signatures are separated with double colons

sum :  Int -> Int -> Int     -- Elm
sum :: Int -> Int -> Int     -- Purescript

You have to be explicit about the type variables you are using.

map :              (a -> b) -> Maybe a -> Maybe b     -- Elm
map :: forall a b. (a -> b) -> Maybe a -> Maybe b     -- Purescript

What is this forall thing? You might want to read up some stuff about it. This issue is a good start purescript/purescript#766.

There are minor differences with signatures for extensible records.

-- Elm
getAge : { a | age : Int } -> Int
getAge { age } = age

-- Purescript
getAge :: forall a. { age :: Int | a } -> Int
getAge { age } = age

Common packages

Elm Purescript Notes
Array Data.Array
Dict Data.Map
List Data.List
Maybe Data.Maybe
Result Data.Either Err is Left and Ok is Right
Set Data.Set
String Data.String
Debug Debug.Trace Trace.spy is the closest thing to Debug.log

Common Functions

Elm Purescript
identity id
always const
toString show
>> >>>
<< <<<
|> #
<| $

Wait, why there's no Maybe.map?

Because you can use typeclasses!

Typeclasses instead of monomorphic code

I'd say the major difference between the two languages is the level of abstraction they let you work with.

People like to whine about Elm not having type classes, lacking Higher Kinded Types, about it being dumb and stupid... there's a reason why Elm is the way it is. One of the benefits/consequences of not having typeclasses/HKT is that code is going to be simpler (because it's monomorphic) and as a result, the language is much easier to learn for beginners. That's one of the great qualities of Elm and I wish people could just accept not every language is meant to be Haskell.

With that out of the way, Elm code is surely simple, but also quite boilerplate-y. Why is that? Well, because there are very little means of abstractions so some things have to be repeated over and over. Have you ever noticed that map (for example) is defined multiple times in different modules?

map : (a -> b) -> Maybe a    -> Maybe b

map : (a -> b) -> Result x a -> Result x b

map : (a -> b) -> List a     -> List b

The type signatures do look very similar indeed! Can this be abstracted in some form? Well yes, map is in fact the core operation of the Functor typeclass! So exciting.

In Purescript, you will rarely find type signatures this specific. The Data.Maybe module in Purescript does not expose a map function. Instead, an instance for Functor is provided for the Maybe a type.

I don't want to get into typeclasses too much, just know that, in practice, you have a single map operation in Purescript that works for all Functors.

-- one map to rule them all
class Functor f where
  map :: forall a b. (a -> b) -> f a -> f b

You will see different instances of this typeclass for suitable types. Implementation wise, they will look very similar to the monomorphic counterpart in Elm.

The other piece of the puzzle are Higher Kinded Types. You should have noticed that the type signature for map has f a and f b in there. These are not expressible in Elm but are very useful to make our code more abstract and reusable. If we take Maybe as an example, f a and f b become respectively Maybe a and Maybe b , so the type signature is exactly the same we have in Elm!

This is like typeclasses 101 and I realize that without examples this is not very useful. Do your own reading!

More on Typeclasses

How to do some useful things

-- Elm
Just 5
|> Maybe.map (\n -> n + 10)
|> Maybe.withDefault 50

-- Purescript
Just 5
# map (\n -> n + 10)
# maybe 50 id

Just 5
# map (_ + 10) -- cool stuff
# maybe 50 id

maybe 50 (_ + 10) (Just 5)

Nice things unique to Purescript

The Language Reference is a good starting point. The main features to look out for are:

  • _ can be used in functions when you don't want to give the parameter a name. It's like anonymous functions on steroids.
  • Pattern matching can be done at the function level. Make sure you provide a type signature. Take a look at this example to see why. Although it is possible to have partial functions in Purescript, you should obviously strive to write total functions (reminder, you can only define total functions in Elm).
  • Guards are pretty cool and can tidy up your code quite a bit. Read more about Guards.
  • Typed holes are just awesome. In Elm you can use type bombs to achieve similar results, but holes are way more powerful. I suggest you take a look at the tutorial Generating collections of random numbers in PureScript to understand how to use them (read it!).

Stuff you'll often see in Purescript

Or, what's with all the dollars and weird operators?!

The $ operator is just function application. It serves the same purpose of the reverse pipe <| in Elm, that is, avoid parenthesis.

The forward pipe is defined as #. They're exactly the same thing.

If you miss the pipe operators you can easily define them yourself like this:

import Data.Function (apply, applyFlipped)

infixr 0 apply as <|
infixl 1 applyFlipped as |>

<$> is alias for map but in infix position, meaning that the two are equivalent

map (\n -> n + 10) (Just 5)
(\n -> n + 10) <$> (Just 5)

I still prefer using map but the second version is probably more idiomatic? I don't know, use whatever you like ๐Ÿ˜›

Now the following might be a little too much if you haven't played around with this stuff, so I suggest you read about common typeclasses (Functor, Applicative and Monad) and learn a bit of theory so that you can figure out these things for yourself.

<*> is the apply operation for Applicative. The intuition is that <*> is somewhat equivalent to andMap in Elm.

>>= is the bind operation for Monad. The intuition is that >>= is somewhat equivalent to andThen in Elm.

do notation is used to make the code more readable. It's just syntactic sugar. If you ever wrote a slightly complex decoder in Elm, you'd know it's not a piece of cake to pipe your way to the result. do notation helps when you are in an Applicative context (like a Decoder) or a Monadic one (like Task), so that the code you write is more expressive.

If this doesn't make sense, don't worry, you'll figure it out with some practice.

Bower and why you should not care

Yeah Purescript still uses Bower, but honestly, it's not that big of a deal. It actually works quite well and I don't see it as being any worse/better than npm or another package manager. It just downloads stuff, it's fast and has a global cache, that's all I care about.

If you really don't want to use it, check out psc-package and package-sets. They are more akin to the way Stackage in the Haskell world works. Basically a package-set is a list of package versions that are guaranteed to work well together, so as long as you stick to using the packages defined in a specific set, you're good to go because you won't have conflicts among your dependencies. I haven't tried it yet, and I think it is still sort of an experimental thing, but might become standard in the future.

This post has a lot of good info as well: Setting up PureScript in March 2018

Don't feel bad for using Bower. It's fine.

Elm Architecture

It's possible to get a pretty solid Elmish architecture in Purescript today! The library I like the most is purescript-spork. You should take a look at the examples (ported from https://guide.elm-lang.org/) and see for yourself.

Samples applications are available in the elm-architecture folder of this repo.

More Repositories

1

zero-bs-haskell

Learn Haskell, with tiny lessons.
Haskell
558
star
2

quad-ci

A CI server written in Simple Haskell.
Haskell
188
star
3

max4node

Control Ableton Live through Node.js. Yes, holy shit.
CoffeeScript
114
star
4

react-native-media-queries

Media queries like functionality for React Native
JavaScript
72
star
5

elm-hn

An Hacker News clone written in Elm.
Elm
67
star
6

runelm.io

A place to share and run elm code.
JavaScript
25
star
7

tex

Minimalistic file explorer, written in Haskell.
Haskell
17
star
8

elm-date-distance

Date/Time distance helpers: time ago, less than 5 minutes, about 1 month...
Elm
16
star
9

wadoo

An XML/XSLT static site generator written in PHP.
PHP
13
star
10

xpathr

be very very *very* careful
PHP
11
star
11

elm-star-dodge

A simple star dodge game clone.
Elm
11
star
12

purescript-simplecrypto

A set of useful cryptographic utilities for blockchain development.
PureScript
11
star
13

openid_auth

A Symphony CMS extension that allow users to authenticate with their OpenID.
PHP
8
star
14

resave_entries

A simple Symphony CMS extension that let you resave your entries after structural section changes.
PHP
7
star
15

shorten

A Symphony CMS field that shorten entry ids to tiny alphanumeric strings (like u8X).
PHP
7
star
16

node-xonek2

Node api for the awesome XONE:K2 MIDI controller by Allen & Heath
CoffeeScript
5
star
17

elm-mastermind

Mastermind game written in Elm. Play with a runnable demo at https://runelm.io/c/f2t
Elm
5
star
18

elm-memory-game

Memory Game written in Elm. Play with a runnable demo at https://runelm.io/c/fbi
Elm
5
star
19

builder-workspace

PHP
5
star
20

builder

Builder extension for Symphony CMS
PHP
4
star
21

xpathr-old

santa claus is coming to town โ™ซ (he's a bit late)
PHP
4
star
22

purescript-merkle-tree

Merkle Trees in Purescript.
PureScript
4
star
23

image_info

Iptc and Exif metadata for Symphony CMS
PHP
3
star
24

alpacaaa.github.io

HTML
3
star
25

quad-sample-pipeline

JavaScript
3
star
26

elm-rock-paper-scissors

Rock paper scissors game written in Elm. Play with a runnable demo at https://runelm.io/c/9cr
Elm
3
star
27

yothisiscool

Show some love to the amazing people behind cool open source projects!
HTML
3
star
28

trunk.life

There's only one branch that matters #trunklife
CSS
2
star
29

elm-roguelike

Elm
2
star
30

referendum2011

Python
2
star
31

redux-purify

Purify your actions and reducers, no more boilerplate and purified sin free code for ya
JavaScript
2
star
32

simpledb-backup

Simple solution to backup mysql dbs and push them to a remote server (ssh).
PHP
1
star
33

osso

PureScript
1
star
34

haybale

Haskell
1
star
35

purescript-AoC2017

I am perfectly aware I will never complete it.
PureScript
1
star
36

peanocoin

Simple cryptocurrency in Purescript.
PureScript
1
star