These are chat archives for ramda/ramda

14th
Feb 2018
Jason Shin
@JasonShin
Feb 14 2018 02:13
Sorry guys I was working
so for example
let zz = [{x: 1}, {x: 2}]
R.find(R.propEq('z', 1), zz)
In this example
Instead of silently returning undefined, is it possible to enforce an error?
Mike Lambert
@lax4mike
Feb 14 2018 02:15
you can check out R.defaultTo
Jason Shin
@JasonShin
Feb 14 2018 02:16
yeah, but it will still make it hard to track errors
Mike Lambert
@lax4mike
Feb 14 2018 02:16
or maybe R.tap(checkForError)
Jason Shin
@JasonShin
Feb 14 2018 02:16
I think
Mike Lambert
@lax4mike
Feb 14 2018 02:16
you define checkForError
Jason Shin
@JasonShin
Feb 14 2018 02:16
I'd love to see if Ramda can simply throws a type error or
value error
Mike Lambert
@lax4mike
Feb 14 2018 02:17
well, Array.find doesn't throw an error either
Jason Shin
@JasonShin
Feb 14 2018 02:18
Yeah, why don't we address this issue in Ramda instead of inheriting it?
Mike Lambert
@lax4mike
Feb 14 2018 02:18
i don't see it as an issue
Jason Shin
@JasonShin
Feb 14 2018 02:18
...hmm right
Mike Lambert
@lax4mike
Feb 14 2018 02:18
try/catch is only one way of handling errors
one that doesn't really jibe with functional programming...
Jason Shin
@JasonShin
Feb 14 2018 02:19
true
I was trying to use Ramda in TS but it was very difficult.
I was spending 90% time on fixing TS type errors
Ivanov Ivan
@832bb9
Feb 14 2018 07:43
@xgrommx, as mentioned in R.zip docs "The returned list is truncated to the length of the shorter of the two input lists.". But in my case i need so the resulted array has length of the longest array. Is there is a better way to achieve that?
Ivanov Ivan
@832bb9
Feb 14 2018 07:48
@bijoythomas R.ap is ok, but it seems to me, that curryN(1) is more obvious. Thanks, by the way.
Matthew Willhite
@miwillhite
Feb 14 2018 15:50
@JasonShin Maybe what you are looking for is something like Sanctuary that gives you some type safety around your functions
Brad Compton (he/him)
@Bradcomp
Feb 14 2018 16:28
@JasonShin I will typically use Maybe for stuff like that, and there are a few helper functions I define that can make life easier:
//(a -> Bool) -> a -> Maybe a
const maybeFromPred = curry((pred, val) => 
    pred(val) ? Just(val) : Nothing());

//(a -> b?) -> a -> Maybe b 
const toMaybe = curry((f, val) => 
    maybeFromPred(complement(isNil), f(val)));
With those, you can just do:
toMaybe(find(propEq('z', 1)))(zz)
It's possible whatever Maybe library you want to use has similar functionality, but this will work with any Maybe library.
functionalStoic
@functionalStoic
Feb 14 2018 19:41
 //Remove Labels and Measurements
  [
    'label',
    'width',
    'height',
    'spacing',
    'spacingTop',
    'spacingRight',
    'spacingBottom',
    'spacingLeft'
  ].map(label => WALL[`${name}-${label}`].remove());
