Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImmutableJS vs Ramda or ImmutableJS + Ramda - can they be used together

I'm exploring these two libs and for me ImmutableJS has (mostly) immutable data structures whereas Ramda has swiss army knife set of FP utils.

When I google I see articles like Ramda vs ImmutableJS that recommend one or the other, but at first glance, at least to me, they complement each other.

I can imagine having all my data structures in React app to be ImmutableJS (List, Set, Map, Record) but use Ramda to compose functions, make transformations on ImmutableJS collections etc or when I miss some features like uniq.

What do you think? Is it sensible approach?

like image 652
dragonfly Avatar asked Sep 03 '25 07:09

dragonfly


1 Answers

Can they be used together; not smoothly or seamlessly.

It is most probably an approach that, as things stand currently, will lead you toward at least some difficulty, and most probably a great deal of difficulty. The reason for this is that immutable's structures are not compatible with ramda's functions. You may be able to create wrappers or an interop to patch this functionality in, but this seems like a fairly big project.

This issue goes into further detail, and includes some potential alternatives to immutablejs such as List.

Here's a very small example of why the lack of interoperability is a problem:

const { Map } = Immutable;
const { path } = R;

const standardObject = {a: 1, b: 2, c: 3};
const map1 = Immutable.Map(standardObject);

const getA = path(['a']);

const result = getA(map1);
const defaultResult = getA(standardObject);

console.dir(result);
console.dir(defaultResult);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>

You could presumably build the conversions into the work you're doing in ramda; this seems like the simplest way around the problem though it does mean you'd be tying your code very closely to immutable data structures. That might work like this:

const { Map } = Immutable;
const { path, pipe } = R;

const standardObject = {a: 1, b: 2, c: 3};
const map1 = Immutable.Map(standardObject);

const toStandard = map => map.toObject()

const getA = pipe(
  toStandard,
  path(['a'])
)

const result = getA(map1)

console.dir(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>
like image 151
OliverRadini Avatar answered Sep 05 '25 01:09

OliverRadini