Skip to content

Commit

Permalink
Merge pull request #148 from calmm-js/feature/flat
Browse files Browse the repository at this point in the history
Added `flat`
  • Loading branch information
polytypic authored Feb 13, 2018
2 parents f8680b5 + c3ef94d commit 29e94ce
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ parts. [Try Lenses!](https://calmm-js.github.io/partial.lenses/playground.html)
* [`L.traverse(algebra, (maybeValue, index) => operation, optic, maybeData) ~> operation`](#L-traverse "L.traverse: (Functor|Applicative|Monad) c -> ((Maybe a, Index) -> c b) -> POptic s t a b -> Maybe s -> c t") <small><sup>v10.0.0</sup></small>
* [Nesting](#nesting)
* [`L.compose(...optics) ~> optic`](#L-compose "L.compose: (POptic s s1, ...POptic sN a) -> POptic s a") or `[...optics]` <small><sup>v1.0.0</sup></small>
* [`L.flat(...optics) ~> optic`](#L-flat "L.flat: (POptic s [...s1...], ...POptic sN [...a...]) -> POptic [...s...] a") <small><sup>v13.6.0</sup></small>
* [Recursing](#recursing)
* [`L.lazy(optic => optic) ~> optic`](#L-lazy "L.lazy: (POptic s a -> POptic s a) -> POptic s a") <small><sup>v5.1.0</sup></small>
* [Adapting](#adapting)
Expand Down Expand Up @@ -1169,6 +1170,13 @@ first two.
Note that [`R.compose`](http://ramdajs.com/docs/#compose) is not the same as
`L.compose`.

##### <a id="L-flat"></a> [](#contents) [](https://calmm-js.github.io/partial.lenses/index.html#L-flat) [`L.flat(...optics) ~> optic`](#L-flat "L.flat: (POptic s [...s1...], ...POptic sN [...a...]) -> POptic [...s...] a") <small><sup>v13.6.0</sup></small>

`L.flat` is like [`L.compose`](#L-compose) except that [`L.flatten`](#L-flatten)
is composed around and between the given optics. In other words, `L.flat(o1,
..., oN)` is equivalent to `L.compose(L.flatten, o1, L.flatten, ..., L.flatten,
oN, L.flatten)`.

#### <a id="recursing"></a> [](#contents) [](https://calmm-js.github.io/partial.lenses/index.html#recursing) [Recursing](#recursing)

The [`L.lazy`](#L-lazy) combinator allows one to build optics that deal with
Expand Down
7 changes: 7 additions & 0 deletions src/partial.lenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,13 @@ export function compose() {
}
}

export function flat() {
const r = [flatten]
for (let i = 0, n = arguments.length; i < n; ++i)
r.push(arguments[i], flatten)
return r
}

// Recursing

export function lazy(o2o) {
Expand Down
16 changes: 16 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ describe('arities', () => {
find: 1,
findWith: 1,
first: undefined,
flat: 0,
flatten: 4,
foldTraversalLens: 2,
foldl: 4,
Expand Down Expand Up @@ -1705,6 +1706,21 @@ describe('L.pointer', () => {
})
})

describe('L.flat', () => {
testEq(
`L.modify(
L.flat(
'a',
'b',
'c'
),
R.negate,
[{a: [[{b: {c: 1}}], [{b: [{c: 2}]}]]}, {a: {b: {c: [[[3]]]}}}]
)`,
[{a: [[{b: {c: -1}}], [{b: [{c: -2}]}]]}, {a: {b: {c: [[[-3]]]}}}]
)
})

if (process.env.NODE_ENV !== 'production') {
describe('debug', () => {
testThrows(`X.set(-1, 0, 0)`)
Expand Down
1 change: 1 addition & 0 deletions test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export const traverse = T.fn(
// Nesting

export const compose = T.fnVarN(0, T_optic, T_optic)
export const flat = T.fnVarN(0, T_optic, T_optic)

// Recursing

Expand Down

0 comments on commit 29e94ce

Please sign in to comment.