Skip to content

Instantly share code, notes, and snippets.

View pootsbook's full-sized avatar

Philip Poots pootsbook

  • ClubCollect
  • Alphen aan den Rijn, NL
View GitHub Profile

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

import Html
import Html.App as App
import Html.Attributes as Attributes
import Html.Events as Events
import Json.Decode as Json
import String
main =
App.beginnerProgram
@pdamoc
pdamoc / Pong.elm
Last active October 16, 2016 20:07
Pong Example
-- See this document for more information on making Pong:
-- http://elm-lang.org/blog/pong
import Color exposing (..)
import Collage exposing (..)
import Element exposing (..)
import Keyboard
import Text
import Time exposing (..)
import Window exposing (Size)
import Html.App as App
@yang-wei
yang-wei / decode.md
Last active April 2, 2024 20:18
Elm Json.Decode tutorial and cheatsheet

When receiving JSON data from other resources(server API etc), we need Json.Decode to convert the JSON values into Elm values. This gist let you quickly learn how to do that.

I like to follow working example code so this is how the boilerplate will look like:

import Graphics.Element exposing (Element, show)
import Task exposing (Task, andThen)
import Json.Decode exposing (Decoder, int, string, object3, (:=))

import Http
@yang-wei
yang-wei / destructuring.md
Last active February 20, 2024 04:40
Elm Destructuring (or Pattern Matching) cheatsheet

Should be work with 0.18

Destructuring(or pattern matching) is a way used to extract data from a data structure(tuple, list, record) that mirros the construction. Compare to other languages, Elm support much less destructuring but let's see what it got !

Tuple

myTuple = ("A", "B", "C")
myNestedTuple = ("A", "B", "C", ("X", "Y", "Z"))
@mgold
mgold / SuperTimer.elm
Created February 12, 2016 14:40 — forked from shamrin/SuperTimer.elm
Restartable timer
module SuperTimer where
import Html exposing (..)
import Html.Events exposing (onClick)
import Time exposing (Time)
import Signal exposing (Address)
type Action = Tick Time | Toggle Bool | NoOp
type alias Model = { running: Bool, count: Float}
-- interface is a more familiar name for the intention of the construct
type alias Append a rest = { rest | append : a -> a -> a}
-- implement is a more familiar name
stringAppend rest = {rest | append = \x y -> x ++ y}
-- this is Monoid
type alias AppendWithId a rest = Append a {rest | id : a}
-- ^ 1 potential superclass
@JoshCheek
JoshCheek / learning_elm.md
Last active February 18, 2017 22:50
Learning Elm

Learning Elm

What is it? (20 min)

Glance through a few of the examples, maybe 5 or so, to see what kinds of things Elm is doing, what domains they're in, what the code looks like, what other people think Elm is.

@manigandham
manigandham / rich-text-html-editors.md
Last active March 13, 2024 23:51
Rich text / HTML editors and frameworks

Strictly Frameworks

Abstracted Editors

These use separate document structures instead of HTML, some are more modular libraries than full editors

module Main where
import Signal
import Time exposing (Time, millisecond)
import String
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Effects exposing (Never, Effects)