These are chat archives for ramda/ramda

10th
Apr 2016
Baqer Mamouri
@bmamouri
Apr 10 2016 04:08

Hey nice people,

I have a question: I want to call my ramda function with a second arguments. It is easier to explain with an example:

const getAllData =
  R.compose(
    R.uniq,
    R.flatten,
    R.map(R.path(['names'])),
  )

Now getAllData can only works on key names. I wish to use this with other keys as well.

David Chambers
@davidchambers
Apr 10 2016 04:24

@bmamouri, you could change the first line to…

const getAllData = path =>

and change the fifth line to…

R.map(R.path(path))
Baqer Mamouri
@bmamouri
Apr 10 2016 04:33

Thanks @davidchambers, I was thinking of doing that as well, but when you want to call the function you have to do this:

getAllData(['names'])(data)

Not the most elegant way. I couldn't use R. or R.curry to simplify it as well. I thought maybe there is a better way!

David Chambers
@davidchambers
Apr 10 2016 04:38
You could have the function take two args and use R.curry.
Baqer Mamouri
@bmamouri
Apr 10 2016 04:42

Yes you are right @davidchambers. It was so obvious yet I did not see it! Thank you

I actually don't need to use curry as well. Here is my final function:

const extractAllUnique = (data, path) =>
  R.compose(
    R.uniq,
    R.flatten,
    R.map(R.path(path)),
  )(properties)


extractAllUnique(properties, ['name'])
Risto Stevcev
@Risto-Stevcev
Apr 10 2016 11:31
does anyone know if you can construct a template string from a regular string?
without hardcoding a literal, ie
String.template('hello ${name}') -> `hello ${name}`
^ something like that ^
Denys Mikhalenko
@prontiol
Apr 10 2016 11:47
nope
James Forbes
@JAForbes
Apr 10 2016 12:48
({name}) -> `hello ${name}`
Risto Stevcev
@Risto-Stevcev
Apr 10 2016 13:16
@JAForbes That's not the same
In particular, I want to get the value from RegExp.source and convert it into a template
so for example:
const string1 = 'foo', string2 = 'bar'
toTemplateString(/${string1}|${string2}/.source)
// `foo|bar`
the CSS BNF grammar has a mixture of templates inside regexp, so I can't rewrite the above using something like parser.either w/ a parser combinator lib
at least not without making it substantially harder to read
James Forbes
@JAForbes
Apr 10 2016 13:52
oh I see, my mistake didn't see the change in ticks/quotes
Risto Stevcev
@Risto-Stevcev
Apr 10 2016 14:01
I figured out somewhat of a hack, but it's the best I think it can get:
@ram-bot
const tmpl = string => new Function('return `'+ string +'`')()
const name = 'foo'
tmpl('hello ${name}!')
ram-bot
@ram-bot
Apr 10 2016 14:02
'hello foo!'
Denys Mikhalenko
@prontiol
Apr 10 2016 14:24
nice
Risto Stevcev
@Risto-Stevcev
Apr 10 2016 18:40
@joneshf nope
It turns out that template literals aren't quite like templates in libraries, in that they can only be defined at runtime
the example I gave is a bit of a hack, and only slightly better than using eval because unlike eval, creating the function body in that way makes it so that it only has access to its function scope and the global scope, and no scopes in between
so it's slightly safer, though the code I need to create has nothing to do with user input, so it's safe by default
Denys Mikhalenko
@prontiol
Apr 10 2016 18:49
you still can use (0, eval) hack to perform a global eval
i prefer Function constructor though
Risto Stevcev
@Risto-Stevcev
Apr 10 2016 19:07
@prontiol Oh nice, didn't know about that one
Risto Novik
@riston
Apr 10 2016 20:33
Is there any eloquent solution for this example in Ramda, I mean for reduce job for aggregation.
var word = "abcdgfaa";

var result = word
    .split("")
    .reduce((prev, current) => {
        prev[current] = !prev[current] ? 1 : ++prev[current]; 
        return prev;
}, {});

console.log("Counted", result);
Denys Mikhalenko
@prontiol
Apr 10 2016 20:40
R.countBy(R.identity)
Michael Hurley
@buzzdecafe
Apr 10 2016 21:40
:zap: