Skip to content

Commit

Permalink
feat(operations): expose compose function!
Browse files Browse the repository at this point in the history
  • Loading branch information
Flavio Corpa committed Oct 4, 2020
1 parent 4a8c414 commit 8f7f2f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
13 changes: 4 additions & 9 deletions __tests__/functions.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { compose, curry, get, set, setIndex, toUpper } from '../src/functions'
import { curry, get, set, setIndex, toUpper } from '../src/functions'

const cubed = (num, exp) => num ** exp
const exp = curry(cubed)
const obj = { foo: 'bar' }
const arr = [1, 2, 3]

describe('Function Operators', () => {
test('curry -> should curry functions', () => {
expect(exp(5)(3)).toBe(125)
})
const cubed = (num, exp) => num ** exp
const exp = curry(cubed)

test('compose -> should compose N functions correctly', () => {
const inc = x => x + 1

expect(compose(inc, exp(5))(1)).toBe(inc(exp(5, 1)))
expect(exp(5)(3)).toBe(125)
})

test('toUpper -> should do what is expected ehem', () => {
Expand Down
12 changes: 10 additions & 2 deletions __tests__/operations.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { OpticComposeError, UnavailableOpticOperationError } from '../src/errors'
import { toUpper } from '../src/functions'
import { curry, toUpper } from '../src/functions'
import { getter } from '../src/Getter'
import { alter } from '../src/Lens'
import { collect, optic, over, path, set, view } from '../src/operations'
import { collect, compose, optic, over, path, set, view } from '../src/operations'
import { setter } from '../src/Setter'

const theme = {
Expand All @@ -27,6 +27,14 @@ const themeWithoutFontFamily = {
}

describe('Operations over Optics', () => {
test('compose -> should compose N functions correctly', () => {
const inc = x => x + 1
const cubed = (num, exp) => num ** exp
const exp = curry(cubed)

expect(compose(inc, exp(5))(1)).toBe(inc(exp(5, 1)))
})

test('view should read from a lens', () => {
const codeLens = path(['styles', 'CodeSurfer', 'code'])
const codeSurferLens = path(['styles', 'CodeSurfer'])
Expand Down
7 changes: 0 additions & 7 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
export const curry = (f, arity = f.length, ...args) =>
arity <= args.length ? f(...args) : (...argz) => curry(f, arity, ...args, ...argz)

/**
* Function composition!
*
* @param {...any} fns - Comma-separated list of functions to be composed (right -> left)
*/
export const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args)

// get : s -> {s: a} -> Maybe a
export const get = curry((key, obj) => obj[key])

Expand Down
7 changes: 7 additions & 0 deletions src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ export const collect = template =>

export const transform = getter

/**
* Function composition!
*
* @param {...any} fns - Comma-separated list of functions to be composed (right -> left)
*/
export const compose = (...fns) => args => fns.reduceRight((x, f) => f(x), args)

// OPERATIONS
// ==========

Expand Down

0 comments on commit 8f7f2f9

Please sign in to comment.