5 kyu
Lazy evaluation
1,792 of 1,795surtich
Loading description...
Functional Programming
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
should use more recent JavaScript (classes, rest parameters). i'd do it, but regular users cannot edit katas anymore ...
Preferred way to fix a kata would be to fork it (you know how to fix a current state of a kata, do you?), and announce the fork as usual with a suggestion. Then it can be approved as any other fork.
Since forking kata has been available, it's not really necessary to edit approved kata directly anymore (unless you think otherwise?)
The difference is that editing fixes the kata now, while forks can stay unapproved forever, because reviewing takes time and motivation, and is only possible for users who already solved the kata.
I used to upgrade language versions and improve assertion messages from time to time after solving a kata, now I dont think I will bother doing that anymore.
I understand that the change has probably been implemented to prevent malicious or confused users from ruining katas, and it's a valid reason, but on the other hand it can delay fixes and upgrades for a long time.
I guess there is some mistake in the last example in 'Test Reuse lazy filterNumbers + filterRange + max function' section. I understood it when I was exploring the output. I realized that it is as in the description, but the correct result is different. I mean, in the description '6' is received but in the example '1'. Can you fix it or explain me what I understood wrong?
Not a kata issue! The example in description uses a
filterRange
of2
to7
whereas the test case you're encountering one uses afilterRange
of1
to3
.I believe one of the test cases might be wrong. Test Reuse lazy filterNumbers + filterRange + max function, 2nd one. My function passes every one except for this one, here are the results for each stage: [Function: filterNumbers] arguments - [ 1, '3', [ 2 ], {}, 4, 2, 1, 8, 6, [], '7', -1, { v: 5 }, 4 ] result - [ 1, 4, 2, 1, 8, 6, -1, 4 ], [Function: filterRange] arguments - [ 1, 3, 1, 4, 2, 1, 4, 2, 1, 8, 6, -1, 4 ] result [ 1, 2, 1, 2, 1 ], [Function: max] arguments - [ 1, 2, 1, 2, 1, 2, 1 ] result 2 Everything seems correct but for some reason the expected output in the last one is 1, not 2. Might there be a mistake here?
as the name of the tests imply, your solution has to be reusable, i.e.
invoke()
should behave correctly when called several times on the sameLazy
objectAll tests are running successfully, but after one particular an error is thrown: 'Test Reuse lazy filterNumbers + filterRange + max function'. So what's the idea of that test, and why an error is thrown despite test value and value from solution are equal?
yeah, i believe i'm talking about the same one just above. noone cares
Thanks for the awesome kata! I learned a lo tfrom it:)
Specify the invoike 'target' parameter will always be an array
Done!
Great kata, but why do you pass a list of arguments into the invoke function as array .invoke([arg1, arg2 ...])? I guess it would be better to pass arguments as usual .invoke(arg1, arg2 ...)
Maybe I could have done it like you are suggesting. I think now it would be undesirable because it would require a change to refactor the solution to twelve people. Thanks for the suggestion.
This comment has been hidden.
Updated solution with closures
Those methods are the functional way. I'm glad you could solve it.
I appreciate you mark the kata as ready and give it your rank assessment.
It's kind of unclear what behaviour is expected from the description. I had to figure it out from the example code and test errors. Ideally, a description should describe the problem in full.
Also, I'm not enitrely sure this is even lazy evaluation. The point of lazy evaluation is so that functions can be combined and used in the middle of a loop, instead of iterating over everything mutliple times, or creating unnecessary arrays and such. The example code provided makes this impossible and so, while I completed the kata, I didn't really do any of the lazy-eval type stuff.
Thanks, wthit56, for your commet.
I have tried to explain the description a little more.
About what lazy evaluation means, I have used then explanation of Functional JavaScript book.
I thinkt the problem is more what calls are made in qhat order and with what arguments from where. Below, I've added comments to show exactly how it should work; this might make things clearer.
Thanks a lot. I have added your commet to the kata description.
Seems like something's gone wrong in the copy. The commented lines are out of place and pointing at the wrong lines. @.@"
My mistake. Sorry and thanks again!
Cool. Happy to help ^^
I think that solutions which use an array internally to store functions, and modify this array on add(), miss the point of reusability. In my opinion reusability is achieved when add() returns a new instance, without altering the previous one, so both can be used. I'd suggest to add a test which tests for reusability, something like: var smallNumbersOnly = new Lazy().add(filterNumbers).add(filterRange, 2, 7); var maxNumber = smallNumbersOnly.add(max); smallNumbersOnly.invoke([1,8,"6",4,7]) == [4,7] maxNumber.invoke([1,8,"6",4,7]) == 7
I know what you're saying, but it seems like that wasn't the intent of the original kata. Maybe the author could reword things to be clearer on what he means by "reusability."