These are chat archives for ramda/ramda

11th
Aug 2017
Matthew Willhite
@miwillhite
Aug 11 2017 02:00
@Yord how about some usage examples or a type signature? I understood you wanted something like R.props, but the ability to pass in multiple paths instead of just (flat) props
Angel J Piscola
@Redmega
Aug 11 2017 05:29
Ramda gurus, I have this function. I believe it can be better and pure ramda, but I've been awake for about 18 hours. Grace me with your wisdom:
export const resolveMenu = (name, menus) => {
  const def = { items: [] };
  if (R.isNil(menus) || R.isEmpty(menus)) {
    return def;
  }
  const nameRegex = new RegExp(name, 'i');
  const menu = R.find(R.propSatisfies(R.test(nameRegex), 'name'), menus);
  return menu;
};
Philipp Wille
@Yord
Aug 11 2017 05:41
@miwillhite Sry, should have been more specific to start with: paths should be to path as props is to prop:
const paths = useWith(lift(path), [identity, of])
const obj = { a: { c: 1 }, b: { d: 2 } }

paths([['a', 'c'], ['b', 'd']], obj) // [1, 2]
Matthew Willhite
@miwillhite
Aug 11 2017 06:23

@Redmega Usually when I see nil checks I start thinking Maybe… I came up with this, using some Sanctuary:

// resolveMenu' :: String -> [Menu] -> Menu
const resolveMenu_ = (name, menus) =>
  S.pipe([
    S.find(compose(equals(toLower(name)), toLower, prop('name'))),
    S.fromMaybe({ items: [] }),
  ])(menus);

It is a bit different…
1) I chose to lowercase the name comparisons instead of using a case-insenstive Regex
2) Instead of returning undefined if no matches are found, the default { items: [] } is returned

Sanctuary is included in the Ramda repl, so you can run this and try it out
of course you could make it point free by dropping the menus from the pipe and second fn argument
I don’t know exactly what your input is, just guessing it is something like: [{ name: 'foo', items: [1,2] }]
Jonah
@jonahx
Aug 11 2017 06:51
@miwillhite :thumbsup:
Jawad Ahmad
@jawadahmadd
Aug 11 2017 10:46
Hey everyone! I am new to functional programming and new to ramda.
James
@aretecode
Aug 11 2017 10:47
@jawadahmadd welcome! 2 pieces of advice
  • name things well
  • don't go over the edge / slipper slope it's a careful balance :-P
Jawad Ahmad
@jawadahmadd
Aug 11 2017 10:48
haha I will James. Thanks!
James
@aretecode
Aug 11 2017 10:49
:-D awesome
plexigras
@plexigras
Aug 11 2017 11:34
how do i best chain promises and callbacks?
Matthew Willhite
@miwillhite
Aug 11 2017 11:48
plexigras
@plexigras
Aug 11 2017 12:06
oh nice
plexigras
@plexigras
Aug 11 2017 12:39
ramda is amazing
Mateusz Burzyński
@Andarist
Aug 11 2017 13:16
does anyone have an idea for a neat composition for such a problem:
split array into 2 by iterating from the right and determining a split point by a predicate over temporary accumulator
plexigras
@plexigras
Aug 11 2017 13:17
im not sure but maybe use reduce
Mateusz Burzyński
@Andarist
Aug 11 2017 13:18
use case:
array of some objects being {id, count} pair and i need to split it into 2 from the right so that i have a leftover and 'toBeFetched' parts and I want to stop iterating when I gather in an accumulator some N which is a sum of counts
cannot bail on reduce while iterating
plexigras
@plexigras
Aug 11 2017 13:21
maybe make an example
Mateusz Burzyński
@Andarist
Aug 11 2017 13:22
im on it! :P
let acc = 0
const target = takeLastWhile(({ count }) => {
  if (acc >= 50) {
    return false
  }
  acc += count
  return true
}, items)

const leftover = take(items.length - target.length, items)

