DEV Community

Matteo De Micheli
Matteo De Micheli

Posted on

Extreme Reusability with Functional Programming & Lodash

There's many ways that people write code. While every coding style has its own strengths & weaknesses, all priotize that the code is reusable, testable, readable and functional (works).

import { flow, map, get, add, reduce } from 'lodash/fp'

const getScore = get('score')

export const countUserScores = flow(
  map(getScore),
  reduce(add)(0),
)

The code above is not only extremely reusable (composed functions) but also elegant. It takes an array of user objects with a score and counts them.

Over the course of my professional career I've come to appreciate the simple nature of functional programming and the inherent testability (no side effects).

Further reading:

Top comments (0)