Skip to content

Instantly share code, notes, and snippets.

@jem-computer
Created November 12, 2015 01:45
Show Gist options
  • Save jem-computer/58db333dbd39083ccb4f to your computer and use it in GitHub Desktop.
Save jem-computer/58db333dbd39083ccb4f to your computer and use it in GitHub Desktop.
import { curry, equals, groupBy, head, mapObj,
mapObjIndexed, prop, reject, tap } from 'ramda'
/**
* takes a function and two objects
* merges them by applying fn to
* the same k/v in each.
*
* @func
* @sig (a,a -> a) -> {k: v} -> {k: v} -> {k: v}
* @param {Function} fn A function used to merge items
* @param {Object} obj1 The first object
* @param {Object} obj2 The first object
* @return {Object} The result of applying fn to each val in obj1 & obj2
* @example
*
* const fn = R.add
* const obj1 = { foo: 1, bar: 10, baz: 5 }
* const obj2 = { foo: 8, bar: 20, baz: 7 }
* mergeWith(fn, obj1, obj2)
* //=> { foo: 9, bar: 30, baz: 12 }
*/
export const mergeWith = curry((fn, a, b) => {
return mapObjIndexed((va, k) => {
const vb = prop(k, b)
return fn(va, vb)
}, a)
})
// List
// (a -> String) -> [a] -> { String: [a] }
export const indexBy = curry((_prop, list) => {
return mapObj(head, groupBy(prop(_prop), list))
})
// String -> [String] -> [String]
export const without = curry((a, b) => reject(equals(a), b))
export const log = tap(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment