These are chat archives for ramda/ramda

17th
Oct 2017
arian‮
@arian-swydo
Oct 17 2017 08:36
@kurtmilam I got the same error just now
or maybe it's a different problem?
Kurt Milam
@kurtmilam
Oct 17 2017 08:39
@arian-swydo the PR hasn't been merged yet: ramda/ramda#2344
my bad
that's the correct bin
hmm, I'm guessing maybe I should do a converge as workaround?
Kurt Milam
@kurtmilam
Oct 17 2017 08:40
Yeah, that PR should fix it.
arian‮
@arian-swydo
Oct 17 2017 08:41
converge( identity, [ nthArg(0), onUpdate ] )
something like that maybe?
Kurt Milam
@kurtmilam
Oct 17 2017 08:42
Or you could whip up your own tap until the fix hits:
const tap = fn => x => {
  fn( x )
  return x
}
arian‮
@arian-swydo
Oct 17 2017 08:43
much better, thanks
Kurt Milam
@kurtmilam
Oct 17 2017 08:43
:+1:
arian‮
@arian-swydo
Oct 17 2017 11:27
flip keeps bugging out on me :/
have to replace it with a=>b=>c=>a(c)(b);
Kurt Milam
@kurtmilam
Oct 17 2017 11:40
example?
arian‮
@arian-swydo
Oct 17 2017 11:54
@kurtmilam
not sure if that is the right link
but you can see in the withStateHandlers
there is the function
which not point free looks like:
( state, props ) => value => set( lens )( value )( state )
which i tried to do with flip( set(lens) )
but I'm guessing it's because it has an arity of 2?
the commented out flip works
i updated it
with the better comment
Kurt Milam
@kurtmilam
Oct 17 2017 12:01
waiting for the bin to load
arian‮
@arian-swydo
Oct 17 2017 12:01
oh haha, yeah it's terrible
(has npm though)
it tries to do an .apply on the flipped function, but says it is not a function
wait, actually, It's my mistake
I should wrap it with an arity 1
of some kind
D: sorry
Kurt Milam
@kurtmilam
Oct 17 2017 12:04
Your _flip is different than Ramda's.
arian‮
@arian-swydo
Oct 17 2017 12:04
yep, sorry that's why I keep assuming wrong
should have been nAry(1, flip(set(lensProp('innerText'))))
Kurt Milam
@kurtmilam
Oct 17 2017 12:05
:+1:
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 15:59
Does anyone here know how I can simplify something like this using some chain/composition/recursion functions?
    return R.map(
    section => [
      R.prop('id', section),
      R.map(
        element => [
          R.prop('id', element),
          R.map(
            item => [
              R.prop('id', item),
              R.path(['log_element_items', 'results'], item)
            ],
            R.path(['log_elements', 'results'], element)
          ),
        ],
        R.path(['log_sections', 'results'], section)
      )
    ]
  )(logs)
Fred Daoud
@foxdonut
Oct 17 2017 17:08
@AdrianSkierniewski small change could be
  item => R.ap([
    R.prop('id'),
    R.path(['log_element_items', 'results'])
  ], [item])
@AdrianSkierniewski but your code makes the intents clear, not sure if changes would make things actually simpler..
Adrian Perez
@blackxored
Oct 17 2017 17:40
how do I apply the same arguments to two functions with only passing them once?
don't want to use compose+tap
which would be naive I think
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 17:40
@foxdonut thanks
Fred Daoud
@foxdonut
Oct 17 2017 17:43
@blackxored R.ap perhaps? see above
Adrian Perez
@blackxored
Oct 17 2017 17:45
hmm true, takes array params though
I think i'll live ;)
or unapply it maybe at some point
thanks
Fred Daoud
@foxdonut
Oct 17 2017 17:46
welcome
Brad Compton (he/him)
@Bradcomp
Oct 17 2017 18:02
Try
R.converge
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:19
and how about this kind of transformation is there any ramda function that I can compose instead of iterating through that array ?
[12, [[42], [43], [44]]] ==> [[12, 42], [12, 43], [12, 44]]
Kurt Milam
@kurtmilam
Oct 17 2017 18:29
@AdrianSkierniewski this will flatten your tree like you want.
const flattenTree =
  xs =>
    chain( map( prepend( head( xs ) ) ) )
         ( tail( xs ) )

flattenTree( [12, [[42], [43], [44]]] )
// -> [[12, 42], [12, 43], [12, 44]]
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:32
that was exactly what I was trying to figure out
Kurt Milam
@kurtmilam
Oct 17 2017 18:32
:+1:
Adrian Perez
@blackxored
Oct 17 2017 18:33
is it just me or that looked like clojure :joy:
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:34
will this work if I'd get more nested arrays ?
for example
[12, [[42, [[101]]], [43, [[102]]], [44, [[103]]]]]
Scott Sauyet
@CrossEye
Oct 17 2017 18:35
@AdrianSkierniewski: or ([fst, snd]) => map(val => flatten([fst, val]), snd)
What is the expected result of that last?
My suggestion would return [[12, 42, 101], [12, 43, 102], [12, 44, 103]]
Kurt Milam
@kurtmilam
Oct 17 2017 18:38
:+1: Nice! Mine needs some extra work to flatten the third level.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:39
yes you're right @CrossEye
Scott Sauyet
@CrossEye
Oct 17 2017 18:39
But that might only work by coincidence.
What do you want for [12, [[42, [[101]]], [43, [[102], [103]]], [44, [[104]]]]]?
Mine returns [[12, 42, 101], [12, 43, 102, 103], [12, 44, 104]]
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:40
here is my whole example with all levels
[[12, [[42, [[101, [3847]], [102, [3848]]]]]]]
Scott Sauyet
@CrossEye
Oct 17 2017 18:40
But I wonder if you want this instead?: [[12, 42, 101], [12, 43, 102], [12, 43, 103], [12, 44, 104]]
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:40
it's a tree structure and I'd like to build path for each leaf from root to that leaf
so [[12, 42, 101, 3847], [12, 42, 102, 3848]]
if I'm readying those arrays correctly :)
basically I always have array with first element equal id of parent and second element equals array of child elements
Scott Sauyet
@CrossEye
Oct 17 2017 18:46
That last example has one more level of [] wrapping the whole thing than the earlier ones. Is this intended?
Lewis
@6ewis
Oct 17 2017 18:46
Hi guys
anyone use typescript?
Scott Sauyet
@CrossEye
Oct 17 2017 18:46
I'm sure someone does. Not me, though.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:48
yes your result is correct
[[12, 42, 101], [12, 43, 102], [12, 43, 103], [12, 44, 104]]
it's expected result
but it won't work if I have more that one element in that 3rd level array
for example
this works [12, [[42, [[101]]]]] but this doesn't [12, [[42, [[101], [102]]]]]
I'd like to use that function on map so it would generate all paths from root to leaf
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:53
so it works for 2 levels [12, [[42], [43]]] => [12, [[42], [43]]] but it doesn't for 3 levels [12, [[42, [[101], [102]]], [43, [103], [104]]]] => [12, [[42, [[101], [102]]], [43, [103], [104]]]]
[[12, 42, 101], [12, 42, 102], [12, 43, 103], [12, 43, 104]]
here is list of all paths from root to leaf from that array
Scott Sauyet
@CrossEye
Oct 17 2017 18:55
Right, now that we know a little more about what you want, it's clear you need a recursive function
Lewis
@6ewis
Oct 17 2017 18:55
@CrossEye I am debating whether it will play well with functional paradigm
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:56
I was looking for some hint that I could use to build that function
your solution with that destructuring looks very clean
Brad Compton (he/him)
@Bradcomp
Oct 17 2017 18:58
Ideally you're going to use recursion, probably with reduce
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 18:59
I wouldn't come up with that idea
@Bradcomp thank you for another idea how to solve that problem, I'm still trying to change my way of thinking to be more functional and this isn't easy :)
Brad Compton (he/him)
@Bradcomp
Oct 17 2017 19:01
My head is fuzzy right now, I might try to come up with something here though...
Kurt Milam
@kurtmilam
Oct 17 2017 19:03
This does a little more than what you're asking. I'm trying to figure out what I need to remove to make it work for your case:
const allPaths =
  d => x => t =>
    nTest( d[ t[ 0 ] ].Type, x )
      ? !R.is( Array, t[ 1 ] )
        || isEmpty( t[ 1 ] )
          ? [ [ t[ 0 ] ] ]
          : map( prepend( t[ 0 ] ) )
               ( chain( allPaths( d[ t[ 0 ] ] )
                                ( x )
                      )
                      ( t[ 1 ] )
               )
     : []
But it also reduces a rose tree that's in exactly the same format as yours to a list of paths from root to leaf node. It just includes some additional filtering logic that I need to extract.
In the meantime, here's an example implementation from your earlier question. I'll try to work up a recursive version of that solution shortly.
Kurt Milam
@kurtmilam
Oct 17 2017 19:46
@AdrianSkierniewski I think this recursive solution will work for your earlier question. I shortened the property names and am obviously not testing it against real data :)
const logs =
  [{id: 1, s:{r:[{id: 2, e:{r:[{id: 3, i:{r:['item']}}]}}]}}]

const doLogs =
  ([hd, ...tl]) =>
    map(juxt([prop('id')
             ,tl.length > 0
                ? x => doLogs(tl)(path(hd, x)) 
                : path(hd)
             ]))
doLogs([['s', 'r'], ['e', 'r'], ['i', 'r']])
      (logs)
// -> [[1, [[2, [[3, ["item"]]]]]]]
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 19:49
wow, thank you
Kurt Milam
@kurtmilam
Oct 17 2017 19:50
Note: I made some edits just now. Would love to know if it does the trick against real data. You need to pass it a list of paths to work on, then the log data.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 19:57
@kurtmilam
[ [ 12,
    [ [ 42,
        [ [ 101,
            [ { description: null,
                id: 3847,
                label: 'Safe count changeover is complete and signed off',
                log_element_id: 101,
                position: 0,
                priority: 0 } ] ],
          [ 102,
            [ { description: null,
                id: 3848,
                label: 'Safe count changeover is complete and signed off #2',
                log_element_id: 101,
                position: 0,
                priority: 0 } ] ] ] ] ] ] ]
last level is special because it doesn't contain next level, that's why I just used R.path without map
Kurt Milam
@kurtmilam
Oct 17 2017 19:58
Is that what you wanted it to look like? Or did you want to extract the id from the inner object?
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 19:58
my code returns this [[12, [[42, [[101, [3847]], [102, [3848]]]]]]]
so the last level is converted to just [R.prop('id')]
Kurt Milam
@kurtmilam
Oct 17 2017 19:59
Great, I'll handle the inner case and post the solution
I think this will do the trick.
Kurt Milam
@kurtmilam
Oct 17 2017 20:04
There's probably a slightly nicer way to do that, since there's already a prop('id') higher up in that function, but that's a start if it works.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:06
it works :)
Kurt Milam
@kurtmilam
Oct 17 2017 20:06
:+1:
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:07
these numbers in that array are foreign keys
I'd need to use them during load tests
to send post request I'd need to have array that looks like this [[12, 42, 101, 3847], ...]
Kurt Milam
@kurtmilam
Oct 17 2017 20:10
I think this recursive solution will work for your second question:
const allPaths =
  ([fst, snd]) =>
    !R.is( Array, snd )
        || isEmpty( snd )
          ? [ [ fst ] ]
          : map( prepend( fst ) )
               ( chain( allPaths )
                      ( snd )
               )

allPaths([12, [[42, [[101]]], [43, [[102]]], [44, [[103]]]]])
//-> [[12, 42, 101], [12, 43, 102], [12, 44, 103]]
@AdrianSkierniewski here are both recursive solutions used together in a pipe.
It's probable that these could be better integrated to improve performance. I may take a look at it for fun :)
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:16
first function works for original data it returns [ [ [ 12, [ [ 42, [ [ 101, [ 3847 ] ], [ 102, [ 3848 ] ] ] ] ] ] ] ]
but second function doesn't work probably because last array is an array with numbers instead arrays
theoretically it could be [[3847]]
to unify the code
Kurt Milam
@kurtmilam
Oct 17 2017 20:20
righto, I'll figure that out :)
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:23
maybe returning that leaf value wrapped with array is the easiest solution
I'm trying to fix that right now
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:29
hmm, how I can wrap that R.prop in array in this code R.map(R.prop('id'))(R.path(hd, x)) ?
I could create custom function that will return x => [x.id]
and use it instead prop
or is it something similar already in ramda?
Kurt Milam
@kurtmilam
Oct 17 2017 20:31
I tried that, but it didn't do the trick. pipe( prop('id'), of ).
The return from the first function is too deeply nested, so we should be peeling off array levels rather than adding them.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:32
yes I noticed that
ok
probably those two function should be wrote as one, right? Instead returning those arrays I could try to return those paths
I thought that splitting this process in to two steps will be easier to understand for me
Kurt Milam
@kurtmilam
Oct 17 2017 20:37
It's possible that it would have been good to start out trying to do it in one step, then split where it makes sense if you find a good place. It's not always easy to know, up front, where to split a complex operation.
My brain is a little fried, so I'm going to take a short break and come back to it later :D
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:43
I could send you some example data if you want to take a look at it
I'll get back to it tomorrow morning
Kurt Milam
@kurtmilam
Oct 17 2017 20:50
:+1: I think I get the data, I'm just running out of gas tonight.
Adrian Skierniewski
@AdrianSkierniewski
Oct 17 2017 20:54
have a good night :)
Kurt Milam
@kurtmilam
Oct 17 2017 21:20
this may be working, but my brain is too fried to tell. I'll look again tomorrow.