Exploring functional/tacit style programming, specifically implementing the snake game (example: )
The main issue at hand involves processing an array of strings like:
[
['2 '],
['10']
]
and obtaining a list of coordinates in numerical order of the 'value'. In this context, 0 represents the head of the snake, 2 the tail, and the whole structure represents the board.
With this in mind, the following function was created:
var findSnake = function(rendered_board) {
return r.map(function(x) {
return r.map(function (y) {
return {
x: x,
y: y,
val: rendered_board[x][y]
};
})(r.keys(r.split('', rendered_board[x])));
})(r.keys(rendered_board));
};
As a result, the following output is obtained:
[ [ { x: '0', y: '0', val: '2' }, { x: '0', y: '1', val: ' ' } ],
[ { x: '1', y: '0', val: '1' }, { x: '1', y: '1', val: '0' } ] ]
After sorting, this provides the desired list of coordinates. Despite its functionality, there are concerns regarding coding style.
Is it possible to rewrite findSnake using a point-free style? Are there more idiomatic solutions to this problem?