Trying to determine why this is a bad pattern. Doesn’t feel good but I’m not sure why besides it clearly not being pure. The array is a list of properties I need to remove from and an object(WALL). Any better pattern or ramda function that would be ideal for this?
Bijoy Thomas
@bijoythomas
Feb 14 2018 19:47
omit?
functionalStoic
@functionalStoic
Feb 14 2018 19:49
Hmm. Ok That looks pretty ideal Thanks!
Bijoy Thomas
@bijoythomas
Feb 14 2018 19:49
yw
functionalStoic
@functionalStoic
Feb 14 2018 19:59
@bijoythomas Oh I guess I missed the main part in that I need to run the remove() function that is on each of those properties
Vasili Sviridov
@vsviridov
Feb 14 2018 20:01
@functionalStoic you can map with invoker for that, but why do you want to mutate the objects?
also then maybe use forEach to hint at the side-effects
functionalStoic
@functionalStoic
Feb 14 2018 20:02
The answer to your question is I’m not sure why I want to mutate and that I don’t. It’s just the method of the library I’m using and trying to figure out a better way
Vasili Sviridov
@vsviridov
Feb 14 2018 20:03
ah... i see, which library is that?
functionalStoic
@functionalStoic
Feb 14 2018 20:03
Is running that remove() function that the library(paperJS) provides inherently mutating?
Vector/Canvas library
Vasili Sviridov
@vsviridov
Feb 14 2018 20:04
well, I assume that since you're running a method on an object it would then mutate said object...
functionalStoic
@functionalStoic
Feb 14 2018 20:05
Agreed. Not sure what the better alternative approach is
Vasili Sviridov
@vsviridov
Feb 14 2018 20:06
that would really depend on what the library is doing...
joao@kapmug
@jay-jlm
Feb 14 2018 20:38
Hey guys, I'm learning about Ramda's juxt and how it (potentially) differs from the Clojure version... what I'm trying to do is to expose list values as property names. Here's how it can be done in Closure:
(into {} (map (juxt identity name) [:a :b :c :d]))
;;=> {:a "a" :b "b" :c "c" :d "d"}
And here's my attempt with Ramda, but I haven't been able to do it with juxt:
R.pipe(R.groupBy(o => o), R.map(R.head))(['a', 'b', 'c'])
Alternatively:
R.pipe(R.groupBy(o => String(o)), R.fromPairs)(['a', 'b', 'c'])   //  =>  { a: "a", b: "b", c: "c" }
joao@kapmug
@jay-jlm
Feb 14 2018 20:44
but can it be done is a more similar way to the Clojure version?
Bijoy Thomas
@bijoythomas
Feb 14 2018 20:52
compose( apply(zipObj), juxt([map(toString), identity]) )(['a', 'b', 'c']) will that help?
Ebrahem Al-Hajeri
@ehajri
Feb 14 2018 21:20
hello, is it possible to rewrite R.prop('Total', R.find(R.propEq('Date', obj))(list))? I'll reuse this line and would like to go for something like f(obj)(list)
tried compose and curry but didn't work for me
Vasili Sviridov
@vsviridov
Feb 14 2018 21:28
so, find an object in the list, where Date matches the predicate and then extract the Total property from that object?
Ebrahem Al-Hajeri
@ehajri
Feb 14 2018 21:29
yes
Vasili Sviridov
@vsviridov
Feb 14 2018 21:31
date => pipe(find(propEq('Date', date)), propOr(null, 'Total')) doesn't work for you?
Ebrahem Al-Hajeri
@ehajri
Feb 14 2018 21:35
Not sure why it works :P i need to read it a couple of times, thanks!
Vasili Sviridov
@vsviridov
Feb 14 2018 21:37
date => pipe(
    find(propEq('Date', date)), // Find the thing in the list first, explicitly passing date, doing it point-free would be really suboptimal
    propOr(null, 'Total'), // From that extract the property, but since find might return null - use propOr to mitigate for that case
)
the resulting function would await for the list to pass through this pipeline
so you could use it as
const findBySpecificDate = findByDate('01-01-2018');
const item = findBySpecificDate(list);
could be wrapped into curryN if you want to pass both arguments at once...
joao@kapmug
@jay-jlm
Feb 14 2018 21:42
@bijoythomas nice one!
Bijoy Thomas
@bijoythomas
Feb 14 2018 21:48
:+1:
joao@kapmug
@jay-jlm
Feb 14 2018 21:49
@bijoythomas I get some funny characters thou:
{
    "\"a\"": "a",
    "\"b\"": "b",
    "\"c\"": "c"
}
Jason Shin
@JasonShin
Feb 14 2018 22:06

@miwillhite Thanks, Sanctuary sounds great. I'm using Ramda heavily in this project https://github.com/Biphub/Biphub. If I am going to refactor it to Sanctuary, is it going to require a lot of work?

@Bradcomp Thanks, I will have a look

Bijoy Thomas
@bijoythomas
Feb 14 2018 22:09
@ram-bot compose( apply(zipObj), juxt([map(toString), identity]) )(['a', 'b', 'c'])
hmm .. how does one get the ram-bot to run code?
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:16
@jay-jlm its because of the toString being run on a string. I guess it would be nice if toString did a check on its argument
Brad Compton (he/him)
@Bradcomp
Feb 14 2018 22:16
@ram-bot
compose( apply(zipObj), juxt([map(toString), identity]) )(['a', 'b', 'c'])
ram-bot
@ram-bot
Feb 14 2018 22:16
{ '"a"': 'a', '"b"': 'b', '"c"': 'c' }
Brad Compton (he/him)
@Bradcomp
Feb 14 2018 22:16
You need to wrap the code in triple backtics
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:16
@ram-bot
compose(
  apply(zipObj),
  juxt([map(when(complement(is(String)), toString)), identity])
)(['a', 'b', 'c'])
ram-bot
@ram-bot
Feb 14 2018 22:16
{ '"a"': 'a', '"b"': 'b', '"c"': 'c' }
joao@kapmug
@jay-jlm
Feb 14 2018 22:17
see :-)
funny characters :-(
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:18
yes .. I thought the check for string type would help
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:24
@ram-bot
compose(
  apply(zipObj),
  juxt([identity, identity])
)(['a', 'b', 'c'])
ram-bot
@ram-bot
Feb 14 2018 22:24
{ a: 'a', b: 'b', c: 'c' }
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:26
@jay-jlm I guess you could get away w/o the explicit toString conversion since the resulting object will have the keys as string
and thank you @Bradcomp
Jason Shin
@JasonShin
Feb 14 2018 22:36
I just wanna dance today
Denis Stoyanov
@xgrommx
Feb 14 2018 22:44
@bijoythomas compose(mergeAll, map(unnest(objOf)))(['a', 'b', 'c'])
Bijoy Thomas
@bijoythomas
Feb 14 2018 22:53
@xgrommx that is nice! I understand compose(mergeAll, map(ap(objOf, identity)))(['a', 'b', 'c']). I assume unnest is behaving like the S combinator?
or maybe not
joao@kapmug
@jay-jlm
Feb 14 2018 23:00
@xgrommx , thank you, although the initial question was about how to accomplish the same result using juxt()
here's how it's done in closure
(into {} (map (juxt identity name) [:a :b :c :d]))
;;=> {:a "a" :b "b" :c "c" :d "d"}
joao@kapmug
@jay-jlm
Feb 14 2018 23:21
btw, I'm curious how this actually works:
unnest(objOf())('a') // => {"a": "a"}
since objOf needs two inputs to work, not sure where that is coming from