I wanted to practice functional programming (fp) without using any library but using vanilla JS only. So I took a problem from Advent of Code (the 2nd part of Day 5):


--- Part Two --- Now, the jumps are even stranger: after each jump, if the offset was three or more, instead decrease it by 1. Otherwise, increase it by 1 as before.

Using this rule with the above example, the process now takes 10 steps, and the offset values after finding the exit are left as 2 3 2 3 -1.

How many steps does it now take to reach the exit?

I'm doing the 2nd part of Day 5. You can only access the 2nd part once you solved the 1st part.

To access the 2nd part type in this number 376976 or check out the solution here: Advent of Code 2017 Day 5 (part 1) in Functional Programming (FP)

First my procedural solution:

  const parsedInput = input => input
    .map(c => parseInt(c));

  function jumper(maze) {
    let stepNumber = 0;
    let position = 0;
    do {
      const newOffset = maze[position] >= 3 ?
        maze[position] - 1 :
        maze[position] + 1;
      const oldPosition = position;
      position = position + maze[position];
      maze[oldPosition] = newOffset;
    } while (position < maze.length);
    return stepNumber;
  console.log("solution ", jumper(parsedInput(INPUT)));

The code above has got more operation than in the first part of this riddle. But it's way faster and shorter than the functional approach of the 1st problem (Advent of Code 2017 Day 5 (part 1) in Functional Programming (FP)).

My FP solution:

  const parsedInput = input => input
    .map(c => parseInt(c));

  const jumper = input => {
    const currentMaze = input.maze;
    const currentPosition = input.position;
    const currentStepNumber = input.stepNumber;

    if (currentPosition >= currentMaze.length) {

    const newOffset = currentMaze[currentPosition] >= 3 ?
      currentMaze[currentPosition] - 1 :
      currentMaze[currentPosition] + 1;
    const maze = currentMaze
      .slice(0, currentPosition)
      .concat(currentMaze.slice(currentPosition + 1));
    const position = currentPosition + currentMaze[currentPosition];
    const stepNumber = currentStepNumber + 1;
    setTimeout(() => jumper({
      /*removes call stack -> infinite recursion without built in Tail Call Optimization*/
      maze, position, stepNumber
    }), 0);
  const showAnswer = res => console.log(res);
  const startMaze = parsedInput(INPUT);

    maze: startMaze,
    position: 0,
    stepNumber: 0

The FP solution is longer and slower. I couldn't solve it without recursion. After a while you hit the stack size. Thanks to Thanks to @Blindman67 he suggested a workaround with setTimeout.

Do you know an FP approach that is faster and shorter than the procedural one?

  • \$\begingroup\$ your 'procedual' code looks perdy dern functional to me. \$\endgroup\$ – I wrestled a bear once. Jan 15 '18 at 18:48
  • \$\begingroup\$ @Iwrestledabearonce. In the first code I used a loop and changed variables. That's not how I understand FP. \$\endgroup\$ – thadeuszlay Jan 15 '18 at 20:36
  • \$\begingroup\$ FP is not about not changing variables or avoiding loops, it's about being "decalrative" and not causing side-effects. Just clone maze before operating on it and viola, no side effects. Functions like map, reduce, forEach are, internally, loops anyway. \$\endgroup\$ – I wrestled a bear once. Jan 15 '18 at 20:41
  • \$\begingroup\$ if you wanna be trendy you can always assign jumper as an anonymous JS constant. \$\endgroup\$ – I wrestled a bear once. Jan 15 '18 at 20:43
  • 2
    \$\begingroup\$ @Iwrestledabearonce. No, I don't want to be "trendy". Never did. Never will. Just improving my skills as a developer. \$\endgroup\$ – thadeuszlay Jan 15 '18 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.