Ramda: Remove from list by value

Created on 15 May 2015  路  4Comments  路  Source: ramda/ramda

I apologize if this already exists and I am just not seeing it, but it'd be nice to have something equivalent to underscore's without, though non-destructive of course. E.g.

R.without('c', ['a', 'b', 'c']) // ['a', 'b']  

Most helpful comment

You could use R.reject:

> R.reject(R.eq('c'), ['a', 'b', 'c'])
['a', 'b']

If you want to reject multiple values you could use R.reject in conjunction with R.contains.

> R.reject(R.contains(R.__, ['c', 'd']), ['a', 'b', 'c', 'd', 'e', 'f'])
['a', 'b', 'e', 'f']

All 4 comments

An equivalent of splice would also be nice. I know you can achieve it with insertAll and remove, but the concision of Array.splice is pretty sweet.

You could use R.reject:

> R.reject(R.eq('c'), ['a', 'b', 'c'])
['a', 'b']

If you want to reject multiple values you could use R.reject in conjunction with R.contains.

> R.reject(R.contains(R.__, ['c', 'd']), ['a', 'b', 'c', 'd', 'e', 'f'])
['a', 'b', 'e', 'f']

We rejected trying to duplicate Array.prototype.splice because its API is so hairy.

var x = [1, 2, 3, 4, 5, 6];
x.splice(3, 2, 10, 11); //=> [4, 5]
x; //=> [1, 2, 3, 10, 11, 6]

splice is variadic, with several optional parameters. It returns one thing (the removed list) but mutates another (the original list). There just seemed no clean way to make anything remotely resembling it that fits with Ramda's philosophy. But as you note, a combination of remove and insertAll will cover the functionality.

FYI: R.without now exist: https://ramdajs.com/docs/#without

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antontelesh picture Antontelesh  路  3Comments

ldk18501 picture ldk18501  路  3Comments

FranzSkuffka picture FranzSkuffka  路  3Comments

makarkotlov picture makarkotlov  路  3Comments

alexcarruthers picture alexcarruthers  路  3Comments