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
zrn
@zrn:matrix.org
[m]
I m not good at ramda but it seems you do many times same function just with new arguments, maybe just create a new function that would take an array of arguments and reduce it using your function?
zrn
@zrn:matrix.org
[m]
also you shouldn't mutate inputs if you do fp
Alexei Kozhushkov
@joystick

Hi there, i'm looking for a way to Ramda-mize the following:

const foo = ('someMethod', 'someParam') => x = > x.someMethod(someParam)

Much appreciated 🙃

3 replies
Tristan Forward
@tforward

This question is related to the Ramda Docs and Html sematics.

The Ramda Docs page has headers like "add", that when clicked navigate within the current window to the add function.

Looking at the html:

<h2>
<a tabindex="2" class="name" href="#add">add</a>
<span class="pull-right">
<span class="label label-category">Math</span>
<a target="_blank" title="View source on GitHub" href="https://github.com/ramda/ramda/tree/v0.28.0/source/add.js"><small class="glyphicon glyphicon-new-window"></small></a>
</span>
</h2>

Mdn docs for the a tag say using the ID creates the link,

<p><a href="#Section_further_down">
Jump to the heading below
</a></p>

<h2 id="Section_further_down">Section further down</h2>

Yet in Ramda docs there is no ID, so how does it still work?

zrn
@zrn:matrix.org
[m]
<div id="add" class="section-id"></div>
it is in fact there
but it's strange that h2 is not inside that div
Tristan Forward
@tforward
Thank you! Yeah that div is fairly odd. Not sure why the id and class are not just on the section tag itself vs. this ghost div element.
Justin Carrus
@jcarrus

Hi everyone, I'm new to Ramda (and FP) and was hoping for some help with implementing some code with Ramda.

I have an object o with current value of { foo: [{ value: "bar" }] }.

I would like to create a function that appends an array of newData to one of the child arrays of existingData based on a dynamic criteria (in this case, I just take the first key of existingData).

Without Ramda, it looks like this:

const o = { foo: [{ value: "bar" }] };
function pushToFirstKey(newData, existingData) {
  const key = Object.keys(existingData)[0]
  return {
    ...existingData,
    [key]: [...existingData[key], ...newData]
  }
}
pushToFirstKey([{ value: "baz" }], o);
// Result: {"foo": [{"value": "bar"}, {"value": "baz"}]}

link

My attempt with Ramda looks like this:

const o = { foo: [{ value: "bar" }] };
const pushToFirstKeyFP = (newData, existingData) =>
  R.compose(
    R.assocPath([R.head(R.keys(existingData))], R.__, existingData),
    R.concat(R.prop(R.head(R.keys(existingData)), existingData))
  )(newData);
pushToFirstKeyFP([{ value: "baz" }], o);
// Result: {"foo": [{"value": "bar"}, {"value": "baz"}]}

link

Which I suspect could be better written.

Does anyone have suggestions for me?

zrn
@zrn:matrix.org
[m]
it is kind of strange that you named parameters somethingData, when first is shape of [{}] and second is a different shape {[{}]}.
you can use modify like this:
const mergeArraysAtFirstKey = (arr, obj) => (
  modify(head(keys(obj)),
         concat(arr))
);
also make sure that you actually know what first key will be, if there are keys that could be converted to number then they would be first
Justin Carrus
@jcarrus
@zrn:matrix.org ahh, thanks! That is much cleaner! And yes, good point on my naming. This was really helpful
zrn
@zrn:matrix.org
[m]

I should have mentioned that this is just for illustration, I wanted to understand how exactly it works, so optimisations like that wouldn't work in my case.

I also didn't hope to get any answer after so long, since then for my case I also considered generators, but they have overhead and are slower when entire collection must be computed. What is the performance price of using transducers? They look a bit like generators, object that has a function. So I guess it could be same as generators.

zrn
@zrn:matrix.org
[m]
I think I made a different design so I don't need to solve exactly this problem, still I hope I understood right how transducers work. But I want to know how they perform and when should they be used and when they should not be used.
The Very Angry Caterpillar
@ti_estin_twitter

I don't know if this is useful information, but I found an example where R.flatten() on an array of objects with a length property does not work as intended.

https://ramdajs.com/repl/?v=0.28.0#?const%20obj%20%3D%20%7B%20length%3A%200%20%7D%0Aconst%20array1%20%3D%20%5B%20obj%2C%20obj%2C%20obj%20%5D%0Aconst%20array2%20%3D%20%5B%20array1%2C%20array1%2C%20%5B%20array1%2C%20array1%20%5D%2C%20array1%2C%20array1%5D%0A%0Aconsole.log%28R.flatten%28array2%29%29%0Aconsole.log%28array2.flat%282%29%29

In this example, R.flatten(array) and array.flat() produce different results.
This behavior is probably due to the fact that in the implementation of _isArrayLike(), an object with a length property of 0 is considered an array.
(I found this while using CodeMirror; CodeMirror's Line object has a length property which signifies how many chars the Line has.)

zrn
@zrn:matrix.org
[m]
idk if ramda should do exactly as normal js methods, map doesn't work exactly like method because of index parameter.
if (x.length === 0) { return true; } there is specific case for such object which makes it array like so I guess it works as intended.
I think it makes sense, we have {0: 'a', length: 1} this is array like, same "array" with zero items should look like {length: 0}
zrn
@zrn:matrix.org
[m]
I think whole ramda doesnt work with iterators
Chuánqí Sun
@chuanqisun
Hi all, I'm new to Ramda and not proficient in FP. I saw Maybe.Just, Maybe.Of, and Maybe.Nothing in the documentation but not explained anywhere. Could anyone help point out what they are in JavaScript context and/or where I can learn about them?
1 reply
adellamaggiora
@adellamaggiora

Hi, I would like to know how to make a check inside the pipelines to avoid the process to throw exception.
For example:

const isTypeDensity = R.propEq('type', 'density');

const getDensity = R.pipe(
R.find(isTypeDensity),
R.pick(['value', 'error', 'measureUnit'])
)

when I pass a null argument to getDenisty it will throw an error but also if i pass an empty array (or an array with all alements that don't match the predicate) will throw an error because R.pick will take a null value. How can i fix this issue?
with vanilla javascript i can easily do with something like this:
myArray?.find()?. etc..

thank you for the help.

zrn
@zrn:matrix.org
[m]

you should somehow stop when find is undefined, there is no reason to do the pick at that point

const isTypeDensity = R.propEq('type', 'density');

const data = [{type: 'density', value: '1'}];
const wrongData = [{type: 'abc'}]

const density = //find(isTypeDensity, data);
 find(isTypeDensity, wrongData);

const userPick = pick(['value', 'error', 'measureUnit']);

ifElse(identity, userPick, F)(density);

so something like this works, I can't say that I like it.
it is also not clear what u want returned when it fails.

zrn
@zrn:matrix.org
[m]

:point_up: Edit: you should somehow stop when find is undefined, there is no reason to do the pick at that point

const isTypeDensity = R.propEq('type', 'density');

const data = [{type: 'density', value: '1'}];
const wrongData = [{type: 'abc'}]

const density = //find(isTypeDensity, data);
 find(isTypeDensity, wrongData);

const userPick = pick(['value', 'error', 'measureUnit']);

ifElse(identity, userPick, F)(density);
// or
when(identity, userPick)(density);

so something like this works, I can't say that I like it.
it is also not clear what u want returned when it fails.

Timar
@sugoidesune
Spent some prototyping a Ramdaa DataObject where you can endlessly chain functions where transformers automatically are transduced. Anyone find that interesting?
var data_array = [1, 2, 3,'hot', 4, 5, 6]
var D = x=> new Dataflow()


var data = D()
  .filter(Number.isInteger)
  .filter(x => x % 2)
  .map(x => x * 2)
  // these two filters and map get transduced automagically
  .append('test')
  .filter(x=> x>3)
  .map(x => x * 2)
// filter + map = separate transducer
  .prepend('maroon')
  .compute(data_array)
// [ 'maroon', 5 ]
Timar
@sugoidesune
If someone wants to play around, proof of concept: https://replit.com/@TimarIvo/ramda-data-flow#index.js
Swen Mulderij
@Swendude

Hello everyone, I'm using Ramda with TS and can't get the mapping of objects to work:

const double = (x: number) => x * 2;

R.map(double, { aKey: 1 });

Complains that no overload can be found:

No overload matches this call.
  Overload 1 of 6, '(fn: (x: number) => number, list: readonly number[]): number[]', gave the following error.
    Argument of type '{ aKey: number; }' is not assignable to parameter of type 'readonly number[]'.
      Object literal may only specify known properties, and 'aKey' does not exist in type 'readonly number[]'.
  Overload 2 of 6, '(fn: (x: number) => never, list: { aKey: number; }): unknown', gave the following error.
    Argument of type '(x: number) => number' is not assignable to parameter of type '(x: number) => never'.
      Type 'number' is not assignable to type 'never'.
  Overload 3 of 6, '(fn: (x: number) => number, obj: Functor<number>): Functor<number>', gave the following error.
    Argument of type '{ aKey: number; }' is not assignable to parameter of type 'Functor<number>'.
      Property 'map' is missing in type '{ aKey: number; }' but required in type '{ [key: string]: any; map: <B>(fn: (a: number) => B) => Functor<B>; }'.ts(2769)
What am I doing wrong?
Matt Diamond
@mattdiamond
@Swendude it's possible that the Ramda types from DefinitelyTyped are incorrect
Matt Diamond
@mattdiamond
you might just need to specify your input type as one of the generic parameters
Tom
@tofo_gitlab
It begs the question. Why use Ramda in TS. What are the gains?
I was wondering whether it might be good to add an equivalent function that python has. sorted(). In python it's a simple general purpose non mutating sort function. Could make things a little easier to have in in Ramda. better than Array.prototype.sorted = function() {return [...this].sort()} or const sorted = a = > [...a].sort()
1 reply
Johan Apolinario Véliz
@johan-apo
I am starting my journey learning FP with JS. I have a newbie question. Can anything on OOP be coded using FP?
1 reply
silently
@silently

hello, for guitar players I wanted to share a front-end project I made with ramda (and react): https://silently.github.io/cocotte/

this app iterates on the guitar fretboard to find playable chord voicing (see how ramda is used here for instance: https://github.com/silently/cocotte/blob/main/src/domain/processors/fingering.ts)
Thanks for ramda!

(if you have some feedback don't hesitate opening an issue on the project)

3 replies
[pihentagy]
@pihentagy:matrix.org
[m]
Is there a ramda function, which applies a list of function to one argument?
Or should I misuse ap with a one element array?
const double = x => x*2;
const square = x => x*x;

R.whatever([double, square], 3) => // [6, 9]
3 replies
zrn
@zrn:matrix.org
[m]
map((f)=>f(3), [double, square]) that kind of works
Krupodorov Roman
@seeker1983
rows.map(
row => R.assoc("City", userCities[row.Email] || row["City"])(row)
)
2 replies
How can I rewrite this to remove row variable in mapping?
Akira Cai
@blackwindforce
@seeker1983 converge, lift, ap, juxt, applySpec, Applicative (modify, propOr)
R.map(
  R.lift(R.assoc("City"))(
    R.either(
      R.pipe(
        R.prop("Email"),
        R.prop(R.__, userCities)
      ),
      R.prop("City")
    ),
    R.identity
  )
);
zrn
@zrn:matrix.org
[m]

I have a strange problem:
two functions, with difference in using assoc or dissoc, I d like to do something like

// this is just a simplified example
const k = 1;
const v = 2;
const obj = {};
const abstract = (f) => f(k,v obj)

const assocEr = abstract(assoc)
const dissocEr = abstract(dissoc)

the problem is that dissoc and assoc take different number of arguments, and object is last argument, value is in the middle. Should I define my own functions to assoc/dissoc that would change order of arguments to (obj,k,v) ? Or just give up on abstracting these functions if they are like 2 lines of code anyway?

7 replies
fpdude
@fpdude

Hi, why does transduce fail to work with reduce?

const reduceTransducer = R.compose(R.reduce((acc, x) => R.concat(acc, [x + 1]), []), R.take(2))
const mapTransducer = R.compose(R.map(x => x + 1), R.take(3))

R.transduce(reduceTransducer, R.flip(R.append), [], [1, 2, 3, 4]);
R.transduce(mapTransducer, R.flip(R.append), [], [1, 2, 3, 4]);

Both transducer functions do the same thing, but the reduce variant throws reduce: list must be array or iterable. Can someone enlighten me? :D

zrn
@zrn:matrix.org
[m]
I dont know for sure but map can return transducer and reduce can not, transduce is already performing iteration with reduce so it probably does not make sense
fpdude
@fpdude
Yeah, I got it now, thanks! Turns out it's actually written in the documentation; that functions like map, promap, filter, and etc. "Acts as a transducer if a transformer is given in list position". But I wonder, can I make a reduce function transducable by hand? Is it possible, does it make sense to do? Because I can imagine having a reduce in the pipe/compose stack that I might want to it be transducable, I think. Also, is into a simpler alternative to transduce?
zrn
@zrn:matrix.org
[m]
It must be possible, ramda is written in js, I think it returns an object with some properties so you can create your reduce which would return such object, but do you really need it that much? I am pretty sure ramda took idea of transduce and transducers from clojure where there was first reducers and then transducers and there reduce can not be used as transducer.
I think you already are reducing, so why would you need inside this reduction another reduce and over what should it reduce?
examples are in clojure but you just need to read it instead of function(x,y) as (function x y)
Coop
@Darkle

Hi, I was hoping to get some help with my ramda code with typescript. I am trying to use R.evolve, but am running into a strange type error in typescript that I am a bit confused by. Here is the code on the typescript playground: https://tsplay.dev/wOxApN
You should see the following error:

No overload matches this call.
  The last overload gave the following error.
    Type '<V extends Evolvable<{ [x: string]: (a: string) => unknown; }>>(obj: V) => Evolve<V, { [x: string]: (a: string) => unknown; }>' is not assignable to type '(list: readonly any[]) => any'.
      Types of parameters 'obj' and 'list' are incompatible.
        Type 'readonly any[]' is not assignable to type 'Evolvable<{ [x: string]: (a: string) => unknown; }>'.
          Index signature for type 'string' is missing in type 'readonly any[]'.(2769)

I'm not to sure where its getting the readonly any[] from.