return [ leftover, target ]
ofc wrapped in a function
splitLastWhen with acc would be cool
Mateusz Burzyński
@Andarist
Aug 11 2017 13:30
also I had no idea about reduceWhile, but same thing as with split~, would need a sibling function reduceRightWhile
i feel there is some neat abstraction over what i want, but cannot find it :<
plexigras
@plexigras
Aug 11 2017 13:30
take a look at reduce
Mateusz Burzyński
@Andarist
Aug 11 2017 13:31
well, everything can be solved with reduce :trollface:
would love to find more descriptive solution though
i guess ill adjust create my own splitRightWhenAccum
Mateusz Burzyński
@Andarist
Aug 11 2017 14:30
if anyone is interested
const _splitRightWhenAccum = <T, A>(
    fn: (T, A) => [boolean, A],
    acc: A,
    arr: T[],
): [T[], T[]] => {
    let result = false

    for (let index = arr.length; index > 0; index--) {

        // eslint-disable-next-line no-param-reassign
        [ result, acc ] = fn(arr[index - 1], acc)

        if (result) {
            return splitAt(index - 1, arr)
        }
    }

    return [[], arr]
}
export const splitRightWhenAccum = curry(_splitRightWhenAccum)
working fine so far
plexigras
@plexigras
Aug 11 2017 14:40
R.reduce((acc, val) => {
  if (acc[1] <= 50) acc[1] += val
  else acc[0].push(val)
  return acc
},[[],0])
Denis Stoyanov
@xgrommx
Aug 11 2017 15:07
reduceWhile?
Angel J Piscola
@Redmega
Aug 11 2017 15:40
@miwillhite Appreciated! However the menu name can be something like 'Foo Menu', and I can't control that from the data side, which is why I chose regex. Sanctuary looks interesting though. Maybe is from Fantasy Land right? I have literally 0 understanding of that so I'll try to read up!
plexigras
@plexigras
Aug 11 2017 15:41
i see there is a rollup fix but is there one for webpack
Joey Figaro
@joeyfigaro
Aug 11 2017 16:23
Hey guys. This pipe I have here: http://joeysharesthings.com/0C2V373X3X0f is including empty arrays as a result of R.map(R.dropWhile(R.equals(customer_id))) - how might I include a step to refrain from including/remove empty arrays from the end result?
Joey Figaro
@joeyfigaro
Aug 11 2017 16:39
^^
let id_collection = R.pipe(
  R.filter(R.propEq('description', customer.description)),
  R.map(R.pick(['id'])),
  R.map(R.values),
  R.map(R.dropWhile(R.equals(customer_id))),
  R.flatten
)(customers);
plexigras
@plexigras
Aug 11 2017 17:04
is improving treeshaking capabilitys planed?
Denis Stoyanov
@xgrommx
Aug 11 2017 17:53
3 map -> 1 map and composition of 3 functions ( 2 law of functor)
Bijoy Thomas
@bijoythomas
Aug 11 2017 18:03
:+1:
Matthew Willhite
@miwillhite
Aug 11 2017 18:06
@joeyfigaro Without trying this (an example data set would be helpful) I’d say try using R.chain on the last one if you are expecting a flattened array
Philipp Wille
@Yord
Aug 11 2017 19:34

Hey guys, just finished a blog post introducing Ramda by example for my team at work. Thought I would post it here, too:

http://canyou.philme.in/#/on/ramda?

Any feedback and critique is very welcome! :)

Angel J Piscola
@Redmega
Aug 11 2017 20:04
Can I critique the URL itself? I love it but the # gave me an aneurism :joy:
Joey Figaro
@joeyfigaro
Aug 11 2017 20:17
@miwillhite Thanks for jumping in, Matthew. I started editing a dump of some data to make it shareable and realized it was going to take a long time. Going to keep hacking at it.
Currently, I’m trying to figure out how best deal with using Maybe results inside of a pipe
Just { value: [ '8-11-2017', { ids: [Object], source: 'card' } ] }
Just { value: [ '8-11-2017', { ids: [Object], source: 'card' } ] }
Just { value: [ '8-11-2017', { ids: [], source: 'card' } ] }
Just { value: [ '8-11-2017', { ids: [], source: 'card' } ] }
Just { value: [ '8-11-2017', { ids: [Object], source: 'ach' } ] }
Just { value: [ '8-11-2017', { ids: [Object], source: 'ach' } ] }
Just { value: [ '8-11-2017', { ids: [Object], source: 'ach' } ] }
Just { value: [ '8-11-2017', { ids: [Object], source: 'ach' } ] }
Nothing {}
id_collection = R.pipe(
            R.map(R.values),
            R.prop('metadata'),
            R.tap(console.log)
        )(updates);
When metadata returns a Just, I’m not real sure how to get at that value prop. prop(‘value’) doesn’t work.
Anyone have any insights on working with Just/Nothing instances inside of a pipe?
Sebastien Daniel
@SebastienDaniel
Aug 11 2017 20:27

Hi, a little new to Ramda, and am having a hard time mapping this logic:
data structure:

{
   sessions: {
    currentSessionId: 1 
  },
  inputs: [ {sessionId: 1 }, {sessionId: 2} ]
}

