These are chat archives for ramda/ramda

5th
Apr 2019
Filip Białek
@fbialek
Apr 05 09:26
Hi! is there a way to not execute map two times and make that snippet more flat?
map(func1, map(func2, myArray));
Rakesh Pai
@rakeshpai
Apr 05 09:29
@fbialek
const fn = map(compose(func1, func2));
fn(myArray);
Filip Białek
@fbialek
Apr 05 09:30
great! thanks a lot!
Mårten Pettersson
@mtnptrsn
Apr 05 13:25

Hiya guys!

I'm new to Ramda (so sorry for newbie question). I'm currently having some issues with parameters when nesting forEach's. This is my code:

const sendTeamToAllPlayers: (teams: string[][]) => void = forEach(
  forEach(
    pipe(
      getUserIdFromUserInputReference,
      user =>
        sendMessageToUser({
          user,
          message:
            'Here is your team: ' /* I need access to the outer parameter "teams" here */,
        }),
    ),
  ),
)

I've done some unnecessary typing here just to make it easier to understand what comes in. My issue here is: I don't know how to access the teams variable, aka the variable the gets passed in to sendTeamToAllPlayers when inside the second forEach in a functional way. I find myself creating impure functions in this case, just using the variable that gets passed in to the outer function.

Rakesh Pai
@rakeshpai
Apr 05 13:39

Hmm. Making this point-free might not be worth the readability tradeoff. That said, I think the starting point would be to consider maybe wrapping the sendMessageToUser to make it curried:

const wrapped = team => user => sendMessageToUser(...);

That would let you split up the invocations into two: wrapped(team) returns a function which later accepts a user. You could create this wrapped(team) in the outer loop's iteration, and pass the function into the inner loop. With that done, you could then look into creating the function itself in a point-free way.

A tip I'd have is to isolate the side-effects, i.e., if sendMessageToUser is side-effect-y, it's probably a good idea to first create the list of messages you want to send, and then call sendMessage on each item in the list. That way, creating the list of messages is pure, and only actually sending it is impure.
Mårten Pettersson
@mtnptrsn
Apr 05 13:44
@rakeshpai Thank you so much. I'll try it out!
@rakeshpai Great tips :)
Rakesh Pai
@rakeshpai
Apr 05 13:55
Here's my take: Not particularly point-free, but I think any more than this might make it harder to read.
const createMessageForUser = team => user => ({
  user,
  message: 'Here is your team: ' /* I need access to the outer parameter "teams" here */,
});

const createAllMessages = team =>
  team.map(createMessageForUser(team));

const sendMessages = pipe(
  createAllMessages,
  forEach(sendMessageToUser)
);

// usage
sendMessages([
  ['foo', 'bar'],
  ['baz']
]);
Mårten Pettersson
@mtnptrsn
Apr 05 14:10
Thank you. Very helpful. It's surprisingly tricky to compose like this without creating impure function like I'm used to.
Rakesh Pai
@rakeshpai
Apr 05 14:18
:) It takes a little getting used to. It's also always worth questioning whether it's improving your code. :)