by

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
Kevin Jones
@imkevinjones
really simple
Brad Compton (he/him)
@Bradcomp
For me it really can help to look at the types of the different functions I'm dealing with, and make sure that each function is expecting the same thing the last function is returning if that makes sense?
Kevin Jones
@imkevinjones
Yep, that's the usual. I was just being mislead by every google result that had people like me who were looking for ramda + async answers and the only examples were migrating from pipeP to pipeWith(andThen)... lots of outdated code examples out there.
Brad Compton (he/him)
@Bradcomp
Interesting haha, I've definitely explained that migration more than once.
Kevin Jones
@imkevinjones
I can't even imagine. With Ramda gaining a lot of ground in the JS FP game, people are jumping on board in droves.
yasenfire
@yasenfire
I use pipeWith(andThen), mostly when I have a promise somewhere early in the pipeline and all other transformations are over its result
Technically nothing stops you from writing const asyncCompose = (f, g) => x => { const a = await g(x); return f(a); } and use this but it doesn't really have benefits over promises. It's just a syntactic sugar for promises and it's strictly for imperative code so no much benefit from using it in "ramda-code". Even worse, promises are at least objects and data so it's easier to deal around them in functional code.
What is even better is to not use promises in their direct or async/await form and going directly to Tasks and Futures (for JS it means using Fluture).
Azat S.
@azat-io
I have three arguments in my function. Third argument is function which I want to call with argument from second argument. I there any way to make it?
callFuncWithArgs(
  nthArg(1),
  nthArg(2),
)(null, 3, a => a * a) // 9
Ben Briggs
@ben-eb
@azat-io
converge(applyTo, [nthArg(1), nthArg(2)])
Azat S.
@azat-io
@ben-eb Awesome, thank you so much!
Ben Briggs
@ben-eb
@azat-io You're welcome :slight_smile:
Same thing works with call if you swap around the nthArgs. Not sure which I prefer
Azat S.
@azat-io
Thanks!
Collin Kaepernick
@llaenowyd

I thought maybe this doc is not reflective of exception safety

once
Accepts a function fn and returns a function that guards invocation of fn such that fn can only ever be called once, no matter how many times the returned function is invoked. The first value calculated is returned in subsequent invocations.

But actually not bad 🤔 seems to behave as stated:

let onceSubjectCallCount = 0;

const onceSubject = (doThrow=false) => {
  onceSubjectCallCount++;
  if (doThrow) throw onceSubjectCallCount
  return onceSubjectCallCount;
}

const onceSubjectOnce = R.tryCatch(
  R.once(onceSubject),
  R.identity
)

const result = [
  onceSubjectOnce(true),
  onceSubjectOnce(true),
  onceSubjectOnce(true)
]

result // [1, undefined, undefined]

:clap:

Bruno Martins
@Martins7
Hello, could someone help me?
I'm getting error in jest because of a lib function
 TypeError: g.call is not a function

at node_modules/ramda/src/internal/_pipe.js:3:14
at node_modules/ramda/src/internal/_arity.js:11:19
I'm using version 0.27.0 of the package
Brad Compton (he/him)
@Bradcomp
Can you post the code or an example that is causing the error? My gut says there is a value being passed to pipe that isn't a function and that is causing this error.
Bruno Martins
@Martins7
import withRouter from 'next/router';
import { Head, UserInfoAndNavigation, WithAuth, WithLogs, Error } from 'components';

const enhance = (R.compose(withRouter, WithAuth, WithLogs) as any);

// WithAuth
export default ComposedComponent => {
  class Component extends React.PureComponent<IProps> {  }
   return connect(mapStateToProps, mapDispatchToProps)(Component);
}

//WithLogs
export default ComposedComponent =>
  class extends React.PureComponent { }
Brad Compton (he/him)
@Bradcomp
An error occurs when you call enhance?
I don't see anything here that looks weird. WithAuth, WithRouter, and WithLogs are all functions so that compose should work fine.
Looking deeper right now...
Bruno Martins
@Martins7
The error is caused when I run my tests via JestJS, when trying to assemble my component
Test suite failed to run

    TypeError: g.call is not a function

      123 | };
      124 | 
    > 125 | export default connect(mapStateToProps, mapDispatchToProps)(enhance(WithRouterGlobalLayout));
          |                                                             ^
      126 | 

      at node_modules/ramda/src/internal/_pipe.js:3:14
      at node_modules/ramda/src/internal/_arity.js:11:19
Brad Compton (he/him)
@Bradcomp

https://github.com/ramda/ramda/blob/master/source/internal/_pipe.js

Here is the offending code. g will take on the values of WithAuth and WithRouter over the course of executing enhance. I am assuming both of those are Functions and thus have a call method on them, but we can verify that experimentally too.

Does this code fail when running outside a test harness? If not then Jest could be messing with the prototypes or something.

If so, then we can try removing first one and then the other of those two functions from enhance and see if either way fixes things, or else just try WithAuth.call(WithAuth, component) and the same for WithRouter

Can I call function like this without losing this?
Cannot read property 'x' of undefined
yasenfire
@yasenfire
You can if you use bind(this) strategically. Or you can use invoker(0, 'wow')
Azat S.
@azat-io

You can if you use bind(this) strategically. Or you can use invoker(0, 'wow')

Could you give me example? I’m trying to make it with invoker

Azat S.
@azat-io

Like this?

const callFunc = compose(
  flip(useWith(
    invoker(1, 'wow'),
    [
      prop('z'),
    ],
  )),
)

callFunc(S, someObjWithZ)

How can I drop first argument if my call function is callFunc(null, S, someObjWithZ)?

yasenfire
@yasenfire
const wow = invoker(1, 'wow');
const callFunc2 = converge(call, [
  compose(flip(wow), nthArg(0)), 
  compose(prop('z'), nthArg(1))
]);
callFunc2(S, someObjWithZ);
Collin Kaepernick
@llaenowyd

I was reading this article on medium "Understanding Functor and Monad With a Bag of Peanuts," that says

  1. associative law
    ...
    Functor[X].map(f).map(g) == Functor[X].map(x => g(f(x))
    This law allows us to chain map function calls instead of composing multiple functions and then applying them in a single map operation.

As a contrived example, I think the 2nd version extractPath2 could use a lot more memory on some inputs:

const parse = R.tryCatch(JSON.parse, R.always(null))
const viewPath = R.compose(R.view, R.lensPath)

const extractPath1 = path => R.map( R.compose(viewPath(path), parse) )

const extractPath2 = path => R.compose( R.map(viewPath(path)), R.map(parse) )

The medium article, maybe it's only to say the 2 extractPath functions yield the same result, without comment on runtime performance.

Or is it suggesting that a fp language runtime might take advantage of this "associative law" to rearrange equivalent compositions as an optimization?

Does ramda do anything with this code beyond naive expectation on reading it?

yasenfire
@yasenfire
It will not necessarily use more memory but it will definitely create tempB = map(viewPath(path), tempA) and tempC = map(parse, tempB). Yes, true fp language can optimize it (though maybe not. For example, such an optimization would make not much sense in haskell -- with laziness both cases work in the same way). No, ramda doesn't do it for you, it's your job as a developer to see there are n functions in a composition wrapped by map and it can be replaced with map of composition. Ramda however supports transducers, lazy by design, so you can write in this style to exlude any possibility for a mistake.
amir
@amirbr
Hola small question, prop('name', undefined) should throw an error?
because in my app it does throw an error. and when I try it on ramda REPL it return undefined.
Am I doing something wrong?
yasenfire
@yasenfire
Are you sure that prop throws an exception and not some code above that uses result of prop('name', undefined)?
3 replies
Brad Compton (he/him)
@Bradcomp
What version of Ramda are you using?
amir
@amirbr
0.25.0
Brad Compton (he/him)
@Bradcomp

Interesting... just looked at the changelog and it was 0.25 that actually updated the behavior. ramda/ramda#2319

It definitely works in the most recent version: https://ramdajs.com/repl/#?prop%28%27a%27%2C%20undefined%29

amir
@amirbr
yes, you right, i fix it.
Thank you so much @Bradcomp
Damien Remars
@damrem
hi, anyone knows how to display another version than the last one on this site?
Brad Compton (he/him)
@Bradcomp

@damrem You can put the version in the docs URL like this: https://ramdajs.com/0.25.0/docs/

We should make that documented or easier to discover.

Damien Remars
@damrem
@Bradcomp Thank you!
Marcelo Olivas
@mfolivas
new to Rambda. Really liking it so far. Can someone show me some examples of how to use pipe along with promise. I understand that you could use pipeWith but I would love to see some examples.
John Hartnup
@ukslim
@mfolivas First tip: don't confuse Rambda with Ramda
John Hartnup
@ukslim

Is there an idiomatic alternative to:

const adjustFirst = (pred, fn) => list => {
   const index = findIndex(pred, list);
   return adjust(index, someFunction, list); 
}

?

3 replies
Collin Kaepernick
@llaenowyd
is it possible to define an empty lens, something like,
[ R.isNil(R.view(emptyLens)(anyObject)),
  areEquivalent( R.set(emptyLens, anyValue), R.identity )
] // [true, true]
Brad Compton (he/him)
@Bradcomp
@llaenowyd I think we can, though I am unsure how lawful it is (didn't check haha). We can look at how a lens is constructed with a getter and a setter, then use those to get us where we want to go:
const getter = always(undefined)
const setter = (a, b) => b

const emptyLens = lens(getter, setter)

view(emptyLens, "anything") // undefined
set(emptyLens, "anything")(12) // 12
1 reply
Johnny Hauser
@m59peacemaker
Can anyone understand this? http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.641.2441&rep=rep1&type=pdf
And if so, how do I learn and how long did it take you?
enricofadly
@enricofadly
import R from 'ramda'
import scores from './scores.json'

const reviewCreditScores = R.map(
  R.cond([
    [R.lte(800), R.always(`${'value'} is excellent`)],
    [R.lte(700), R.always(`${'value'} is good`)],
    [R.lte(650), R.always(`${'value'} is fair`)],
    [R.gte(649), R.always(`${'value'} is poor`)]
  ])
)
how can i get the current element that iterate through map in the ramda so i can replace the 'value' string with it
1 reply