code-reviews

mdallastella 2015-06-15T07:58:04.000226Z

Can someone take a look to a little project I made? Is not finished yet, but I’d like to have some feedback. It’s my first almost-complete Clojure software: https://github.com/mdallastella/fen-to-graph

mdallastella 2015-06-15T07:58:15.000228Z

Thanks

slipset 2015-06-15T08:18:23.000229Z

mdallastella coord-list, isn't that a map on a (range 0 9)

slipset 2015-06-15T08:18:39.000230Z

I've never had the ability to read loops in clojure.

mdallastella 2015-06-15T08:22:17.000231Z

@slipset: coord-list should be a vector like [:a8 :b8 :c8 … :f1 :g1 :h1]

mdallastella 2015-06-15T08:22:51.000232Z

probably there are better ways to do it

mdallastella 2015-06-15T08:23:18.000233Z

any idea?

slipset 2015-06-15T08:23:24.000234Z

Yes, I mean a map as in the function map, not the datastructure

slipset 2015-06-15T08:23:52.000235Z

A thing I often look for when code-reviewing javascript is code like:

slipset 2015-06-15T08:25:35.000236Z

function f(ints) {
      var result = [];
      ints.forEach(i) {
          result.push(i++);
     }
     return result;
  }

mdallastella 2015-06-15T08:26:11.000240Z

instead of using map, I see

slipset 2015-06-15T08:26:21.000241Z

exactly

mdallastella 2015-06-15T08:27:04.000242Z

let me try to rewrite it using map

slipset 2015-06-15T08:31:01.000244Z

might just be that map-indexed is what you're looking for

slipset 2015-06-15T08:31:02.000245Z

https://clojuredocs.org/clojure.core/map-indexed

slipset 2015-06-15T08:41:21.000246Z

crap, it might actually be a reduce of some sort, since you might need to keep track of the row you're on 😞

mdallastella 2015-06-15T08:41:32.000247Z

@slipset: what about this:

slipset 2015-06-15T08:41:59.000248Z

(map-indexed (fn [i v] (keyword (str v (mod  (inc i) 8 )))) (flatten (repeat 8 column-list)))

slipset 2015-06-15T08:42:34.000249Z

Doesn't quite get what you want, but with some more math skillz it might work.

mdallastella 2015-06-15T08:43:05.000250Z

(def coord-list
  (flatten
   (map (fn [n] (map #(keyword (str % n)) column-list))
        (range 8 0 -1))))

mdallastella 2015-06-15T08:43:34.000251Z

double map

mdallastella 2015-06-15T08:58:58.000252Z

@slipset what about the rest?

slipset 2015-06-15T09:00:56.000253Z

This is what I was looking for:

slipset 2015-06-15T09:01:07.000254Z

(map #(keyword (str %1 %2))(cycle column-list) (mapcat (partial repeat 8) (range 1 9)))

mdallastella 2015-06-15T09:07:32.000255Z

πŸ‘

mdallastella 2015-06-15T09:38:42.000256Z

@slipset: this is what I need, thanks: (map #(keyword (str %1 %2)) (cycle files) (mapcat (partial repeat 8) (range 8 0 -1)))

mdallastella 2015-06-15T09:38:59.000257Z

from :a8 down to :h1

slipset 2015-06-15T09:48:34.000258Z

cool!

slipset 2015-06-15T09:49:41.000259Z

I guess expand-row-empty-squares could get some of the same treatment

slipset 2015-06-15T09:53:56.000260Z

also, I guess your number->underscore could make some use of -> or ->>

slipset 2015-06-15T09:54:51.000261Z

and why (Integer/parseInt (str n)) shouldn't n suffice?

slipset 2015-06-15T10:48:36.000263Z

mdallastella: kudos for using juxt!

slipset 2015-06-15T10:50:11.000264Z

althugh seems like (filter board/valid-coord? ((apply juxt functions) position)) is repeated across all the pieces?

mdallastella 2015-06-15T10:50:39.000265Z

@slipset: juxt is a mind blowing and amazing function πŸ˜ƒ

mdallastella 2015-06-15T10:51:22.000266Z

Yep, some code can be refactored πŸ˜ƒ

slipset 2015-06-15T11:10:10.000267Z

mdallastella: rook-moves and bishop-moves are basically the same, just a function of how they move? up-left up-right-down-left down-right vs up right down left

slipset 2015-06-15T11:11:15.000268Z

Maybe the pieces could have the allowed moves as a value on them?

slipset 2015-06-15T11:11:53.000269Z

or Piece could have a get-moves which would return [up right down left] for a rook?

slipset 2015-06-15T11:13:00.000270Z

To me it seems a bit strange with a record called Rook and a function called rook-moves, that's sort of what I'm trying to address.

slipset 2015-06-15T11:19:54.000271Z

I guess you could also make a (defmulti valid-moves type)

slipset 2015-06-15T11:20:45.000272Z

and then (defmethod valid-moves fen-to-graph.pieces.King ...)

slipset 2015-06-15T11:21:17.000274Z

But I'm not sure on the stylishness in mixing records/protocols and multimethods

mdallastella 2015-06-15T12:44:20.000276Z

@slipset: as soon as I come home from a customer, I’ll answer you, thanks :simple_smile:

slipset 2015-06-15T12:44:34.000277Z

no worries :simple_smile:

slipset 2015-06-15T12:45:00.000278Z

I like reading code, and especially golfing with other peoples code :simple_smile:

mdallastella 2015-06-15T12:45:15.000279Z

πŸ‘ :simple_smile:

gmorpheme 2015-06-15T21:46:37.000285Z

coord-list is simplest as a for comprehension isn't it? (for [row (range 1 9) col column-list] ...)

gmorpheme 2015-06-15T21:54:22.000286Z

apply concat map in fen.clj can just be mapcat