Back to front pageView on Hacker News (opens in a new tab)Ask HN: What is the most beautiful piece of code you've ever read?476 pointsby seisvelas (on Hacker News)7 years agoPreferably an elegant snippet rather than an entire (well engineered) codebase.Comments (380) 116 threadsShow all commentsShowing thread 28 of 116[-]neillyons7 years ago2 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"[-]MawKKe7 years ago1 reply#Idiomatic python: v = names[0] if names else "empty"lgas7 years ago#Yes, but the point is you can forget the check in python but not in Elm.dorfsmay7 years ago#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`
Ask HN: What is the most beautiful piece of code you've ever read?476 pointsby seisvelas (on Hacker News)7 years ago
neillyons7 years ago2 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"
dorfsmay7 years ago#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`
Comments (380) 116 threads
Show all comments
Showing thread 28 of 116