So I've been struggling with this for a few hours now. I have this array [[[4,2]],[[1,2],[1,1]]]
and I'd like to transform this array into [[[4,2]],[[1,3]]]
.
So a function with type f :: [[[Integer]]] -> [[[Integer]]]
Problem
I have a 2d array with inner arrays of length 2: [[x,y] .. ]
An inner array is a duplicate if its head element is repeated: [[1,2],[1,1]]
If there are duplicates I want to take the sum of all the tails and create a new array with the head as the duplicate value and the sum of duplicates as the tail value: [[1,2],[1,1]]
becomes [[[1,3]]
What I have
dup [x,_] [y,_] = x == y
sample = [[[3,5],[2,3],[1,1]],
[[3,5],[2,3],[4,2],[1,2]],
[[3,5],[2,3],[4,2],[1,2]],
[[4,2],[1,2],[1,1]]]
ifDuplicateGroup = map (groupBy dup) sample
getSumOfDups n = map sum [concat $ map tail y | y <- n, (length y) > 1]
sumOfSample = map getSumOfDups sample
Returns:
sumOfSample = [[],[],[],[3]]
Desired Result:
sumOfSample =
[[[3,5],[2,3],[1,1]],
[[3,5],[2,3],[4,2],[1,2]],
[[3,5],[2,3],[4,2],[1,2]],
[[4,2],[1,3]]]`
this is the best I could work through. Please help! I cant figure out how to get the desired result.