Upgrade to Pro — share decks privately, control downloads, hide ads and more …

3 types in 5 minutes

3 types in 5 minutes

An introduction to Scala's `Option`, `\/` and `Validation` type

Mathias Sulser

August 05, 2016
Tweet

More Decks by Mathias Sulser

Other Decks in Programming

Transcript

  1. O
    p
    t
    i
    o
    n , ∨ and V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    3 2 types in 5 minutes
    by Mathias Sulser

    View Slide

  2. O
    p
    t
    i
    o
    n
    s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = S
    o
    m
    e
    (
    "
    h
    e
    l
    l
    o
    "
    )
    h
    o
    g
    e
    : S
    o
    m
    e
    [
    S
    t
    r
    i
    n
    g
    ] = S
    o
    m
    e
    (
    h
    e
    l
    l
    o
    )
    s
    c
    a
    l
    a
    > h
    o
    g
    e
    .
    m
    a
    p
    (
    _ +
    + "
    , w
    o
    r
    l
    d
    "
    )
    r
    e
    s
    0
    : O
    p
    t
    i
    o
    n
    [
    S
    t
    r
    i
    n
    g
    ] = S
    o
    m
    e
    (
    h
    e
    l
    l
    o
    , w
    o
    r
    l
    d
    )

    View Slide

  3. O
    p
    t
    i
    o
    n
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u : O
    p
    t
    i
    o
    n
    [
    S
    t
    r
    i
    n
    g
    ] = N
    o
    n
    e
    f
    u
    g
    u
    : O
    p
    t
    i
    o
    n
    [
    S
    t
    r
    i
    n
    g
    ] = N
    o
    n
    e
    s
    c
    a
    l
    a
    > f
    u
    g
    u
    .
    m
    a
    p
    (
    _ +
    + "
    , w
    o
    r
    l
    d
    "
    )
    r
    e
    s
    1
    : O
    p
    t
    i
    o
    n
    [
    S
    t
    r
    i
    n
    g
    ] = N
    o
    n
    e

    View Slide

  4. Option
    s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = S
    o
    m
    e
    (
    1
    0
    )
    h
    o
    g
    e
    : S
    o
    m
    e
    [
    I
    n
    t
    ] = S
    o
    m
    e
    (
    1
    0
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u = S
    o
    m
    e
    (
    2
    )
    f
    u
    g
    u
    : S
    o
    m
    e
    [
    I
    n
    t
    ] = S
    o
    m
    e
    (
    2
    )
    s
    c
    a
    l
    a
    > f
    o
    r {
    | a <
    ‐ h
    o
    g
    e
    | b <
    ‐ f
    u
    g
    u
    | } y
    i
    e
    l
    d a / b
    r
    e
    s
    2
    : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = S
    o
    m
    e
    (
    5
    )

    View Slide

  5. O
    p
    t
    i
    o
    n
    s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = S
    o
    m
    e
    (
    1
    0
    )
    h
    o
    g
    e
    : S
    o
    m
    e
    [
    I
    n
    t
    ] = S
    o
    m
    e
    (
    1
    0
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = N
    o
    n
    e
    f
    u
    g
    u
    : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = N
    o
    n
    e
    s
    c
    a
    l
    a
    > f
    o
    r {
    | a <
    ‐ h
    o
    g
    e
    | b <
    ‐ f
    u
    g
    u
    | } y
    i
    e
    l
    d a / b
    r
    e
    s
    3
    : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = N
    o
    n
    e

    View Slide

  6. O
    p
    t
    i
    o
    n
    two values: S
    o
    m
    e and N
    o
    n
    e
    used for values that can be present ..
    .. or absent
    no more N
    u
    l
    l
    P
    o
    i
    n
    t
    e
    r
    E
    x
    c
    e
    p
    t
    i
    o
    n

    View Slide


  7. This is read as Disjuntion or Either
    First we need some goodies: s
    c
    a
    l
    a
    z
    i
    m
    p
    o
    r
    t s
    c
    a
    l
    a
    z
    .
    _
    , S
    c
    a
    l
    a
    z
    .
    _
    S
    t
    r
    i
    n
    g \
    / I
    n
    t is read as: this thing can be a S
    t
    r
    i
    n
    g or an
    I
    n
    t . One or the other, not both.
    S
    t
    r
    i
    n
    g in this case is the l
    e
    f
    t value and I
    n
    t is the r
    i
    g
    h
    t
    value.

    View Slide


  8. s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e : S
    t
    r
    i
    n
    g \
    / I
    n
    t = 1
    .
    r
    i
    g
    h
    t
    h
    o
    g
    e
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    1
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u : S
    t
    r
    i
    n
    g \
    / I
    n
    t = "
    s
    o
    m
    e
    t
    h
    i
    n
    g e
    l
    s
    e
    "
    .
    l
    e
    f
    t
    f
    u
    g
    u
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = ‐
    \
    /
    (
    s
    o
    m
    e
    t
    h
    i
    n
    g e
    l
    s
    e
    )

    View Slide


  9. s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = 1
    0
    .
    r
    i
    g
    h
    t
    [
    S
    t
    r
    i
    n
    g
    ]
    h
    o
    g
    e
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    1
    0
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u = 2
    .
    r
    i
    g
    h
    t
    [
    S
    t
    r
    i
    n
    g
    ]
    f
    u
    g
    u
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    2
    )
    s
    c
    a
    l
    a
    > f
    o
    r {
    | a <
    ‐ h
    o
    g
    e
    | b <
    ‐ f
    u
    g
    u
    | } y
    i
    e
    l
    d a / b
    r
    e
    s
    4
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    5
    )

    View Slide


  10. s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = 1
    0
    .
    r
    i
    g
    h
    t
    [
    S
    t
    r
    i
    n
    g
    ]
    h
    o
    g
    e
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    1
    0
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u = "
    s
    o
    m
    e
    t
    h
    i
    n
    g e
    l
    s
    e
    "
    .
    l
    e
    f
    t
    [
    I
    n
    t
    ]
    f
    u
    g
    u
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = ‐
    \
    /
    (
    s
    o
    m
    e
    t
    h
    i
    n
    g e
    l
    s
    e
    )
    s
    c
    a
    l
    a
    > f
    o
    r {
    | a <
    ‐ h
    o
    g
    e
    | b <
    ‐ f
    u
    g
    u
    | } y
    i
    e
    l
    d a / b
    r
    e
    s
    5
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = ‐
    \
    /
    (
    s
    o
    m
    e
    t
    h
    i
    n
    g e
    l
    s
    e
    )

    View Slide


  11. disjunctions let us add information to a value
    give a reason if something doesn't exist

    View Slide

  12. From O
    p
    t
    i
    o
    n to ∨
    s
    c
    a
    l
    a
    > v
    a
    l h
    o
    g
    e = 1
    0
    .
    s
    o
    m
    e
    h
    o
    g
    e
    : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = S
    o
    m
    e
    (
    1
    0
    )
    s
    c
    a
    l
    a
    > h
    o
    g
    e \
    /
    > "
    w
    h
    a
    t i
    s t
    h
    i
    s
    ?
    "
    r
    e
    s
    6
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = \
    /

    (
    1
    0
    )
    s
    c
    a
    l
    a
    > v
    a
    l f
    u
    g
    u = n
    o
    n
    e
    [
    I
    n
    t
    ]
    f
    u
    g
    u
    : O
    p
    t
    i
    o
    n
    [
    I
    n
    t
    ] = N
    o
    n
    e
    s
    c
    a
    l
    a
    > f
    u
    g
    u \
    /
    > "
    w
    h
    a
    t i
    s t
    h
    i
    s
    ?
    "
    r
    e
    s
    7
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    I
    n
    t
    ] = ‐
    \
    /
    (
    w
    h
    a
    t i
    s t
    h
    i
    s
    ?
    )

    View Slide

  13. V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    Both O
    p
    t
    i
    o
    n and ∨ are short circuiting.
    They don't accumulate.
    Let's say we want to validate S
    h
    o
    p
    s .
    s
    c
    a
    l
    a
    > c
    a
    s
    e c
    l
    a
    s
    s S
    h
    o
    p
    (
    r
    a
    t
    i
    n
    g
    : I
    n
    t
    , i
    t
    e
    m
    s
    : L
    i
    s
    t
    [
    S
    t
    r
    i
    n
    g
    ]
    )
    d
    e
    f
    i
    n
    e
    d c
    l
    a
    s
    s S
    h
    o
    p

    View Slide

  14. V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    d
    e
    f h
    a
    s
    H
    i
    g
    h
    R
    a
    t
    i
    n
    g
    (
    s
    h
    o
    p
    : S
    h
    o
    p
    ) = {
    i
    f
    (
    s
    h
    o
    p
    .
    r
    a
    t
    i
    n
    g >
    = 4
    ) s
    h
    o
    p
    .
    r
    i
    g
    h
    t
    e
    l
    s
    e "
    l
    o
    w r
    a
    t
    i
    n
    g s
    h
    o
    p
    "
    .
    l
    e
    f
    t
    }
    d
    e
    f h
    a
    s
    E
    n
    o
    u
    g
    h
    I
    t
    e
    m
    s
    (
    s
    h
    o
    p
    : S
    h
    o
    p
    ) = {
    i
    f
    (
    s
    h
    o
    p
    .
    i
    t
    e
    m
    s
    .
    s
    i
    z
    e > 1
    ) s
    h
    o
    p
    .
    r
    i
    g
    h
    t
    e
    l
    s
    e "
    n
    o
    t e
    n
    o
    u
    g
    h i
    t
    e
    m
    s
    "
    .
    l
    e
    f
    t
    }

    View Slide

  15. V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    v
    a
    l b
    a
    d = S
    h
    o
    p
    (
    3
    , L
    i
    s
    t
    .
    e
    m
    p
    t
    y
    [
    S
    t
    r
    i
    n
    g
    ]
    )
    d
    e
    f v
    a
    l
    i
    d
    a
    t
    e
    (
    s
    h
    o
    p
    : S
    h
    o
    p
    ) =
    f
    o
    r {
    _ <
    ‐ h
    a
    s
    H
    i
    g
    h
    R
    a
    t
    i
    n
    g
    (
    s
    h
    o
    p
    )
    _ <
    ‐ h
    a
    s
    E
    n
    o
    u
    g
    h
    I
    t
    e
    m
    s
    (
    s
    h
    o
    p
    )
    } y
    i
    e
    l
    d s
    h
    o
    p
    v
    a
    l r
    e
    s
    u
    l
    t = v
    a
    l
    i
    d
    a
    t
    e
    (
    b
    a
    d
    )
    s
    c
    a
    l
    a
    > r
    e
    s
    u
    l
    t
    r
    e
    s
    1
    2
    : s
    c
    a
    l
    a
    z
    .
    \
    /
    [
    S
    t
    r
    i
    n
    g
    ,
    S
    h
    o
    p
    ] = ‐
    \
    /
    (
    l
    o
    w r
    a
    t
    i
    n
    g s
    h
    o
    p
    )

    View Slide

  16. V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    S
    t
    r
    i
    n
    g \
    / S
    h
    o
    p is not enough
    We need L
    i
    s
    t
    [
    S
    t
    r
    i
    n
    g
    ] \
    / S
    h
    o
    p to accumulate errors
    In fact, N
    o
    n
    E
    m
    p
    t
    y
    L
    i
    s
    t
    [
    S
    t
    r
    i
    n
    g
    ] \
    / S
    h
    o
    p is what we need.
    We can turn S
    t
    r
    i
    n
    g \
    / S
    h
    o
    p into
    V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    [
    N
    o
    n
    E
    m
    p
    t
    y
    L
    i
    s
    t
    [
    S
    t
    r
    i
    n
    g
    ]
    , S
    h
    o
    p
    ] by using
    .
    v
    a
    l
    i
    d
    a
    t
    i
    o
    n
    N
    e
    l
    Finally we can chain validations using |
    @
    | function

    View Slide

  17. V
    a
    l
    i
    d
    a
    t
    i
    o
    n
    d
    e
    f v
    a
    l
    i
    d
    a
    t
    e
    A
    c
    c
    (
    s
    h
    o
    p
    : S
    h
    o
    p
    ) =
    (
    h
    a
    s
    H
    i
    g
    h
    R
    a
    t
    i
    n
    g
    (
    s
    h
    o
    p
    )
    .
    v
    a
    l
    i
    d
    a
    t
    i
    o
    n
    N
    e
    l |
    @
    |
    h
    a
    s
    E
    n
    o
    u
    g
    h
    I
    t
    e
    m
    s
    (
    s
    h
    o
    p
    )
    .
    v
    a
    l
    i
    d
    a
    t
    i
    o
    n
    N
    e
    l
    ) {
    (
    _
    ,
    _
    ) =
    > s
    h
    o
    p
    }
    v
    a
    l r
    e
    s
    u
    l
    t = v
    a
    l
    i
    d
    a
    t
    e
    A
    c
    c
    (
    b
    a
    d
    )
    s
    c
    a
    l
    a
    > p
    r
    i
    n
    t
    l
    n
    (
    r
    e
    s
    u
    l
    t
    )
    F
    a
    i
    l
    u
    r
    e
    (
    N
    o
    n
    E
    m
    p
    t
    y
    [
    l
    o
    w r
    a
    t
    i
    n
    g s
    h
    o
    p
    ,
    n
    o
    t e
    n
    o
    u
    g
    h i
    t
    e
    m
    s
    ]
    )

    View Slide

  18. Thank you very much!

    View Slide