Last active
June 3, 2018 10:39
-
-
Save WaldoJeffers/f2638e8c872eaa57511827c26940d622 to your computer and use it in GitHub Desktop.
Get homeworld stats about Star Wars characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { compose, get, mergeWith, reduce } from 'conductor' | |
const character_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
const fetchJSON = url => fetch(url).then(res => res.json()) | |
const fetchCharacter = id => fetchJSON(`https://swapi.co/api/people/${id}`) | |
const getHomeworldName = compose(get('name'), fetchJSON, get('homeworld'), fetchCharacter) | |
const toObj = (key, value) => ({ [key]: value }) | |
const countOne = name => toObj(name, 1) | |
const countHomeworld = compose(countOne, getHomeworldName) | |
const mergeByAdding = mergeWith(add) | |
const reducer = (acc, character_id) => getHomeworldName(character_id) | |
.then(mergeByAdding(acc)) | |
const homeworld_stats = reduce(reducer, character_ids) | |
// { Alderaan: 1, Naboo: 1, Stewjon: 1, Tatooine: 7 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment