Skip to content

Instantly share code, notes, and snippets.

@cscalfani
cscalfani / typesInElm.md
Last active July 25, 2016 22:03
How to Think About Types in Elm

Union Types in Elm

Elm types have 2 parts:

  1. The name of the type that starts with an uppercase character
  2. The different constructors
type Vehicle
	= Car Int

Default Values in Elm

Using Maybe

In Elm, optional values are handled with Maybe. The following functions show a simple implementation of optional parameters:

combineOpts : Maybe String -> Maybe String -> String -> String -> String
combineOpts = prefix suffix s1 s2 =

Elm Type Signatures Speaks Volumes

When I first started looking at ML-style type signatures, I didn't realize the amount of hidden information is contained within them. But by simply asking a few simple questions and remembering that the functions are pure, you can surmise a lot about the internal workings of the function without ever looking at a single line of code.

Simple Abstract Signatures

Let's start off with a simple signature:

@cscalfani
cscalfani / patternMatchingMakeBug.elm
Last active March 6, 2017 18:42
Elm 0.18 Pattern Matching compiler bug (uses Gigabytes of memory, 100% CPU and forever to compile)
module Bug exposing (..)
{-| Uses tons of CPU, time to compile and Gigbytes of memory. It's exponetially worse the more cases there are.
The gigabytes of memory happen when the first of the 3-tuple is the same. Change the tuples to have the x0, x1, etc as the first and the memory issue disappears but not the CPU or time to compile.
Also starts to be a real problem 3-tuple and higher.
-}
bug : String
bug =

Type Safe JSON Decoding in Elm

The power of a Static Typed language can seem magical at first. But the goal here is to take a tiny peak behind that curtain.

Elm's implementation of JSON parsing is type safe and how it achieves that can seem like a mystery. Even though I got the code to work, it took me a while to fully understand how it works.

I'm writing it down here for 2 reasons. To help others gain a greater understanding of Types and so I don't forget what I learned.

Word of Caution

@cscalfani
cscalfani / FuncApplicationElm.md
Last active June 11, 2018 22:59
Fun with Function Application (Elm version)

Fun with Function Application (Elm version)

Function application operators in Elm make programming more understandable and help reduce parenthesis.

(<|)

The application operator, <|, can be used to reduce the need for parenthesis. The following:

@cscalfani
cscalfani / elmInNode.md
Last active April 6, 2020 20:57
Elm in Node (0.17)

Elm in Node (0.17)

Why?

Sharing code between the client and the server in a Universal Javascript application is a big gain. No more are the days of rewriting code for the server.

But moving from Javascript to Elm in the front end can feel like a move backwards. So much of the code we write is environment independent. And it would be great if we could leverage some of the front end logic on the backend.

How?

module Main where
import Prelude
import Control.Monad.Except.Trans (ExceptT, runExceptT, throwError, catchError)
import Control.Monad.State.Trans (StateT, runStateT, get, put)
import Control.Monad.Writer.Trans (class MonadTell, WriterT, runWriterT, tell)
import Data.Either (Either)
import Data.Tuple (Tuple)
import Effect.Console as Console
const http = require("http");
const hostname = "0.0.0.0";
const port = 3000;
const server = http.createServer((req, res) => {
console.log(`\n${req.method} ${req.url}`);
console.log(req.headers);
const reverseArray = a => {