DEV Community

Cover image for Scope progression
Luis Ángel Méndez Gort
Luis Ángel Méndez Gort

Posted on

Scope progression

In imperative programming, we usually have code that looks the following way:

func addOneToSlice(xs []int) []int {
  rs := make([]int, len(xs))
  for i, value := range xs {
    rs[i] = value + 1
  }
  return rs
}
Enter fullscreen mode Exit fullscreen mode

However, notice the following about the for loop:

  • Each iteration has a specific purpose, which is to add one to the current element.
  • However, each iteration has no constraint on which element it can operate.
  • Operating with xs[i+2] and rs[i+3] wouldn't fundamentally alter the structure of the code we have, while making the end result incorrect.

Compare how the same task would be done in F#:

let rec addOneToList =
  function
  | [] -> []
  | x :: xs -> x + 1 :: addOneToList xs
Enter fullscreen mode Exit fullscreen mode

Now consider the following:

  • We have a list as a function argument.
  • A list in functional languages is a linked list.
  • The efficient and standard operations on linked lists are:
    • Separating the head x from its tail xs
    • Doing something to the head x
    • Comparing the list passed as a parameter with the empty list []

Given these restrictions, adding 1 to any element y not at the head of the list would significantly alter the structure of our function.

Now compare how the computation progresses in both styles:

  • In the functional style, we create a new scope with new values, which involves making a recursive call in the example above.
  • In the imperative style, we mutate an existing value without changing the scope.

In functional style, marrying both scope with computational progress has the following consequences:

  • We avoid mutation.
  • The execution flow is explicit.
  • The structure we are dealing with becomes clear.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay