Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Jan 31 2019 22:17
    CrossEye commented #2779
  • Jan 31 2019 21:04
    ArturAralin commented #2779
  • Jan 31 2019 20:08
    CrossEye commented #2779
  • Jan 31 2019 18:56
    buzzdecafe commented #2631
  • Jan 31 2019 18:09
    ArturAralin commented #2779
  • Jan 31 2019 16:18
    CrossEye commented #2779
  • Jan 31 2019 16:10
    CrossEye commented #2631
  • Jan 31 2019 16:06
    CrossEye commented #2777
  • Jan 31 2019 14:44
    ArturAralin opened #2779
  • Jan 31 2019 07:39
    inferusvv commented #2631
  • Jan 31 2019 03:07
    sespinozj commented #2771
  • Jan 31 2019 02:33
    machad0 commented #2771
  • Jan 31 2019 02:26
    JeffreyChan commented #2777
  • Jan 30 2019 14:30
    CrossEye closed #2777
  • Jan 30 2019 12:13
    vanyadymousky updated the wiki
  • Jan 30 2019 01:42
    JeffreyChan commented #2777
  • Jan 29 2019 21:06
    vanyadymousky updated the wiki
  • Jan 29 2019 16:28
    CrossEye commented #2777
  • Jan 29 2019 15:50
    mbostock commented #2772
  • Jan 29 2019 15:48
    CrossEye commented #2772
Yosevu Kilonzo
@yosevu
Thanks @chughes87 !
Charles Hughes
@chughes87
Anyone have ideas why I'd be getting the following type error from adding a simple tap((x) => console.log(x)) after a filter statement in a pipeline?
[ts] No overload matches this call.
  The last overload gave the following error.
    Argument of type 'FilterOnceApplied<unknown>' is not assignable to parameter of type '(x: unknown) => unknown[]'.
      Type 'unknown[] | Dictionary<unknown>' is not assignable to type 'unknown[]'.
        Type 'Dictionary<unknown>' is missing the following properties from type 'unknown[]': length, pop, push, concat, and 26 more.
Alex Carpenter
@hybrid_alex_twitter
:wave: Very new to Ramda and curious how I would go about grouping objects in an array by month?
const data = [
  { title: "One", date: "2021-03-03" },
  { title: "Two", date: "2020-03-03" },
  { title: "Three", date: "2021-03-22" },
  { title: "Four", date: "2020-03-03" },
  { title: "Five", date: "2020-02-23" },
]
Marcelo Olivas
@mfolivas

@hybrid_alex_twitter , try the following:

const addMonth = request => {
  const date = new Date(request.date)
  const month = date.toLocaleString("default", { month: "long" })
  return {
    ...request,
    month
  }
}
const byMonth = R.groupBy(R.prop('month'))

const withMonth = data
  .map(addMonth)

byMonth(withMonth)

the output should be:

{
   "February":[
      {
         "date":"2020-02-23",
         "month":"February",
         "title":"Five"
      }
   ],
   "March":[
      {
         "date":"2021-03-03",
         "month":"March",
         "title":"One"
      },
      {
         "date":"2020-03-03",
         "month":"March",
         "title":"Two"
      },
      {
         "date":"2021-03-22",
         "month":"March",
         "title":"Three"
      },
      {
         "date":"2020-03-03",
         "month":"March",
         "title":"Four"
      }
   ]
}
Brad Compton (he/him)
@Bradcomp
You also don't need to add the month to the object. The logic for generating the month could just be returned in the function passed to groupBy if you don't want to modify the object
Marcelo Olivas
@mfolivas
👆 @Bradcomp to the rescue!!
Marcelo Olivas
@mfolivas
const getMonth = request => {
  const date = new Date(request.date)
  const month = date.toLocaleString("default", { month: "long" })
  return month
}
const byMonth = R.groupBy(getMonth)
byMonth(data)
Alex Carpenter
@hybrid_alex_twitter
@mfolivas Nice! Can this go one more level and group by year, then by month?
Brad Compton (he/him)
@Bradcomp
const getYear = (request) => {
// similar logic to the getMonth function, but returns the year
}
const getMonth = (request) => blah;

