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
@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?

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:

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 / 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 =
@cscalfani
cscalfani / coContraFunctors.md
Last active January 4, 2021 20:47
Covariant and Contravariant Functors

Covariant and Contravariant Functors

This article contains more than enough examples to hopefully understand Covariant and Contravariant Functors and how to create them for many situtations.

Note that all type annotations use Int as the concrete type but any concrete type would be equivalent as would any mix of concrete types.

It assumes a general understanding of Functor and a basic understanding of Haskell type classes and data types.

Mapping from a to b(#1)

@cscalfani
cscalfani / MonoidsInHaskellAnIntroductions.md
Last active November 14, 2023 09:30
Monoids in Haskell, an Introduction

Monoids in Haskell, an Introduction

Why should programmers care about Monoids? Because Monoids are a common pattern that shows up over and over in programming. And when patterns show up, we can abstract them and leverage work we've done in the past. This allows us to quickly develop solutions on top of proven, stable code.

Add Commutative Property to a Monoid (Commutative Monoid) and you have something that can be executed in parallel. With the end of Moore's Law, parallelism is our only hope to increasing processing speeds.

What follows is what I've learned after studying Monoids. It is hardly complete, but hopefully will prove to be helpful as an introduction for others.

Monoid Lineage

@cscalfani
cscalfani / CompositionWithMultipleParameters.md
Created December 5, 2017 22:29
Functional Composition with Multiple Parameters in Haskell

Functional Composition with Multiple Parameters in Haskell

In the past, I've written composition functions in both Elm and Haskell that take multiple parameters for the leftmost function, i.e. the function that gets applied first.

(All examples here are in Haskell)

Here was my Haskell implemenation (stolen from the web):

compose2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d

Monads of Functions in Haskell

We're all pretty familiar with Monads that contain values, e.g. Maybe a and Either e a. Typically, these are value type, e.g. Maybe String, Maybe Int, Either String Int, Either MyError MyResult.

When fmap is called on a Maybe Int, it is clear that the Int is going to be mapped to its new value when the fmap is evaluated, e.g.:

fmap (* 10) Just 1 === Just 10