DEV Community

Dwayne Crooks
Dwayne Crooks

Posted on

Which programming language features do you love and why?

A few of my favorites include: first-class functions, algebraic data types and opaque data types.

First-class functions

Because of all the cool abstractionsmap, filter, accumulate, fold etc — you can build with them.

(define (map f l)
  (if (null? l)
      '()
      (cons (f (car l)) (map f (cdr l)))))
Enter fullscreen mode Exit fullscreen mode

Algebraic data types

Especially sum types. I think all languages should support this feature. They make it really convenient to precisely model your domain.

Ruling out invalid data makes your code shorter, simpler, and more reliable. By making sure the set of possible values in code exactly matches the set of valid values in real life, many problems just go away. — Types as Sets

type RemoteData e a
  = NotAsked
  | Loading
  | Failure e
  | Success a
Enter fullscreen mode Exit fullscreen mode

Opaque data types

Data abstraction allows you to hide implementation details and it makes it favorable to change representations. For e.g. you can start with a naive implementation and change to a more efficient representation if it proves critical to the overall performance of the system. Opaque data types are frequently used to implement abstract data types.

module Stack
  ( Stack
  , empty, push, pop
  ) where

data Stack a
  = Empty
  | Stk a (Stack a)

empty :: Stack a
empty = Empty

push :: a -> Stack a -> Stack a
push x s = Stk x s

pop :: Stack a -> Stack a
pop Empty = Empty
pop (Stk _ s) = s
Enter fullscreen mode Exit fullscreen mode

And you?

Top comments (11)

Collapse
 
dfockler profile image
Dan Fockler

Rust has Enums and Traits which are Sum Types and ADTs in other languages. They are really nice to use because Rust forces you to account for all the possibilities when you match against an Enum. Also Traits allow for a really nice way to extend and use familiar interfaces in new ways. Traits are also used for overloading operators like + or - on custom defined types. Rust's Iterator trait is really interesting and is used in a lot of different places.

Ruby is my main love, so blocks are a really wonderful feature that are similar to lambdas. They are so easy to use and allow the nice functional stuff like map, reduce, etc. Also Ruby's metaprogramming syntax is a little too easy, but is also super powerful when you need it.

Collapse
 
dwayne profile image
Dwayne Crooks

Yeah, Rust is really nice.

I like that you can do systems level programming using high-level programming abstractions. And its borrow checker, I'd love to learn the theory behind it someday.

What are you working on with Rust?

Collapse
 
dfockler profile image
Dan Fockler

The borrow checker is actually surprisingly simple, although the consequences of having it are complex. Basically you can either borrow immutably, borrow mutably, or own data. An immutable borrow can be shared in many contexts at a time, a mutable borrow can only be shared with one context at a time, and ownership means you're giving up that data to another context.

I've been working on a raytracer/pathtracer. Although I'm not very good at the trig yet. I also wrote a Piet language interpreter.

Thread Thread
 
dwayne profile image
Dwayne Crooks

Interesting that you're working on a raytracer. That is the project I plan to do when I decide to learn Rust.

I started implementing one many years ago in C but never completed it. Then, I did one in Python to relearn the concepts but it's obviously slow as hell. Rust would be ideal.

All the best with your raytracer.

Thread Thread
 
dfockler profile image
Dan Fockler

Thanks! You too.

Collapse
 
joanllenas profile image
Joan Llenas Masó

My picks (from Elm):

  • Immutability: Fewer things to worry about.
  • case of expressions and Pattern matching: being able (obligated) to exhaustively check all possible patterns of a given expression makes programs much more robust.
  • ADTs: I can't articulate exactly why. They feel very intuitive and I just love them :)
  • |> operator: Cleaner code by getting rid of the parenthesis.
Collapse
 
pdandy profile image
Andy Thompson

I can live without everything but ADTs and pattern matching. In languages without these features, everything feels so clunky.

Collapse
 
dwayne profile image
Dwayne Crooks

Yes, +1 for immutability and pattern matching on tagged unions as well.

By ADTs do you mean algebraic or abstract data types?

Collapse
 
joanllenas profile image
Joan Llenas Masó

Union Types and Product Types, the ones supported by Elm.

Collapse
 
sebbdk profile image
Sebastian Vargr

Not sure i have a favorite feature, i am more a structure/architecture lover.

But if i had to pick, then i would go back to the basics and say functions.
Simply because i cannot imagine writing software without them.

Everything else can go, but i don't wanna goto'.

Collapse
 
dwayne profile image
Dwayne Crooks

I've found that certain features can lead you to structure your app differently. The two go together in my experience.

... functions.

Definitely. Abstraction is central to problem solving and programming. And functions are the key ingredient.