Comments (4)
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.
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.
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.
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)
- Item 4) Why aren't anonymous arrow functions being proudly advertized?
- submittedUser is undefined every once in a while
- exersise #7 Basic: Recursion code bug
- Replace npm package dependency sprintf -> sprintf-js HOT 3
- Why is < 10 operations considered a failure for Exercise 13?
- ES6 Purely Functional Solution For Exercise #16
- Using workshops locally HOT 1
- Exercise 15 solution not fully functional without side effects HOT 3
- async_loops solution accepts any solution that runs HOT 3
- npm WARN deprecated [email protected]: no longer maintained
- Partial Application without Bind ERROR HOT 2
- Exercise 1 - Hello World HOT 3
- Thank you!
- implement_map_with_reduce Boiler Plate is Incorrect HOT 1
- problem with installing the workshop
- Error on running npm install -g functional-javascript-workshop@latest HOT 1
- [Lecture: Basic: Every Some] The result of exercise is returned PASSED although the solution is not correct
- basic recursion - simpler solution HOT 1
- Basic: Call exersion - a question about no using `prototype` HOT 1
- [Partial application without Bind] Why a few of test cases are passed, some are failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functional-javascript-workshop.