Is array data structure evil ?steemCreated with Sketch.

in #programming4 years ago (edited)

In functional programming array is not the more used data structure. So the first assumption is that array isn't easy to use in this paradigm.

Not so easy to use

Array is available in the standard library of Ocaml. But when you start to use it, you realize that a lot of functions of the Array module return unit type.

Unit type? What is this beast?

Unit type is a way to indicate that your code rely on a side effect. There is many kind of side effects, here a non exhaustive list :

  • reading data from outside
  • writing data from outside
  • in place modification of a data

Copy on write datastructures is the common way to work in functional programming.

Array rely on in place modifications aka mutation

Mutable data structures are tricky to use because it make composition of function harder. Why?
In place modifications mean that a function line set return nothing, but modify the Array passed in parameter. A function returning nothing isn't useful to compose with another function.

Functional programming focus more on composition than abstraction.

But knowing this I have to reveal an important information : when you start doing functional programming, you use less array data structures when you switch back to Object oriented programming.

Array is so powerful it will eat your RAM

Array has so many qualities :

  • compact representation in memory, it rely on pointer arithmetic
  • direct access to all elements in constant time, for both read and write operations
  • Algorithms are easy to implement with array

image.png

So what is the problem?

Golden Hammer

A lot of algorithm are made for array. So the classical approach for a programmer is :
1 read data
1 load data in an array
1 process data
1 return the result

image.png

The fact is when you start understanding functional programming, you realize that in many case you doesn't need to load data in memory, you learn to work more in stream approach style :
1 read data as a stream
1 process data on the fly while you consume the stream
1 return the result

This approach isn't exclusive to functional programming.

Not so perfect

In many languages, array isn't implemented like in C. So you don't have a compact representation with all data stored in a single allocation of memory, but in a tree or a hash map exposed through an array like api. Why? Generally this is for enabling dynamic size array.

So caution is needed when arrays are dynamic.

The next problem is about in place modifications : we are now in race for more cores availables in our processor. In place modifications dramatically complexify the design of parallel algorithms.

image.png

How to determine when to use array

When you design an algorithm, ask for yourself the following questions :

  • Do I need to load all data in memory ?
  • Will the performance depends on data access time or cleverness of the algorithms ?
  • if I need dynamic size data structures, what will better handle locality of data?

If you challenge more the usage of arrays, you will realize that a lot of your algorithms will become more simple and efficient .

Sort:  

To the question in your title, my Magic 8-Ball says:

Without a doubt

Hi! I'm a bot, and this answer was posted automatically. Check this post out for more information.

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 64401.36
ETH 2627.01
USDT 1.00
SBD 2.83