I recently I ran into a problem in Erlang project and I had to find a way to Remove multiple spaces in string and leave only one one space.

Some string with     extra        spaces.


Here is solution how to remove it with simple re:replace

re:replace("Some string with     extra        spaces.", "(\\s+\\s+)", " ", [global,{return,list}]).

and the returned output is:

Some string with extra spaces.

Useful links:


Return true if all function returns are true.
If there is at least one false returns false.

Similar to And function but gives the opportunity to execute an additional function

Val1 And Val2 And Val3 ...

Returns true when:

(true And true And true And true) = true

Returns false when:

(true And false And true And true) = false

Erlang code:

Verdicts = [true, true, false, true].
Verdict = lists:all(fun(Elem)->Elem end, Verdicts).



returns the element of a list

More complex example:
We have a list with tuples:

{day_of_week_atom, Temeprature_value}

and we want to check if it was a warm week (temperature above 20 degrees of Celsius)

List = [{monday,21},{tuesday,22},{wednesday,6},{thursday,22},{friday,21}].
	lists:all(fun({_,Temperature})-> Temperature >= 20 end, List).

Function is executed on each element of a list until first false is returned.
So for above example it will be executed for Monday, Tuesday and Wednesday