const byYearAndMonth = pipe(
  groupBy(getYear),
  map(groupBy(getMonth))
)
map will work on Objects, so you'll end up with something like:
{
  1999: {
    January: [...],
   ...
  },
  2000: {
    January: [...],
    ...
  },
  ...
}
Alex Carpenter
@hybrid_alex_twitter
@Bradcomp So awesome, thanks for sharing!
Timothy Alcaide
@timothyalcaide
Hello :) It wiil be cool if we add a keyboard shortcut to focus on the search input of the ramda doc... I Can do it but where can i find the code of the site doc on github ?
Timothy Alcaide
@timothyalcaide
I found in ramda docs source code, it's "/" to focus ^^
Charles Hughes
@chughes87
which makes sense
Coop
@Darkle
Hi, is it possible to get access to the index when using reduceWhile in both the predicate function and the iterator function?
Coop
@Darkle
Xi Shen
@davidshen84
Hi, is it possible to use the identity function to pass two arguments to the next function. Like
compose(func_2_args, identity([1,2]));
James Sinclair
@jrsinclair
Hi @davidshen84, you can do this in Ramda using the converge() function: https://ramdajs.com/docs/#converge, but it's a little hard to work out what's going on at first. You might like to read through https://jrsinclair.com/articles/2019/compose-js-functions-multiple-parameters/, which explains how this works. Essentially the converge() function generalises lift for functions to work with any number of parameters.
oomusou
@oomusou
any datetime libaray recommend for Ramda ? thanks
zackkrida
@zackkrida:matrix.org
[m]
Personally I just use standard things like date-fns and wrap all the things in R.curry()
Xi Shen
@davidshen84
I have an array of objects, like [{}...{v:1}..{}], and I'd like to modify some of them and return the others untouched, like '[{}...{v:2}...{}]`. Basically, it's like clone the original array then do a for-each loop. But I want to do it in a functional way. Does R provide some function to do this?
Marcelo Olivas
@mfolivas
@davidshen84 , as @zackkrida:matrix.org mentioned, I usually just leverage curry for those types of stuff. I also think that you can leverage pipe as well. For example:
const someFunction = (param1, param2, array) => implementation
const someOtherFunction = (array) => implementation
const someFunctionCurried = R.curry(someFunction)
const func = someFunctionCurried('foo', 'bar') //curried function with the two params without the array
const pipeFunction = R.pipe(someFunction, someOtherFunction)
return pipeFunction(data) //passing the array
wangzengdi
@adispring
@davidshen84 You can use R.adjust, R.update or R.map(R.when(...)), to modify specified item(s).
Xi Shen
@davidshen84
@mfolivas , @adispring & @zackkrida:matrix.org Thank you!
Martin Harvan
@martinhrvn
is there a clever way to 'chunk' an array, but not by count of items, but by sum of some property (e.g. ['foo', 'bar', 'baz'] and you want chunk so that sum of lengths is < 8 so in this case you get [['foo','bar'],['baz']]
2 replies
Antonin
@OverGlass
Hi :) How can I refacto this function to be more readable ? Maybe with monads but I did't find any thing :/
type $parse = (text: string) => string[][];
const parse: $parse = pipe(split("\n\n\n"), map(pipe(split("\n\n"), map(pipe(split("\n"), filter(isNotEmpty))))));
1 reply
Trey Davis
@treydavis
apologies for this not-directly-ramda-related post, but I'm looking for a FP-minded JS developer to work on my team. How can I target that type of JS developer?
1 reply
Yosevu Kilonzo
@yosevu
Hey! Should I use Ramda utilities to "spread" array items and "object" properties?
{
  ...state,
  // override props
}
Yosevu Kilonzo
@yosevu
Or maybe I don't need spread if I use lens to update state? Something like this:
pipe(
  updatePropWithLens,
  updateProp2WithLens,
)(state)
Rick Medina
@rickmed
Hi! doesn't ramda has a reduce fn on objects? eg, {} -> [{}], where keys are projected to a prop in all objects in the array
wangzengdi
@adispring
@rickmed Dose the following code satisfy your needs?
const toSingleObjs = R.compose(R.map(R.apply(R.objOf)), R.toPairs)

toSingleObjs({a: 1, b: 2, c: 3}); 
//=> [Object {a: 1}, Object {b: 2}, Object {c: 3}]
jiangyu3
@k644606347
,,l
josuamanuel@hotmail.com
@josuamanuelhot1_twitter

is unionWith function right?

The order of the params for the predicate function looks wrong to me. First parameter corresponds with the second object. and second parameter corresponds with the first object. This makes the first example to fail and the second to work.

R.unionWith((l, r) => l.date.substr(0,10) === r.date, [{date:'2020-01-02T00:00:00'}],  [{date:'2020-01-02'}]) 
//?  [{date:'2020-01-02T00:00:00'}, {date:'2020-01-02'}]

And this works:

R.unionWith((l, r) => l.date === r.date.substr(0,10), [{date:'2020-01-02T00:00:00'}],  [{date:'2020-01-02'}]) 
//? [{date:'2020-01-02T00:00:00'}]
Marcelo Olivas
@mfolivas

this is a more theoretical question since I'm a FP newbie. I want to understand functors and monads so I know how they work and understand how to use them in my application. Today, while reading Joy of Javascripts (I'm a static language OOD for most of my career) I noticed the following code:

const unique = letters => Array.from(new Set([...letters]))

class Id {
  #val
  constructor(value) {
    this.val = value
  }
  static of(value) {
    return new Id(value)
  }
  get() {
    return this.val
  }
}

Id.of('aabbcc').get()
'aabbcc'

//this is where I get lost
const Functor = {
  map(f = identity) {
    return this.constructor.of(f(this.get()))
  }
}

Object.assign(Id.prototype, Functor);

Id.of('aabbcc').map(unique).get()
[ 'a', 'b', 'c' ]

In here Id, is pretty straightforward. I also see (more or less) how they added the functionality of map by adding the Functor but I get lost on the

return this.constructor.of(f(this.get()))

it seems that you are accessing the container (in this case will be the instance of class (object) Id) and get the value passed. Later, it seems that to actually add the functionality by adding the the mixing Functor to the Id.

5 replies
Joshua Travis
@jktravis

I often compose a series of lenses to update various pieces of an object. Sometimes, I find that I need a value from another part of that object, that was just updated in this pipeline, in order to correctly derive another property.

Is there a utility that would help here? I usually just use an arrow function, and reference the values as needed. But am curious if there's a better way.

R.compose(
  st => R.over(R.lensProp("data"),
    R.map(
      R.set(
        R.lensProp("selected"),
        st.isSelectAll,
      ),
    ),
  ),
  R.over(R.lensProp("isSelectAll"), R.not),
)(state);
4 replies
Marcelo Olivas
@mfolivas
Is there a way for me to pluck a particular event, say correlationId from multiple objects and arrays? I am creating an error handler and I want to log the correlationId and send it to my error handler. But there are some objects where the correlationId is nested in different places. Sometimes the events are arrays as well.
wangzengdi
@adispring
@mfolivas Can you show some data?
Anton Melnyk
@heyanton

Hey guys!

Wanted to say again how good ramda is, integrated it on my job and aiming FP path there to reliably handle data processing. Having good feedback from colleagues as well! <3

There is question lately though, that might look trivial, but I have feeling there is solution from FP world that I am just missing.

We use Ramda together with Redux. Particularly, it's useful to compose selector functions as computations (functions that have state -> a signature).

Let's say we have 2 selector functions, one of which needs to use data from another. One selector gets number, other array and I want to use that number as index to array. It looks like this now, using R.nth:

// string[] -> AppState -> string
const nthOfList = list => R.pipe(
  R.path(["myData", "myIndex"]),
  R.nth,
  list
);

// AppState -> string[]
const list = R.pipe(
  R.path(["myOtherData", "myArray"])
);

Now I want to apply list to the nthOfList, but need to call functions with state to get actual list and actual result, because all of them expect state to run computation:

// f(g(x), x)
const elementByIndex = nthOfList(list(state))(state);

But it becomes cumbersome to read quite quickly. I recognized that this is signature of chain and it worked:

const elementByIndex = R.chain(nthOfList, list);
elementByIndex(state);

I'm 99% certain this is some pattern that means something. Is this use case of State monad? I imagine something like this. I would appreciate if you could point me to it. Maybe there is easier way to do what I'm doing (of course I can just write R.nth(myIndexSelector(state), myArray(state)) for this case, but we have many other cases).
The general pattern is "compose 2 selectors without passing state for each of them separately, but passing it once". I have feeling I discovered something from FP wolrd but I'm not sure what :)

3 replies
Tom
@tofo_gitlab
Just wondering if Ramda has a future. I changed the way I code because I thought that Ramda was going to become production ready. But we are still stuck on version 0. I'm loosing interest because I've found it increasingly difficult to justify using it to clients I work with. I don't hear anyone talking about this. Is there a a "stable" alternative to Ramda?
zackkrida
@zackkrida:matrix.org
[m]

@tofo_gitlab: I think it's reasonable to call ramda 'production ready'; I and many others have been using it in shipped code for years.

Personally, using ramda isn't a decision I'd feel the need to share with a client. I use ramda for internals, not exposed as my api or anything, so it'd be a bit like telling a client i was using 'let' instead of 'const'.

What are you looking to see in a v1?

@tofo_gitlab: all of that said, I do see the fp-ts community thriving, and there's fp-ts-std which essentially acts as a ramda replacement: https://samhh.github.io/fp-ts-std/
Stefano Vozza
@svozza
ramda is very stable, can’t remember the last time i encountered a bug
been using it for over 5 years now, back when every new version involved multiple rewrites of your code lol (i’m exaggerating)
yasenfire
@yasenfire
Of course there's future. There's no some specific ramda-driven development, there is functional programming of which ramda brings some tools to JS. If it somehow turns into abandonware in the next few years (not very likely given how JS community goes more and more functional) you'll just switch to yet another library that provides the similar functionality without the need to change your way of thinking about code again. For clients just tell them the minor version if they like it more. There's no some specific magical point after which the program becomes v1.0, it's just about how developers want to label it.
Jacob Lärfors
@jlarfors
Hello! We have some interesting use cases for data manipulation and came across Ramda which looks really nice!
One of the things I am struggling with right now is calculating the Cartesian product of a single object. This object can contain arrays, and basically any type (comes from a GraphQL query). The reason for wanting to calculate the Cartesian product is that we want to render things like tables, trend charts, etc and it makes the data much easier to work with.
Are there any examples of calculating Cartesian product of single objects with Ramda? More than just Cartesian product of two lists... I should note that I have not spent much time looking around Ramda. Not enough to make my own attempt at this :D
Jacob Lärfors
@jlarfors
End result should be an array of objects, where the key is the path to the original data. And the array contains every permutation of the data. This is rather hard to explain, but I think a flattened cartesian product of an object is about the best I can do
anonymouse
@anonymo64520568_twitter
Hi everyone. Question. I have 2 arrays of objects. I want to merge the objects by shared key. Easiest way to explain is to show you via ramda repl -> https://bit.ly/3gvj2wY
anonymouse
@anonymo64520568_twitter

R.merge will merge objects by array index. But given:

[
  {"key": 1, "a": "a", bar: "baz"}, 
  {"key": 2, "b": "b", bar: "bar"}
]

and

[
  {"key": 1, "a": "x", baz: "baz"},
  {"key": 3, "b": "x", baz: "baz"}
]

the result should be:

[
  {"key": 1, "a": "x", bar: "bar", baz: "baz"},
  {"key": 2, "b": "b", "bar": "bar"},
  {"key": 3, "b": "x", "baz": "baz"}
]

So .. the only object that was merged was the one containing { key: 1 }...