cgrand / xforms

Extra transducers and reducing fns for Clojure(script)
575 stars 32 forks source link

just a question #21

Closed lifeiscontent closed 6 years ago

lifeiscontent commented 6 years ago

@cgrand sorry to bother you, I'm not familiar with clojure so I was hoping you could explain the concept of how a sort transducer works maybe in pseudo code so I could implement one in JavaScript.

thanks in advance!

cgrand commented 6 years ago

There’s no magic: the whole input is buffered, the sort only occurs when the upstream completes.

cgrand commented 6 years ago

(closing as just a question, you can still comment to ask for more details)

lifeiscontent commented 6 years ago

@cgrand I see, so it's just a lazy transform? is it not composed? e.g. ['b', 'a'].map(x => x.toUpperCase()).sort()

cgrand commented 6 years ago

I don't get the difference you make. With my understanding of composition and laziness: It composes in that when composed with other transducers you get another transducer. (If you put an uppercasing map in front of it, uppercased string will be buffered and sorted on completion and then reemitted in sort order to downstream transducers.) It's not lazy because it' computed as soon as possible, not on demand from the consumer. (However in this case "asap" equals "when all data has arrived".)

On Tue, Mar 6, 2018 at 9:28 AM, Aaron Reisman notifications@github.com wrote:

@cgrand https://github.com/cgrand I see, so it's just a lazy transform? is it not composed? e.g. ['b', 'a'].map(a => a.toUpperCase()).sort()

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cgrand/xforms/issues/21#issuecomment-370702262, or mute the thread https://github.com/notifications/unsubscribe-auth/AAC3sQlIc34fef3GFvBdUaXNJkZ5rNjjks5tbkiogaJpZM4SX5S8 .

-- On Clojure http://clj-me.cgrand.net/ Clojure Programming http://clojurebook.com

lifeiscontent commented 6 years ago

@cgrand so in the case of composed vs not composed.

in a composed set of transforms everything is done in a single loop.

with a set of uncomposed transforms everything happens in a separate loop.

e.g.:

map -- loop 1 sort -- loop 2

I guess what I'm wondering here is if it's possible to both map/sort items within a single loop.

cgrand commented 6 years ago

Transducers introduce state and state introduces time. "Single loop" is a consequence of most states being not long lived. Alas not the case for sort.

It's not possible to sort on the fly (in the general case; when you know more about the input it can be different but in all cases it will introduce some buffering so you still won't have the 1-in 1-out behavior).

On Tue, Mar 6, 2018 at 10:58 AM, Aaron Reisman notifications@github.com wrote:

@cgrand https://github.com/cgrand so in the case of composed vs not composed.

in a composed set of transforms everything is done in a single loop.

with a set of uncomposed transforms everything happens in a separate loop.

e.g.:

map -- loop 1 sort -- loop 2

I guess what I'm wondering here is if it's possible to both map/sort items within a single loop.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/cgrand/xforms/issues/21#issuecomment-370727195, or mute the thread https://github.com/notifications/unsubscribe-auth/AAC3sUt9T-4r9fi0uZLjM5T5bn-0Ez4Xks5tbl3BgaJpZM4SX5S8 .

-- On Clojure http://clj-me.cgrand.net/ Clojure Programming http://clojurebook.com

lifeiscontent commented 6 years ago

@cgrand ah ok, so the sort transducer you've implemented is just a thing to have for composability. Makes sense. Thanks for spending the time to talk over it with me. Much appreciated!