Lurker News
Preferably an elegant snippet rather than an entire (well engineered) codebase.

Comments (380) 116 threads

Show all comments

Showing thread 28 of 116

neillyons2 replies#
This is very common in Elm but it blew my mind after years of programming in Python. In Python it is very easy to raise an IndexError by getting an element from a list by index that doesn't exist. eg.

    # python
    names = []

    names[0]  # <- raises IndexError
In Elm you are forced to always consider this possibility.

    # Elm
    names = []

    case List.head names of
        Just name ->
            name
        Nothing ->
            "empty"
MawKKe1 reply#
Idiomatic python:

  v = names[0] if names else "empty"
lgas#
Yes, but the point is you can forget the check in python but not in Elm.
dorfsmay#
This always felt weird to me given that Python does a good job with dict:

    d = {}
    assert(d.get("blah"), None)
I was impressed by Rust the first time I run into:

    let mut last;
      for i in &[1, 2, 3] {
          last = i;
      }
    println!("{}", last);

  >> borrow of possibly uninitialized variable: `last`