What I need to do is return a list of inputs that have sessionId === sessions.currentSessionId.
This is trivial, of course, but I'm trying to have an elegant, ramda solution, so to speak.

Could anyone point me in the right direction?

my current implementation:

r.compose(
    r.filter(
        r.compose(
            r.propEq('sessionId'),
            getCurrentSessionId
        ),
    ),
    getInputs
);

of course, getInputs returns a part of the original argument, so getCurrentSessionId fails.

is there a way to send the original argument down ?
Philipp Wille
@Yord
Aug 11 2017 20:32

Can I critique the URL itself? I love it but the # gave me an aneurism :joy:

yeah I know ^^ I host it on github pages which make SPAs challenging ;) (although not impossible)

Joey Figaro
@joeyfigaro
Aug 11 2017 20:49
[{ ids: ['cus_xxx', 'cus_xxx', 'cus_xxx'], source: 'card' }]
If I have the following available inside of map, how can I access that ids prop?
Have tried a nested map.
Kurt Milam
@kurtmilam
Aug 11 2017 20:57
@joeyfigaro pluck might be useful. Will that array sometimes have multiple objects, and are you also interested in source?
The nested map should also work.
Joey Figaro
@joeyfigaro
Aug 11 2017 20:58
Not interested in source, and it won’t ever have multiple objects, no.
I was trying to avoid the nested map because it’s starting to get a little hairy. :)
Playing around with pluck now
@kurtmilam that’s super close - http://joeysharesthings.com/0V3K2d463937
Joey Figaro
@joeyfigaro
Aug 11 2017 21:03
I’m getting back the values I’m interested in (despite being wrapped in superfluous arrays still,) but that index 0 has undefined
For reference, this was the shape we had available prior to using pluck: http://joeysharesthings.com/2q1f1l0a2H0Y
Kurt Milam
@kurtmilam
Aug 11 2017 21:04
path is another option. path([0, 'ids']) should do the trick.
Joey Figaro
@joeyfigaro
Aug 11 2017 21:05
Changing that 0 to 1 gets me back my Just :)
Thanks bud. Really appreciate your help!
Kurt Milam
@kurtmilam
Aug 11 2017 21:06
All I see at the links is a png
Are you using Sanctuary or what?
Joey Figaro
@joeyfigaro
Aug 11 2017 21:06
using ramda-fantasy for maybes
Hadn’t heard of Sanctuary, though
Kurt Milam
@kurtmilam
Aug 11 2017 21:06
I think that's the way to go for maybes. As I understand it, ramda-fantasy isn't really being regularly maintained or updated.
The author of sanctuary-js is a ramda contributor. Check out the sanctuary gitter, as well.
Joey Figaro
@joeyfigaro
Aug 11 2017 21:08
:100: Excellent. Checking it out - thanks again.
Kurt Milam
@kurtmilam
Aug 11 2017 21:08
:thumbsup:
@SebastienDaniel converge might be helpful. Also, you may be interested in o rather than compose when you're only composing two functions:
r.o(
    r.filter(
        r.converge(
          r.o,
            [ r.propEq('sessionId'),
              getCurrentSessionId
            ]  
        ),
    ),
    getInputs
);
Sebastien Daniel
@SebastienDaniel
Aug 11 2017 21:10
thx @kurtmilam
I'll look into those!
Kurt Milam
@kurtmilam
Aug 11 2017 21:11
Sorry, I posted the message prematurely. I haven't tested the edited code, but something along those lines might work for you.
No, that's not going to do the trick :D I should have thought about it a little longer before I posted.
Sebastien Daniel
@SebastienDaniel
Aug 11 2017 21:12
yeah, I'll work on it :)
Kurt Milam
@kurtmilam
Aug 11 2017 21:13
Might add some dummy data to the REPL so I or others can work with it.
Sebastien Daniel
@SebastienDaniel
Aug 11 2017 21:18
works :)
r.converge(
    r.filter(),
    [
        r.compose(
            r.propEq('sessionId'),
            getCurrentSessionId
        ),
        getInputs
    ]
);
that converge method is mighty interesting
Kurt Milam
@kurtmilam
Aug 11 2017 21:19
:thumbsup:
it is, indeed
Sebastien Daniel
@SebastienDaniel
Aug 11 2017 21:19
thx again!
Kurt Milam
@kurtmilam
Aug 11 2017 21:20
No worries - here it is on the REPL.
I'd also change filter() to filter.
Kurt Milam
@kurtmilam
Aug 11 2017 21:29
For ramda-fantasy, getOrElse might be what you're looking for to extract a value from a Just, @joeyfigaro