These are chat archives for ramda/ramda

24th
Sep 2018
Cristian Velasquez Ramos
@cevr
Sep 24 2018 04:00
I'm having trouble reasoning about how to convert this using a more functional approach (I am new to functional programming):
formatTime: time => {
        var hours = Math.floor(time / 60);
        var minutes = time % 60;
        if (hours == 0 && minutes == 0) return '';
        if (minutes < 10) minutes = `0${minutes}`;
        return `${hours}:${minutes}`;
    }
Could anyone provide some guidance?
Johnny Hauser
@m59peacemaker
Sep 24 2018 04:11
@cevr the first thing I would do is make each line you have there as functional as you can
just a sec
I would try to separate out hours from minutes as well. Your logic is a bit mixed up to me.
Baqer Mamouri
@bmamouri
Sep 24 2018 04:22

What is the functional way to pass a variable to an inner function? Let me demonstrate using code snippet:

var data = {
            "homeId": 1,
            "hourlyBudget": {
                "cost": {
                    "total": "0.28",
                    "remainder": "0.28",
                },
                "energy": {
                    "total": "1115",
                    "remainder": "1115",
                },
                "emission": {
                    "total": "0.925",
                    "remainder": "0.925",
                }
            },
            "hourlyRealtimeData": {
                "cost": {
                    "absolute": "0.19",
                    "relative": "0.679"
                },
                "grossEnergyUse": {
                    "absolute": "850",
                    "relative": "0.762"
                },
                "emission": {
                    "absolute": "0.705",
                    "relative": "0.762"
                }
            }
        };


R.pipe(
  R.assoc('hourlyBudget', R.map(
    R.mapObjIndexed((num, key, obj) => parseFloat(num))
  )),
  R.assoc('hourlyRealtimeData', R.map(
    R.mapObjIndexed((num, key, obj) => parseFloat(num))
  ))
)(data);

How can I pass data variable to mapObjIndexed function? Since the mapObjIndexed is inside assoc function, the data object does not reach the mapObjIndexed function.

Cristian Velasquez Ramos
@cevr
Sep 24 2018 04:22
@m59peacemaker Thank you, I'll try and separate the logic
Baqer Mamouri
@bmamouri
Sep 24 2018 04:35
The only way that I can write that is non-functional way:
var data = {
            "homeId": 1,
            "hourlyBudget": {
                "cost": {
                    "total": "0.28",
                    "remainder": "0.28",
                },
                "energy": {
                    "total": "1115",
                    "remainder": "1115",
                },
                "emission": {
                    "total": "0.925",
                    "remainder": "0.925",
                }
            },
            "hourlyRealtimeData": {
                "cost": {
                    "absolute": "0.19",
                    "relative": "0.679"
                },
                "grossEnergyUse": {
                    "absolute": "850",
                    "relative": "0.762"
                },
                "emission": {
                    "absolute": "0.705",
                    "relative": "0.762"
                }
            }
        };


var convertToFloat = R.map(
  R.mapObjIndexed((val, key, obj) => parseFloat(val))
);

data.hourlyBudget = convertToFloat(data.hourlyBudget);
data.hourlyRealtimeData = convertToFloat(data.hourlyBudget);
Johnny Hauser
@m59peacemaker
Sep 24 2018 04:50
@cevr
const divideBy = flip(divide)
const moduloBy = flip(modulo)
const isLt = flip(lt)

const formatHours = compose(Math.floor, divideBy(60))
const formatMinutes = compose(when(isLt(10), compose(concat('0'), String)), moduloBy(60))
const formatTime = converge(unapply(join(':')), [ formatHours, formatMinutes ])
Pedro Ávila
@avilapedro
Sep 24 2018 05:29
@bmamouri
evolve ({
  hourlyBudget: map (map (parseFloat)),
  hourlyRealtimeData: map (map (parseFloat))
}) (data)
Baqer Mamouri
@bmamouri
Sep 24 2018 05:33
@avilapedro Thank you very much. Completely forget about evolve!
Pedro Ávila
@avilapedro
Sep 24 2018 05:33
:thumbsup:
Joey Figaro
@joeyfigaro
Sep 24 2018 15:42
Hey all
if I have an array of objects and want to reduce/merge them all but prefer existing values over null, what might I want?
right now I'm merging them, but it favors null for a key—and I can't specify that it should favor a direction (left/right)
Brad Compton (he/him)
@Bradcomp
Sep 24 2018 16:06
@joeyfigaro Like this? https://goo.gl/4WNh5C
Joey Figaro
@joeyfigaro
Sep 24 2018 16:14
@Bradcomp forever helping me though my brain farts. Thanks, man. :+1:
Pedro Ávila
@avilapedro
Sep 24 2018 17:31

@joeyfigaro there's another cool way of doing it without the points:

const K = curry((x, y) => x)
const KI = curry((x, y) => y)

reduce(
  mergeDeepWith(ifElse (isNil) (KI) (K)),
  {},
  incoming
)

https://goo.gl/qetX3u

K is almost the same as always, the difference is that Ramda's implementetion is not curried (in the same way it normally curries, by allowing calling (a, b) and (a) (b) ). Unfortunately, Ramda's always wouldn't work here because the function will be called like always (1, 2), and Ramda's expect it to be called like: always (1) (2)

And I'm not sure if Ramda implements KI, it is the Kite combinator.

Johnny Hauser
@m59peacemaker
Sep 24 2018 17:58
How can static land things do this?
Container.prototype.ap = function (otherContainer) {
  return otherContainer.map(this.$value);
};
The api has to be same as fantasyland, right?
This is in the staticland docs
// mytype.js

// Static Land methods

export function of(x) {
  // ...
}

export function map(fn, value) {
  // ...
}
thingB.ap couldn't call thingA.map if thingA is implemented that like that ^
Brad Compton (he/him)
@Bradcomp
Sep 24 2018 18:01
export function ap(cont1, cont2) {
  return map(cont1.$value, cont2)
}
where cont1 and cont2 are instances of Container
Johnny Hauser
@m59peacemaker
Sep 24 2018 18:14
But what if they are not both instances of Container?
Brad Compton (he/him)
@Bradcomp
Sep 24 2018 18:14
Then you shouldn't be using ap
Take a look at the rules for the ap method
Specifically - that a and b both need to be the same Apply, in this case, Container
Joey Figaro
@joeyfigaro
Sep 24 2018 18:17
@avilapedro Thanks for sharing—I haven't heard of the Kite combinator. Reading up on that. :)
Johnny Hauser
@m59peacemaker
Sep 24 2018 18:18
doh
Pedro Ávila
@avilapedro
Sep 24 2018 18:19
@joeyfigaro I learned about it at this amazing talk about Lambda Calculus https://youtu.be/3VQ382QG-y4