These are chat archives for ramda/ramda

15th
Dec 2016
Matt McKellar-Spence
@MattMS
Dec 15 2016 13:29
@sdtsui would applySpec be useful to you?
Tyan
@fwchen
Dec 15 2016 13:58
which Maybe libaray in document used?
Bravi
@Bravilogy
Dec 15 2016 16:51
Hi guys. I'm working on a presentation on declarative / functional programming compared to imperative. So I've got a few examples I came up with using imperative and converted them to declarative instead. So here's one of them and just wanted to get an idea if this qualifies as functional programming example? (ramda is in mixture in there btw)
import request from 'reqwest';
import Task from 'data.task';
import {path, curry, lte, _, filter, pipe, indexOf, prop} from 'ramda';

/* ------------- Imperative ------------ */
const getCompanyNames = () => {
    request('https://jsonplaceholder.typicode.com/users', (users) => {
        var companies = [],
            i = 0;

        for(i; i < users.length; i++) {
            var user = users[i];

            if (user.website.indexOf('info') >= 0) {
                companies.push(user.company.name);
            }
        }

        console.log(companies);
    });
};

getCompanyNames();
/* ------------- End Imperative ------------ */



/* ------------- Declarative ------------ */
const propContains = (key, needle) =>
    pipe(prop(key), indexOf(needle), lte(0));

const getJson = url =>
    new Task((reject, resolve) => request(url, resolve));

const filteredUsers = getJson('https://jsonplaceholder.typicode.com/users')
    .map(users => users
        .filter(propContains('website', 'info'))
        .map(path(['company', 'name'])));

filteredUsers.fork(console.error, console.log);
/* ------------- End Declarative ------------ */