Arrays and Procs #2
Description:
Before you start, try the previous kata in the series:
Functions like the one below can be invoked on values as shown:
def func(a)
a + a
end
func(1) #=> 2
func(“str”) #=> “strstr”
func(21) #=> 42
Procs like the one below have a slightly different syntax. For more on that syntax, see the Ruby Proc docs.
func = proc { | a | a + a }
func.call(1) #=> 2
Task:
Your job here is to implement a function (named array_procs
) that will take an array, arr
, and a variable number of procs, procs
, and apply the procs, in order, to their corresponding index in the array. When you run out of procs to apply and there are still array values without procs applied, go back to the first proc and continue from the start. If there are more procs than array elements, leave out the extra ones (from the end). Up to fifteen procs may be inputted. Note that Proc#call
is disabled, so neither of the following will work:
x = proc { | i | i + 2 }
x.(2)
x.call(2)
How the function will be called:
array_procs(an_array, any_number_of_procs1, any_number_of_procs2,...)
Some examples:
arr = [1, 2, 4, 6, 7, 8]
proc1 = proc { | i | i * 2 }
proc2 = proc { | i | i + 1 }
array_procs(arr, proc1, proc2) #=>
[(1 * 2), (2 + 1), (4 * 2), (6 + 1), (7 * 2), (8 + 1)]
[ 2, 3, 8, 7, 14, 9]
array_procs(arr, proc2, proc1) #=>
[(1 + 1), (2 * 2), (4 + 1), (6 * 2), (7 + 1), (8 * 2)]
[ 2, 4, 5, 12, 8, 16]
array_procs(arr, proc2) #=>
[(1 + 1), (2 + 1), (4 + 1), (6 + 1), (7 + 1), (8 + 1)]
[ 2, 3, 5, 7, 8, 9]
array_procs(arr) #=>
[1, 2, 4, 6, 7, 8] since there are no procs to apply on the elements
Also, whenever one of the tests fails, the procs applied will be printed to the console. This is because it is difficult to print the source code for procs, and so the string values of the failed inputs would help.
All input procs will take only one argument, but some procs may take an argument and not use it (for example, proc { | i | 0 + 2 + 22 }
is valid, and should change every value applied to 22). There will also be no need to ensure that the first argument is an array or that the rest of the arguments are proc functions. And again, Proc#call
is disabled.
If you see issues or have any suggestions/ideas at all, please don't hesitate to comment. Otherwise, please rank and mark as ready!
Also check out my other creations: Implement String#transpose, and Implement Array#transpose!
Similar Kata:
Stats:
Created | Oct 25, 2016 |
Published | Oct 25, 2016 |
Warriors Trained | 228 |
Total Skips | 46 |
Total Code Submissions | 177 |
Total Times Completed | 52 |
Ruby Completions | 52 |
Total Stars | 5 |
% of votes with a positive feedback rating | 82% of 25 |
Total "Very Satisfied" Votes | 19 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 4 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |