These are chat archives for ramda/ramda

26th
Dec 2016
Emilio Srougo
@Emilios1995
Dec 26 2016 03:35
Hi everyone. I'm trying to solve this problem. I want to go from this:
[1,5,7,2,4,8,1,6,9]
to
[[1,5,7], [2,4,8], [1,6,9]]
Can anyone think of a nice way to accomplish that?
Emilio Srougo
@Emilios1995
Dec 26 2016 04:56
Basically, each time the numbers in the array stops increasing, I want to partition the array.
Kevin Wallace
@kedashoe
Dec 26 2016 05:36
@Emilios1995 there is a PR to alter groupWith that I think could help you: ramda/ramda#2016
Denis Stoyanov
@xgrommx
Dec 26 2016 13:00
@Emilios1995 do you need parts of your array?
Brenton Alker
@tekerson
Dec 26 2016 16:34
Hey y'all, I have a function fetch :: String -> Promise Item where Items are { name: String, children: Array String }, can anyone point me to a good way to recursively fetch the items named in "children" and collect the Items, starting from an Array String? (this is a dependency graph, but I can only fetch the Items asynchornously). I feel like R.unfold is close, but doesn't seem to play nicely with the Promises (or, at least, I can't see it).
Jason Lengstorf
@jlengstorf
Dec 26 2016 17:37

Hey, folks! I'm working on a small module that finds all elements in a given container, and I'm wondering if there's a way to simplify this last step:

export default function initialize({
  limitToScope = 'body',
  elementsToTarget = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
  limitToDirectDescendants = false,
} = {}) {
  const templateFunc = getSelectorTemplate(limitToDirectDescendants, limitToScope);
  const selector = getSelectorList(templateFunc, elementsToTarget);
  const elements = getElements(selector);

  // Do stuff with the elements...
}

Is there a way to simplify this? Something more like this:

const selector = getSelectorList(limitToDirectDescendants, limitToScope, elementsToTarget);

I had looked at R.useWith, but I couldn't figure out how to make that work the way I wanted.

This is more or less what I'm working on: https://goo.gl/GCs34v
James Forbes
@JAForbes
Dec 26 2016 22:52
@tekerson What's the format of the strings in the Array String?
Brenton Alker
@tekerson
Dec 26 2016 22:53
It's a flat array of strings, matching the "name" field of other Items
James Forbes
@JAForbes
Dec 26 2016 22:53
Does the name field correspond to an endpoint?
Brenton Alker
@tekerson
Dec 26 2016 22:55
endpoint as in API endpoint, no it's all from the same endpoint - but that's kind of irrelevant. I'll paste a more complete example.
James Forbes
@JAForbes
Dec 26 2016 22:55
And do you then want that data to replace the structure of the child? (e.g. does Array String become Array Item)
Brenton Alker
@tekerson
Dec 26 2016 22:56
no, the childrenin the returned Array can stay as the original strings
James Forbes
@JAForbes
Dec 26 2016 22:58
So when you say recursively fetch, do you mean you just want to fire off a request for each string, not return a new structure?
Brenton Alker
@tekerson
Dec 26 2016 22:59
yeh, so the eventual result will be a flat list of all of the items in the dependency tree
James Forbes
@JAForbes
Dec 26 2016 23:01
@jlengstorf if you could swap the arg order of getSelectorList it might make composition more straightforward:
const f = compose(
  ,getElements
  ,getSelectorLIst(elementsToTarget) //swapped order
  ,getSelectorTemplate
)
f(limitToDirectDescendants, limitToScope)
Jason Lengstorf
@jlengstorf
Dec 26 2016 23:04
@JAForbes That makes a lot of sense. I was locked in for a certain order and didn't consider that. Thanks so much!
James Forbes
@JAForbes
Dec 26 2016 23:04
@jlengstorf Glad to help
Brenton Alker
@tekerson
Dec 26 2016 23:05
@JAForbes Here's the complete example - https://goo.gl/Yvy1Bm
so, I'm trying to rewrite option1/the usage to avoid to the manual recursion
I don't really mind whether it "batches" the fetches (caused by the Promise.all) or iterates as soon as possible (ASAP is actually preferable, but batches is fine)
James Forbes
@JAForbes
Dec 26 2016 23:12
I see, I was picturing you had a tree upfront and you wanted to traverse that, but you're building the tree as you fetch which makes it a lot more interesting.
Brenton Alker
@tekerson
Dec 26 2016 23:12
yeh, exactly
that's the bit that's causing issues :)
James Forbes
@JAForbes
Dec 26 2016 23:13
:D
In your real code is this for fetching dependencies/scripts for given components or something?
Brenton Alker
@tekerson
Dec 26 2016 23:20
yeh, it's part of a build system
James Forbes
@JAForbes
Dec 26 2016 23:21
Perhaps a silly question, but could the recursive part be happening on the server where you have all the metadata, then you can remove the async aspect for building the metadata tree which might make things simpler. Or is the server off limits?
Brenton Alker
@tekerson
Dec 26 2016 23:22
That's eventually the plan, but it needs to be maintain BC with the existing API
so, yep, server api is fixed for this purpose
Brenton Alker
@tekerson
Dec 26 2016 23:29
I've also implemented it by writing an unfoldAsync function that essentially does the same as unfold, but expects the iterator function to return a promise and resolves it before iterating again. But I was hoping it was possible to use library functions, otherwise the manual recursive version is probably going to win :(
James Forbes
@JAForbes
Dec 26 2016 23:45
Yeah, the fact the tree is generated as you go makes me think your approach is the best you're gonna get. If you had the tree of strings upfront you could traverse it from a Tree Future Item into a Future Tree Item and then flatten that tree into your Array Item. That would be pretty nice, but its kind of awkward if the tree isn't being generated as you traverse it.
Brenton Alker
@tekerson
Dec 26 2016 23:49
Thats the conclusion I've come to also :thumbsup: Thanks for the confirmation. It's still much better than the previous version :laughing:
James Forbes
@JAForbes
Dec 26 2016 23:56
Yeah no problem :) I think if you eventually could do all of this server side, or have the server send you the metadata up front so you can build the tree upfront it could all become a lot nicer, but given your constraints what you have is great!