Giter VIP home page Giter VIP logo

Comments (4)

timoxley avatar timoxley commented on May 19, 2024

Good point, though it doesn't really matter that much, there are multiple possible solutions:

As you suggested:

function repeat(operation, num) {
  if (num <= 0) return
  operation()
  return function() {
    return repeat(operation, --num)
  }
}

I think I prefer this though:

function repeat(operation, num) {
  return function() {
    if (num <= 0) return
    operation()
    return repeat(operation, --num)
  }
}

and the current solution looks like:

function repeat(operation, num) {
  if (num <= 0) return
  return function() {
    operation()
    return repeat(operation, --num)
  }
}

from functional-javascript-workshop.

AndyHoang avatar AndyHoang commented on May 19, 2024

Could you please explain the repeat function?
As the exercise before it (https://github.com/timoxley/functional-javascript-workshop/tree/master/problems/blocking_event_loop)
As for as I know, repeat seem like to call an action several times. So in this ex, we return a function, and inside of this function, we call operation() one time, then try to call a repeat num-1 times again.
So how exactly operation was called?
Normally I try to write some test case for testing, but I dont know how to write for this one, as a result I don't understand at all :(
The blocking event loop ex, I barely understand it. Maybe it's the reason I couldn't get this one. Could you point me some more tut or exercise about this blocking event loop? (I read this one #62)
I lost here :(

from functional-javascript-workshop.

robinpokorny avatar robinpokorny commented on May 19, 2024

I do not like the return repeat(operation, --num) line. To be precise I do not like the return keyword. It is not needed and it is not present in current official solution. Shouldn't it be removed?

from functional-javascript-workshop.

nsubordin81 avatar nsubordin81 commented on May 19, 2024

I'm was confused about this one. I wasn't able to figure out the trampoline solution without looking up some context about why we use it in javascript. The way I understand it now, one of the main reasons is to make up for lack of tail-call optimization in javascript. For this problem, that would mean even though repeat's last call is the recursive one, the interpreter will still spin up a new stack frame for each recursive invocation.

Going off of other sources where I was reading about this, I arrived at a solution that uses bind() to ensure that the recursive call execution is deferred until trampoline invokes it within the loop. However, the official solution did not seem to need this to prevent the stack overflow and I am not sure why.

I think I get the main point that each step of the recursion should be executing rather than setting up a bunch of placeholders until we get to the base case, but with the resources and instructions provided, I was not as clear on why the stack overflow would be happening and how this solution prevents it

Here was my solution which worked but seems to have unnecessary elements to it:

function repeat(operation, num) {
  // Modify this so it doesn't cause a stack overflow!
  if (num <= 0) return
  operation()
  return repeat.bind(null, operation, --num)
}

function trampoline(fn) {
  while(fn && fn instanceof Function) {
    fn = fn()
  }
  return fn
  // You probably want to implement a trampoline!
}

module.exports = function(operation, num) {
  // You probably want to call your trampoline here!
  return trampoline(repeat.bind(null, operation, num))
}

and for contrast the official solution:

function repeat(operation, num) {
      return function() {
        if (num <= 0) return
        operation()
        return repeat(operation, --num)
      }
    }

    function trampoline(fn) {
      while(fn && typeof fn === 'function') {
        fn = fn()
      }
    }

    module.exports = function(operation, num) {
      trampoline(function() {
        return repeat(operation, num)
      })
    }

from functional-javascript-workshop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.