code-reviews

quoll 2015-06-01T18:14:23.000082Z

I know I’m late to the party, but I was looking at @sickill’s take 2. It’s “nicer” in that it’s based on data structures and lazy seqs, rather than process. But I’m always uncomfortable with using “last"

sickill 2015-06-01T18:14:40.000083Z

quoll: hey :simple_smile:

quoll 2015-06-01T18:14:43.000084Z

unfortunately, my best approach would use loop/recur

quoll 2015-06-01T18:14:45.000085Z

hi

sickill 2015-06-01T18:15:09.000086Z

yeah, my (unpublished) take 3 was with loop/recur actually

quoll 2015-06-01T18:15:16.000087Z

:simple_smile:

sickill 2015-06-01T18:15:32.000088Z

and I'm still torn between these 3

quoll 2015-06-01T18:16:51.000089Z

loop/recur seemed nice to me because it lets you check the upcoming (the second) delay/frame pair, and if they had gone over, then return the first one

quoll 2015-06-01T18:17:35.000090Z

and you only go over the frames once (“last” makes you iterate through them a second time)

sickill 2015-06-01T18:18:08.000091Z

true

quoll 2015-06-01T18:18:50.000092Z

The “loop” form let me unpack heavily. Not sure if that’s good style or not :simple_smile:

sickill 2015-06-01T18:19:01.000093Z

I didn't think of it this way (that last makes it iterate again)

sickill 2015-06-01T18:19:29.000095Z

yeah, I'm not sure either, that's why I asked here 😉

quoll 2015-06-01T18:19:35.000096Z

:simple_smile:

quoll 2015-06-01T18:20:11.000097Z

My loop form was: (loop [[current-delay-frame & [[next-delay next-frame] :as r]] frames t 0] …)

quoll 2015-06-01T18:20:42.000098Z

which is a convoluted unpacking of “frames” :simple_smile:

akiva 2015-06-01T18:21:15.000099Z

reduce/reduced is becoming more common.

quoll 2015-06-01T18:21:51.000100Z

current-frame-delay being the first pair, and next-delay/`next-frame` being the second, with r holding from the second onward. So it’s actually a lot of work hidden in the expression

quoll 2015-06-01T18:22:27.000101Z

reduce is certainly higher level than loop.

akiva 2015-06-01T18:23:12.000102Z

I used to rely on loop/recur a lot but people have often pointed out to me that loop/recur is kind of a last resort sort of thing.

quoll 2015-06-01T18:23:31.000103Z

I’m embarrassed to admit that I was not aware of reductions :simple_smile:

akiva 2015-06-01T18:24:02.000104Z

@quoll: Don’t be embarrassed. There are so many hidden gems in Clojure. Stuff like ffirst and whatnot are just right there but sometimes unseen.

quoll 2015-06-01T18:24:21.000105Z

I prefer not to use loop/recur. OTOH, there is value in it if it lets you define a function more clearly

quoll 2015-06-01T18:25:04.000106Z

I resorted to it this time because it let me avoid the backtracking

quoll 2015-06-01T18:26:28.000107Z

everything I could see that worked on sequences would put me 1 step too far down the seq. @sickill managed it by keeping the head of the discarded seq, and then iterated to the end of it with last

quoll 2015-06-01T18:27:04.000108Z

Ah! I know a technique for dealing with that! Doh!

quoll 2015-06-01T18:29:24.000109Z

it need not be done with this syntax, but work on a structure like: (map vector (rest frames) frames)

quoll 2015-06-01T18:29:48.000110Z

ie. pair up every frame with it’s predecessor

quoll 2015-06-01T18:30:30.000111Z

frames are already 2-element vector pairs, so it’s getting messy by this point :simple_smile: (it’s vectors all the way down!)

sickill 2015-06-01T18:35:27.000113Z

quoll: this pairing (frame with it's predecessor), you would use that with loop/recur or reduce?

sickill 2015-06-01T18:38:19.000114Z

recur is used throughout the whole SICP, you could say the whole book "stands on it", so I guess that's the classic lisp approach

sickill 2015-06-01T18:38:37.000115Z

but not all problems look clean/readable when done with loop/recur imho

sickill 2015-06-01T18:39:45.000116Z

reduce/reduced does the job and it's quick, but it has this imperative break feeling

quoll 2015-06-01T19:04:25.000117Z

sorry, was away

quoll 2015-06-01T19:06:03.000118Z

no, with pairing you can use drop-while with the test on the first element (which is shifted 1 back, via rest), and then the result will be at the head of the remainder, on the second element

sickill 2015-06-01T19:35:07.000119Z

ah, right

quoll 2015-06-01T20:03:34.000121Z

I’ll confess that it doesn’t work all that cleanly. The boundary conditions aren’t handled as well