javascript - get positions of given elements in a 2d array in Ramda -
i have array data, ie.
const data = [[ , '+', ], ['+', , '+'], [ , , '+']]
i'd [x, y] coordinates of every element '+':
const expected = [[1, 0], [0, 1], [2, 1], [2, 2]]
how can in ramda?
i knocked out code works:
const gethorizontalposition = r.map(r.addindex(r.reduce) ( (xs, el, idx) => r.equals('+', el) ? r.append(idx, xs) : xs, [] )) const combinewithverticalposition = r.addindex(r.chain) ( (xs, y) => r.map( (x) => [x, y] )(xs) ) pipe(gethorizontalposition, combinewithverticalposition)(data)
but, can see, it's ugly , not readable. how can more elegantly?
as may have noticed, ramda not index handling. can't come particularly nice solution.
something work, seems less elegant 2 reducers 1 mentioned in comment:
pipe(addindex(map)((xs, i) => pipe( map(equals('+')), addindex(map)((x, j) => x && [i, j]), reject(equals(false)) )(xs)), unnest)(data)
you can see on ramda repl.
Comments
Post a Comment