Skip to content

Instantly share code, notes, and snippets.

@bigsergey
Last active February 10, 2023 06:14
Show Gist options
  • Save bigsergey/b57c566494d0263da1721425e2846d65 to your computer and use it in GitHub Desktop.
Save bigsergey/b57c566494d0263da1721425e2846d65 to your computer and use it in GitHub Desktop.
Exercises for learning functional programming in JS

Exercises for learning functional programming in JS

During implementation you can use libraries like ramda, lodash, underscore, etc. For each exercise you have to write tests.

Link to presentation: Functional programming in Javascript

First exercise - Reduce

Implement forEach, map, filter, reduceRight and some using only reduce.

Second exercise - Currying

Using only currying and split make a function called "words" that returns a list of words in a string. Split function is in Ramda R.split or in lodash _.split or you can use any another function that works similar to JavaScript String split() method.

words('a b c'); // ['a', 'b', 'c']

Using only add and map functions create a function that increases every number in array.

function add(a, b) {
  return a + b;
}

Make a function to find the smallest number in an array. Use only less function and one of the listed below functions: map, filter, reduce.

function less(a, b) {
  return a < b ? a : b;
}

Third exercise - Recursion

Implement a recursive version of the flatMapDeep function. Implement a recursive version of the forEach function.

Fourth exercise - composing, currying, streams(?), etc.

Using Web Service of "MusicBrainz project" do following tasks:

  • Display average length of tracks in the release and in the every single disk. Link to data.
  • Display album titles list with release date. Albums can not be of a type "compilation". The list should be sorted by the release date of the album. Link to data.
  • Present the results in a fancy way! The better, the more points you will get.
  • Additional task: try to get any additional/interesting information from the database using as many functions as possible from lodash or Ramda.

Fifth exercise (extra credit)

Using only reduce implement functions: sum, any, max, concat. Extract the common parts of this, propose API, describe the structure of common parts.

Project assessment criteria

We will evaluate the implementation of projects as follows:

  • Code is written in a function style or not? Pure, pointfree, etc.
  • Quality of tests.
  • Team ingenuity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment