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
Christer Nilsson
@ChristerNilsson

What does this do?

split '\n'
map 
  split ','
  map trim
splitAt 1 
apply lift zipObj

Check out here

17 replies
klequis
@klequis

Although I have successfully used it a couple of times, I'm not understanding how to use R.reduce / reduceBy. Here is an example in plain JS that I would like to do with Ramda

const objs = [
  {
    name: '1',
    fetch: {
      status: 'a'
    }
  },
  {
    name: '2',
    fetch: {
      status: 'b'
    }
  },
  {
    name: '3'
  },
  {
    name: '4',
    fetch: {
      status: 'c'
    }
  }
]

const reduceIt = objs => {
  return objs.reduce((accum, curVal) => {
    if (R.has('fetch')(curVal)) {
      accum.push(curVal)
    }
    return accum
  },[])}


console.log(reduceIt(objs))
/* result
[
  { name: '1', fetch: { status: 'a' } },
  { name: '2', fetch: { status: 'b' } },
  { name: '4', fetch: { status: 'c' } }
]

*/

It looks to me like I should be using R.reduceBy?

Christer Nilsson
@ChristerNilsson
@klequis Why not filter?
 R.filter(x => x.fetch, objs)
klequis
@klequis
@ChristerNilsson Likely I need more sleep :) - thanks.
Christer Nilsson
@ChristerNilsson
Can anyone provide an example where Ramda beats pure JS in execution time?
9 replies
Christer Nilsson
@ChristerNilsson
@klequis As nobody is complaining about my long and boring code, I have to do it myself:
R.filter(fetch, objs)
Or even shorter:
const r = R.filter(fetch)
console.log(r(objs))
Charles Hughes
@chughes87
what about R.filter(R.has('fetch'))
Charles Hughes
@chughes87
I made this today for work. I think this illustrates how nice Ramda can be sometimes: https://i.imgur.com/XxLj3Vl.png
Christer Nilsson
@ChristerNilsson
@chughes87 I guess the right side is the better version. Do you have an input/output pair so I can grasp what is happening? I also think I could make the right side even less complex with tinyRamda.
20 replies
Christer Nilsson
@ChristerNilsson
I would like to know how to use Ramda to solve a search problem.
# You have three operations: +2, *2 and /2
# Try to go from 3 to 4
# The solution: 3 6 8 4   (*2 +2 /2)

# trace of front:
# [ 3 ]
# [ 5, 6 ]
# [ 7, 10, 8, 12 ]
# [ 9, 14, 20, 16, 4, 24 ]

log = console.log

[START,TARGET] = [3,4]

origin = { [START] : 0 }
front = [START]
front1 = []

operation = (a,b) => if b not of origin
    origin[b] = a # side effect
    front1.push b # side effect

while TARGET not in front
    log 'front',front
    front.map (x) => 
        operation x, x + 2
        operation x, x * 2
        if x % 2 == 0 then operation x, x / 2
    [front,front1] = [front1,[]] # side effect

log 'front',front
log 'origin',origin

last = TARGET
actual = [last]
while last
    last = origin[last]
    if last != 0 then actual.unshift last
log 'actual', actual
log 'expect', [3,6,8,4]
origin {
  '3': 0,
  '4': 8,
  '5': 3,
  '6': 3,
  '7': 5,
  '8': 6,
  '9': 7,
  '10': 5,
  '12': 6,
  '14': 7,
  '16': 8,
  '20': 10,
  '24': 12
}
Christer Nilsson
@ChristerNilsson

isOdd taken from Ramda Adjunct:

const isOdd = both(isInteger, pipe(flip(modulo)(2), complement(equals)(0)));

Seven keywords. Seems a lot.

2 replies
Christer Nilsson
@ChristerNilsson
Ramda got a little bit easier to use in Observable today. See this notebook
Christer Nilsson
@ChristerNilsson
@rajukc530 Please give an example of what you would like to do.
Arian R
@arianra
I forgot how magnificent this channel is

isOdd taken from Ramda Adjunct:

const isOdd = both(isInteger, pipe(flip(modulo)(2), complement(equals)(0)));

Seven keywords. Seems a lot.

This is absolutely glorious

thanks for making me feel all warm and tingly inside
Christer Nilsson
@ChristerNilsson

@arianra I guess

isOdd = x % 2 == 1

does not tingle that much!

Arian R
@arianra
ramda shall be the last remnants of humanity when github's skynet takes over
Christer Nilsson
@ChristerNilsson
I'm hoping for an ai that produces ramda code given input and output.
Arian R
@arianra
don't kill my dream of being part of the rebellion
Christer Nilsson
@ChristerNilsson
One day ramda will be considered the new assembler.
Joshua Travis
@jktravis

Is there a Prettier plugin to better handle composes? For example:

const uniqAndSortByLabel = R.compose(
    R.sortBy(R.prop('label')),
    R.uniqBy(R.compose(
        R.toLower,
        R.trim,
        R.prop('label'),
    )),
)

will be transformed to this:

const uniqAndSortByLabel = R.compose(
    R.sortBy(R.prop('label')),
    R.uniqBy(R.compose(R.toLower, R.trim, R.prop('label')))
);

I know there's the comment // prettier-ignore, but I also don't want to put that everywhere I'm using compose.

laputan_machine
@laputan_machine:matrix.org
[m]
Hello, is there a callN function in ramda? e.g. if I wanted to repeat something n times, what would be the idiomatic way of doing this in Ramda?
Nik
@niksosf
@laputan_machine:matrix.org there is a ‘times’ function. Does that help ?
So ‘’’ times( (n)=> fn(), 42)’’’
Lewis
@6ewis
any suggestions to refactor this?
Christer Nilsson
@ChristerNilsson
@6ewis Might be a good idea to give one or more I/O-pairs instead of code.
Dustin Stiles
@dustinws
Hello, is there a clean way to rename a property on an object? I have a function that does it but I wasn't sure if ramda has a nice one liner.
// What I have for turning a mongo document into a domain record
const renameIdProp = ({ _id, ...params }) => ({
  ...params,
  id: _id,
});
5 replies
Abhishek Dubey
@ad99526
Hi guys
I need a ramda function which maps and filters over one iteration
[1,2,3,4].mapAndFilter(el => el !== 1 && 0) //should return [0,0,0]
Christer Nilsson
@ChristerNilsson
@ad99526
map(el => 0, filter(el => el != 1, [1,2,3,4]))
I think you need a transducer. Check out this video: https://youtu.be/SJjOp0X_MVA
Abhishek Dubey
@ad99526
@ChristerNilsson this loops the array twice
@ChristerNilsson i need something like flatMap
3 replies
Christer Nilsson
@ChristerNilsson
@ad99526 It's ok to augment Ramda. If you need a flatMap, just define it in vanilla javascript and add some curry. Remember, Ramda is not cut in stone. A plain for loop is 250% faster than using map and filter.
Charles Hughes
@chughes87
Ramda's flatmap is chain
Andy Mac
@zxol
Can someone please help me understand the purpose of ramda's internal function _curry1? Specifically, the path of when args is zero, return f1?
export default function _curry1(fn) {
  return function f1(a) {
    if (arguments.length === 0 || _isPlaceholder(a)) {
      return f1;
    } else {
      return fn.apply(this, arguments);
    }
  };
}
2 replies
is it part of the placeholder logic, as well?
I'm trying to write my own curry function and i was wondering if I can ignore this part of ramda if I don't implement placeholders
klequis
@klequis

I have been going through Ramda's source code as a learning exercise and don't understand the following:

Here is a simplified curry3 function

import _curry2 from './fns/_curry2' // copy of Ramda function
import _curry3 from './fns/_curry3' // copy of Ramda function

export function curry3(fn) {
  return function f3(a, b, c) {
    const argsLen = arguments.length
    log('curry3 argLen', argsLen)
    switch (argsLen) {
      case 0:
        return f3
      case 1:
        return _curry2(function (_b, _c) {
          return fn(a, _b, _c)
        })
      case 2:
        return _curry1(function (_c) {
          return fn(a, b, _c)
        })
      case 3:
        return fn(a, b, c)
    }
  }
}

Wrap a function in it

const c3 = curry3(function curryMe3(a, b, c) {
  return `curryMe3: ${a}, ${b}, ${c}`
})

const c3 = curry3(function curryMe3(a, b, c) {
  return `curryMe3: ${a}, ${b}, ${c}`
})

And call it

log('c3-0', c3())
// -> argLen 0
// -> c3 [Function: f3]

log('c3-1', c3(1))
// -> argLen 1
// -> c3 [Function: f2]

log('c3-2', c3(1, 2))
// -> argLen 2
// -> c3 [Function: f1]

log('c3-3', c3(1, 2, 3))
// -> argLen 3
// -> c3 curryMe3: 1, 2, 3

So c3 receives 1 argument as fn. What I dont understand is how the arguments passed to curryMe3 get into f3 as 0, 1, 2 or 3 separate arguments.

klequis
@klequis
@zxol I wrote my own version of _curry3 minus the placeholder logic. It appears to be working well
salmondorf
@salmondorf:matrix.org
[m]
Is there a good way to refactor this function?
const sumEq = curry((n, list) => pipe(sum, equals(n))(list));
oomusou
@oomusou
@salmondorf:matrix.org How about let sumEq = useWith (equals, [identity, sum]) ?
salmondorf
@salmondorf:matrix.org
[m]
Hmmm. Looks like that will probably work. Thanks!
Nicholai Nissen
@Nicholaiii
Is there a ramda function that is the equivalent of (e: T | T[]): T[] => Array.isArray(e) ? e : [e]
1 reply
klequis
@klequis
I figured it out - yeah
Marcelo Olivas
@mfolivas

How can I validate that a request is a JSON object? I did something like this:

const getTags = (request) => {
  if (isEmpty(request) || !is(Object, request)) {
    throw new TypeError(
      'The #request is required and needs to be an JSON object'
    )
  }

But then I realized that is(Object, []) returns true