I don't think using lambdas over regular function definitions gets you much here. If your lambda if split over four lines, the extra return is not a big deal. Using function alone will make the code look much more familiar.
Second, assuming you're targeting modern JavaScript (or have polyfills available), you don't need to use lodash. filter and map are already a standard members of arrays.
Also, you really should document your functions' purposes. I can't really figure out what your shuffle, though it does appear to be mutatingboard which is not very functional. Where you are mutating objects, you should probably stick to an imperative style.
Also, you should probably not use [0] and [1]; use .x and .y since that's what you mean!
using lambdas over regular function definitions gets you much here. If your lambda if split over four lines, the extra return is not a big deal. Using function alone will make the code look much more familiar.
Also, you really should document your functions' purposes.
Agree. I wrote this code yesterday, haven't merged yet. At that time I just want to prototype as fast as possible, so less keystroke is a thing :)
you don't need to use lodash
Yes... I just get used to it than native Web API. Also AFAIK, Web API doesn't have range() and few other things.
you should probably not use [0] and [1]; use .x and .y since that's what you mean!
My bad!
Where you are mutating objects, you should probably stick to an imperative style.
How can I write it without this shit var i=0; i<50; i++ ?
for (let i = 0; i < count; i++) {
random_move(board);
}
Is probably the clearest way to write it. While for loops aren't very aesthetic, they are simple and very clear, and easily recognizable to JavaScript programmers.
If you really wanted to get rid of it, you could use something like
for (let i of _.range(count)) {
but since this isn't an established idiom, it takes more mental effort to read even if it is prettier.
I notice that random_move is actually doing too much. It shouldn't choose a move and execute it; it should just choose a move. The shuffle function body is the place where you should call process_move.
A few minor things could help:
I don't think using lambdas over regular
function
definitions gets you much here. If your lambda if split over four lines, the extrareturn
is not a big deal. Usingfunction
alone will make the code look much more familiar.Second, assuming you're targeting modern JavaScript (or have polyfills available), you don't need to use lodash.
filter
andmap
are already a standard members of arrays.Also, you really should document your functions' purposes. I can't really figure out what your
shuffle
, though it does appear to be mutatingboard
which is not very functional. Where you are mutating objects, you should probably stick to an imperative style.Also, you should probably not use
[0]
and[1]
; use.x
and.y
since that's what you mean!Agree. I wrote this code yesterday, haven't merged yet. At that time I just want to prototype as fast as possible, so less keystroke is a thing :)
Yes... I just get used to it than native Web API. Also AFAIK, Web API doesn't have
range()
and few other things.My bad!
How can I write it without this shit
var i=0; i<50; i++
?Many thanks anyway :)
I think
Is probably the clearest way to write it. While
for
loops aren't very aesthetic, they are simple and very clear, and easily recognizable to JavaScript programmers.If you really wanted to get rid of it, you could use something like
but since this isn't an established idiom, it takes more mental effort to read even if it is prettier.
I notice that
random_move
is actually doing too much. It shouldn't choose a move and execute it; it should just choose a move. Theshuffle
function body is the place where you should callprocess_move
.:)
Oh yes. Thank you!