Elm types have 2 parts:
- The
name
of the type that starts with an uppercase character - The different
constructors
type Vehicle
= Car Int
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.
Let's start off with a simple signature:
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 = |
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.
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.
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 => { |