6 kyu
Array#reduce
4,219 of 4,393user578387
Loading description...
Functional Programming
Algorithms
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.
https://www.codewars.com/kumite/645eae46d3560e0013c3c01e?sel=645eae46d3560e0013c3c01e
Here is a translation to update the
assertSimilar
calls toassertDeepEquals
, in order to remove warnings when running the code.Please feel free to review and approve. Thanks.
approved
This comment has been hidden.
No random tests
Node 12 should be enabled
Expected: 'fallaffles', instead got: 'undefinedaffles'.
JavaScript at its finest :)
This does not work like the real reduce as the top answer would ignore falsy initial values and assign the first element instead.
It also does not test the callback to make sure it has the standard four arguments.
Need more test cases.
The initial test in coffeescript is wrong. It reduces to "say!" but tests against "yay!".
Fixed. Thanks for reporting.
I learned a lot about reduce from the famous paper why functional programming matters. It's really a awesome paper. The reduce in js and this kata is somehow not exactly the reduce explained in the paper.
This comment has been hidden.
Thanks! Adding right now.
This comment has been hidden.
If I call
[1,2,3].reduce( f, nil )
it should dof( f( f(nil, 1), 2), 3)
- Your function seems to treatnil
as the test for an undefined starting point, but that shouldn't be the case.For
[1,2,3].reduce( f, nil )
:f.( f.( f.( nil, 1), 2), 3)
f.( f.( 1, 2), 3)
I see it now. Thanks for the clarification!
This comment has been hidden.
Hm, I think I overkilled! I'll sort that out.
EDIT: I originally implemented that last test to sort out people who had copied from somewhere, but I havn't seen many of those, so I'll leave it.
Most solutions handle
nil
as a second argument incorrectly.[true].reduce( ->(m, s) { m && s }, nil)
should benil
, but these solutions returntrue
. Perhaps a test should be added for this.Added!
This comment has been hidden.
Implemented, thanks!
I think
Array#reduce
should not modify array objectThis comment has been hidden.
Undefined
Array#inject
.Actualy, reduce callback function in javascript is called with four parameters - accumulator, element value, element index and reference to array. Maybe you implement this for javascript and coffeescript (but not for ruby)? And also give a link to reduce method documentation in kata description.
Good suggestion, but for simplicity's sake, we'll keep the kata specification as it is. Besides, I've found people cheat by copying the code on Mozilla, so having a case the same as that would be hard to regulate!
This comment has been hidden.
Resolved!
You should do some order-specific tests, and make sure the correct values are passed to the
process
function, and in the correct order.You could also add tests for when no
initial
value has been given, or even when noprocess
was given. (Maybe expect an error?)I like the idea of
initial
value defaults, but how do you suggest testing order-specificity?(Edit: implemented
initial
defaults)Well, in the code:
we expect the following calls:
If these calls are made out of order, or some are missing, or the result is different, the reduce method doesn't work properly.
As for an unspecified
initial
value, you should go by the JS spec for the reduce method to reduce confusion. Basically, the initial value would be assumed to be the first value in the array, so:Allright, I think the string tests work well as order tests, and the default
initial
is implemented: resolved!I cheated - you might want to disable
Array.prototype.reduceRight
as well.Resolved, thanks for telling me!