A very simple introduction to Functional Programming

If you’ve heard the “buzzwords” Functional Programming (FP), composition, point free, etc, and you were completely lost… you’re NOT alone. Here’s a sneak peak of the things you can do if you compose things and follow a functional and point free style o…


This content originally appeared on DEV Community and was authored by sigfried

If you’ve heard the "buzzwords" Functional Programming (FP), composition, point free, etc, and you were completely lost… you’re NOT alone. Here’s a sneak peak of the things you can do if you compose things and follow a functional and point free style of programming.

const _ = require('lodash/fp');

const lines = [
  {
    name: "We're the eggmen",
    order: 2,
  },
  {
    name: "I'm the eggman",
    order: 1
  },
  {
    name: "I'm the walrus",
    order: 3
  }
]

function main(lines) {
  // generateSelectObject :: Object -> Object
  const generateSelectObject = ({ name, order }) => ({
    value: `${name}_${order},
    label: name,
  });

  const sortAndMapLines = _.compose(
    _.map(generateSelectObject),
    _.sortBy(['order'])
  )

  const orderedLines = sortAndMapLines(lines);

  orderedLines.unshift({
    label: 'All lines',
    value: 'All lines'
  })

  return orderedLines;
}

const res = main(lines);
console.log(res)

I’ll argue that the most interesting part of this boring code is this one:

const sortAndMapLines = _.compose(
    _.map(generateSelectObject),
    _.sortBy(['order'])
  )

This is what FP is all about, you define the steps you need in order to achieve something, in this case the sorted and then mapped results of lines. Notice we’re composing two functions there, the sort and the map from Lodash and it’s point free because neither function declares explicitly with what data they will work with.

Hopefully this rambling is helpful and it will whet your appetite to search for better ways to do your work and improve the overall quality of our code. A very good place to start with is Prof. Frisby's Mostly Adequate Guide to Functional Programming which I very much recommend.


This content originally appeared on DEV Community and was authored by sigfried


Print Share Comment Cite Upload Translate Updates
APA

sigfried | Sciencx (2021-08-24T23:54:33+00:00) A very simple introduction to Functional Programming. Retrieved from https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/

MLA
" » A very simple introduction to Functional Programming." sigfried | Sciencx - Tuesday August 24, 2021, https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/
HARVARD
sigfried | Sciencx Tuesday August 24, 2021 » A very simple introduction to Functional Programming., viewed ,<https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/>
VANCOUVER
sigfried | Sciencx - » A very simple introduction to Functional Programming. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/
CHICAGO
" » A very simple introduction to Functional Programming." sigfried | Sciencx - Accessed . https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/
IEEE
" » A very simple introduction to Functional Programming." sigfried | Sciencx [Online]. Available: https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/. [Accessed: ]
rf:citation
» A very simple introduction to Functional Programming | sigfried | Sciencx | https://www.scien.cx/2021/08/24/a-very-simple-introduction-to-functional-programming/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.