Fork me on GitHub
#core-async
<
2017-10-17
>
tanzoniteblack17:10:38

Inspired by the youtube video above, I'm trying to re-write some sections of our code to use less go blocks and <! / >! if possible. I have a section of code that I'm currently trying to see if I can re-write to use transducers, but am not quite seeing how to do it:

;; get-recipes returns hash-map of ids to values
;; desired return is a (channel containing) an ordered sequence of these values based off the input order of `ids`
(go (let [fetched-recipes (<! (get-recipes ids opts))]
         (mapv fetched-recipes ids)))

tanzoniteblack17:10:51

anyone have any suggestions?

yury.solovyov17:10:51

ids is a function, right?

yury.solovyov18:10:33

anyway, you may want to pass transducing function as argument, and apply it to the channel on the get-recipes

tanzoniteblack18:10:19

ids is not a function; it's simply a vector of strings

mccraigmccraig18:10:43

is get-recipes returning a channel of results, or a promise-chan with a single result ?

tanzoniteblack18:10:15

a channel with a single result (which looks like {"id1" val1 "id2" val2})

tbaldridge19:10:27

you can do this with transducers. In general a go that takes from another channel and returns a value can be replaced with a map @tanzoniteblack

tbaldridge19:10:17

`(chan 10 (map (fn [fetched-recipes] (mapv fetched-recipes ids)))`

tbaldridge19:10:26

And then have get-recipes put values on that channel

tbaldridge19:10:55

But at this point I start to wonder why it needs to return a vector of recipies instead of a channel of recipes

tanzoniteblack20:10:33

Having it return a channel of recipes would be cleaner, I was just trying to minimize the amount of refactoring for my first pass

tanzoniteblack20:10:53

Thanks a lot though, that is...really obvious now that I look at it 🙂

tbaldridge20:10:10

it takes practice 🙂