Skip to content

Commit

Permalink
flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Oct 16, 2018
1 parent 935cdc5 commit f03cf57
Show file tree
Hide file tree
Showing 5 changed files with 438 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<p align="center">
<a href="https://www.npmjs.com/package/tinyfunk"><img src="https://img.shields.io/npm/v/tinyfunk.svg" alt="npm version" style="max-width:100%;"></a>
<a href="https://www.npmjs.com/package/tinyfunk"><img src="https://img.shields.io/npm/dm/tinyfunk.svg" alt="npm downloads" style="max-width:100%;"></a>
<a href="#"><img src="https://img.shields.io/badge/gzip--size-1.49%20kB-blue.svg" alt="gzip-size" style="max-width:100%;"></a>
<a href="#"><img src="https://img.shields.io/badge/gzip--size-1.52%20kB-blue.svg" alt="gzip-size" style="max-width:100%;"></a>
<br />
<a href="https://travis-ci.org/flintinatux/tinyfunk"><img src="https://travis-ci.org/flintinatux/tinyfunk.svg?branch=master" alt="Build Status" style="max-width:100%;"></a>
<a href="https://coveralls.io/github/flintinatux/tinyfunk?branch=master"><img src="https://coveralls.io/repos/github/flintinatux/tinyfunk/badge.svg?branch=master" alt="Coverage Status" style="max-width:100%;"></a>
Expand Down Expand Up @@ -90,6 +90,7 @@ If you've lived with FP long enough, you are likely familiar with most of the fu
| `evolve` | `{ k: (v -> v) } -> { k: v } -> { k: v }` |
| `filter` | `(a -> Boolean) -> [a] -> [a]` |
| `find` | `(a -> Boolean) -> [a] -> a` |
| `flatten` | `[a] -> [b]` |
| `flip` | `(a -> b -> c) -> (b -> a -> c)` |
| `head` | `[a] -> a` |
| `identity` | `a -> a` |
Expand Down
9 changes: 9 additions & 0 deletions src.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const _partial = (f, args) =>
const _putBy = f => (acc, item) =>
assoc(f(item), item, acc)

// _smash :: ([a], a) -> [b]
const _smash = (acc, item) =>
concat(acc, flatten(item))

// length :: [a] -> Number
const length = list =>
list.length
Expand Down Expand Up @@ -357,6 +361,10 @@ const pipeP = unapply(flip(reduce(flip(then))))
// cond :: [[(a -> Boolean), (a -> b)]] -> a -> b
const cond = compose(reduceRight(thrush, unit), map(apply(ifElse)))

// flatten :: [a] -> [b]
const flatten =
when(Array.isArray, reduce(_smash, []))

// pluck :: k -> [{ k: v }] -> [v]
const pluck = curry((key, list) =>
map(prop(key), list)
Expand Down Expand Up @@ -419,6 +427,7 @@ _assign(exports, {
evolve,
filter,
find,
flatten,
flip,
head,
identity,
Expand Down
11 changes: 11 additions & 0 deletions test/flatten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { expect } = require('chai')

const { flatten } = require('..')

describe('flatten', () => {
const arr = [ 1, [ 2, 3 ], [ 4, [ 5 ] ] ]

it('recursively flattens an array', () =>
expect(flatten(arr)).to.eql([ 1, 2, 3, 4, 5 ])
)
})
2 changes: 1 addition & 1 deletion tinyfunk.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f03cf57

Please sign in to comment.