I have an array that might be either one-dimensional, two-dimension, or three-dimensional. I'm wanting to check to see if all values in a given direction are constant.
Here are 3 functions:
# Check to see if all values in the first dimension are equal
def constant1D(arr)
arr.inject(true) { |same, val| same and val == arr[0] }
end
# Check to see if all values in the second dimension are equal
def constant2Dy(arr)
arr.inject(true) { |same, x| same and constant1D(x) }
end
# Check to see if all values in the third dimension are equal
def constant3Dz(arr)
arr.inject(true) { |same, x| same and constant2Dy(x) }
end
And here is the code that would be checking:
constantX = constant1D(values)
constantY = ((mapType == :twoD) and constant2Dy(values))
constantZ = ((mapType == :threeD) and constant3Dz(values))
Here are some sample runs:
threeDa = [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[1, 2, 3], [4, 5, 6], [7, 8, 9]]]
threeDb = [[[1, 2, 3], [1, 2, 3], [1, 2, 3]], [[4, 5, 6], [4, 5, 6], [4, 5, 6]], [[7, 8, 9], [7, 8, 9], [7, 8, 9]]]
threeDc = [[[1, 1, 1], [2, 2, 2], [3, 3, 3]], [[4, 4, 4], [5, 5, 5], [6, 6, 6]], [[7, 7, 7], [8, 8, 8], [9, 9, 9]]]
>> constant1D(threeDa)
=> true
>> constant1D(threeDb)
=> false
>> constant1D(threeDc)
=> false
>> constant2Dy(threeDa)
=> false
>> constant2Dy(threeDb)
=> true
>> constant2Dy(threeDc)
=> false
>> constant3Dz(threeDa)
=> false
>> constant3Dz(threeDb)
=> false
>> constant3Dz(threeDc)
=> true
Is this the most "Ruby" way, or is there a better way? E.g., referencing arr[0] in the above has a little bit of code smell to me, but perhaps I'm overthinking it.