Skip to content

Instantly share code, notes, and snippets.

@evancz
evancz / union-types.md
Last active March 12, 2018 13:14
Imagine conversations and questions for a time when people say "union type" instead of "algebraic data type". Think about how the responses might work if you said "ADT" instead.

Union Types

If Elm community is going to begin referring to "union types" instead of "algebraic data types" we should think about how that will work in practice. What discussions will we have? Will we find ourselves in awkward spots trying to explain things?

The following question/answer pairs simulate things I'd expect to see in a world of "union types". The pairs are grouped by what background I expect the questions to come from so we know the subtext. I have also added some meta comments in italic to explain my phrasing.

One thing to consider is that a lot of learners will not have a person to ask, so the path to arriving at these answers needs to be easy if you are just searching online.

General Questions

@evancz
evancz / Floatify.elm
Created November 11, 2014 04:10
Trying out different ways to avoid toFloat conversions, based on this discussion (https://groups.google.com/forum/#!topic/elm-discuss/YPA2ww2P9PU)
import Window
import Color
floatify : (Int,Int) -> (Float,Float)
floatify (x,y) =
(toFloat x, toFloat y)
scene (w,h) =
flow down
[ container (floor w) (floor (h / 2)) middle (asText "blue")
@evancz
evancz / Guidelines.md
Last active October 17, 2023 05:36
Some thoughts on how to have nicer discussions online

Towards Discussion Guidelines

I personally like to have discussions in the spirit of the Socratic method. Instead of declaring my opinion, I ask a relevant question. How about this situation? What about this case? This has two possible outcomes.

  1. The other person explains to me how things work in that case. I realize that I misunderstood, and we both come out enriched and in agreement.
  2. The other person realizes that those situations are not covered. They realize they misunderstood, and we both come out enriched and in agreement.

In both cases, it could have been a conflict, egos crashing together. But by asking questions, it becomes a collaboration to find the best answer. Even the simple act of asking a question in the first place says, "I care what you have to say, we can agree on this." That said, I have noticed that it is definitely still possible for things to go wrong within this framework. How can this happen?

There was a passage from [The

Here is one of the changes I needed to make in package.elm-lang.org.

The issue was that the code was relying on linkQualified to always get a non-empty string as the token argument. While it is true that that will happen for the foreseeable future, I took this as an opportunity to do a more serious refactor so that the code was nicer and if it ever breaks in the future it will give me a very specific message.

Before

linkQualified : List String -> String -> Text.Text
linkQualified modules token =
  case List.reverse (String.split "." token) of

Elm 0.15.1 vs 0.16 on Chrome

Result of running these benchmarks on Chrome 45.0.2454.101

These numbers seem to be pretty consistent on Blink-based browsers (Chrome and Opera) but are more like 20% to 50% improvements on FireFox, Safari, and IE. I am not sure how to explain that, so take these numbers more as an indicator of “Elm is generating faster code across the board” as opposed to “Elm is 10x faster!”

business logic from examples

Elm 0.17.1

Install

This is a patch release. Everything is the same, just slightly better!

The main changes are:

  • Default dependencies in elm-package.json are elm-lang/core and elm-lang/html.
  • Error messages for elm-package are improved (e.g. this and this)
import Dom
import Dom.Scroll
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Process
import Task
import WebSocket
import Dict exposing (Dict)
import Html exposing (..)
type Rank = King | Queen | Bishop | Knight | Rook | Pawn
type Color = Black | White
type alias Piece =
{ rank : Rank
port module Spelling exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import String
main =
@evancz
evancz / recursive-values.md
Last active April 2, 2024 20:16
Recursive values in Elm

Recursive Values

In a functional language, writing recursive functions is common, but it is also possible to write recursive values. Here is a simple example of a recursive value:

ones = 1 :: ones

This is an infinite loop. We just keep growing the ones list to infinity. So #873 logged folks running into this and made it an error. This was especially nice for cases like x = x + 1 where folks were expecting Elm to allow mutation.