Skip to content

Instantly share code, notes, and snippets.

View darkslategrey's full-sized avatar

Grégory Faruch darkslategrey

View GitHub Profile
@darkslategrey
darkslategrey / destructuring.md
Created January 9, 2019 07:51 — forked from yang-wei/destructuring.md
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"))
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.0",
"dependencies": {
"direct": {
"dillonkearns/elm-graphql": "4.1.0",
"elm/browser": "1.0.0",
module Cat exposing (Model(..), Msg(..), getRandomCatGif, gifDecoder, init, main, subscriptions, update, view, viewGif)
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode exposing (Decoder, field, string)
module App exposing (Model, Msg(..), init, main, update, view)
-- import Todo.Commands
import Browser
import Cat
import Html exposing (Html, button, div, h1, h2, img, map, text)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick)
[greg@poire infra]$ mix test
Compiling 1 file (.ex)
== Compilation error in file test/infra_test.exs ==
** (CompileError) test/infra_test.exs:12: Infra.__struct__/1 is undefined, cannot expand struct Infra
(ex_unit) expanding macro: ExUnit.Assertions.assert/1
test/infra_test.exs:12: Scraper.InfraTest."test destruct"/1
(elixir) lib/code.ex:677: Code.require_file/2
(elixir) lib/kernel/parallel_compiler.ex:201: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
[greg@poire infra]$
== Compilation error in file test/infra_test.exs ==
** (UndefinedFunctionError) function Scraper.Infra.__using__/1 is undefined or private
(infra) Scraper.Infra.__using__([])
test/infra_test.exs:3: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(elixir) lib/code.ex:677: Code.require_file/2
(elixir) lib/kernel/parallel_compiler.ex:201: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6
defmodule Scraper.InfraTest do
use ExUnit.Case
doctest Scraper.Infra
test "greets the world" do
assert Scraper.Infra.hello() == :world
end
test "destruct" do
defmodule Scraper.Infra do
alias __MODULE__
defstruct name: nil
def bla do
%Infra{name: "default infra"}
end
@moduledoc """
exported_b
===
inside .env