Skip to content

Commit

Permalink
nAry, take, unary
Browse files Browse the repository at this point in the history
  • Loading branch information
flintinatux committed Sep 18, 2018
1 parent 3f83307 commit 93802d2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 2 deletions.
5 changes: 4 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.36%20kB-blue.svg" alt="gzip-size" style="max-width:100%;"></a>
<a href="#"><img src="https://img.shields.io/badge/gzip--size-1.4%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 @@ -105,6 +105,7 @@ If you've lived with FP long enough, you are likely familiar with most of the fu
| `match` | `RegExp -> String -> [String]` |
| `merge` | `{ k: v } -> { k: v } -> { k: v }` |
| `multiply` | `Number -> Number -> Number` |
| `nAry` | `Number -> (a... -> b) -> (a... -> b)` |
| `not` | `a -> a` |
| `objOf` | `k -> v -> { k: v }` |
| `omit` | `[k] -> { k: v } -> { k: v }` |
Expand All @@ -128,10 +129,12 @@ If you've lived with FP long enough, you are likely familiar with most of the fu
| `sortBy` | `Ord b => (a -> b) -> [a] -> [a]` |
| `split` | `RegExp -> String -> [String]` |
| `tail` | `[a] -> [a]` |
| `take` | `Number -> [a] -> [a]` |
| `tap` | `(a -> b) -> a -> a` |
| `then` | `(a -> Promise b) -> a -> Promise b` |
| `thrush` | `a -> (a -> b) -> b` |
| `unapply` | `([a] -> b) -> a... -> b` |
| `unary` | `(a... -> b) -> (a -> b)` |
| `unit` | `a -> ()` |
| `unless` | `(a -> Boolean) -> (a -> a) -> a -> a` |
| `useWith` | `(b... -> c) -> [(a -> b)] -> a... -> c` |
Expand Down
14 changes: 14 additions & 0 deletions src.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ const multiply = curry((a, b) =>
a * b
)

// nAry :: Number -> (a... -> b) -> (a... -> b)
const nAry = curry((n, f) =>
unapply(compose(apply(f), take(n)))
)

// not :: a -> a
const not = a => !a

Expand Down Expand Up @@ -290,6 +295,9 @@ const thrush = curry((x, f) =>
f(x)
)

// unary :: (a... -> b) -> (a -> b)
const unary = nAry(1)

// unit :: a -> ()
const unit = () => {}

Expand Down Expand Up @@ -347,6 +355,9 @@ const last = compose(head, slice(-1, void 0))
// tail :: [a] -> [a]
const tail = slice(1, Infinity)

// take :: Number -> [a] -> [a]
const take = slice(0)

// keys :: { k: v } -> [k]
const keys = reduceObj(_appendKey, [])

Expand Down Expand Up @@ -389,6 +400,7 @@ _assign(exports, {
match,
merge,
multiply,
nAry,
not,
objOf,
omit,
Expand All @@ -412,10 +424,12 @@ _assign(exports, {
sortBy,
split,
tail,
take,
tap,
then,
thrush,
unapply,
unary,
unit,
unless,
useWith,
Expand Down
19 changes: 19 additions & 0 deletions test/nAry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { expect } = require('chai')

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

describe('nAry', () => {
const f = (a=0, b=0, c=0, d=0) =>
a + b + c + d

it('limits the args passed to the wrapped function', () => {
expect(nAry(0, f)(1, 1, 1, 1)).to.equal(0)
expect(nAry(1, f)(1, 1, 1, 1)).to.equal(1)
expect(nAry(2, f)(1, 1, 1, 1)).to.equal(2)
expect(nAry(3, f)(1, 1, 1, 1)).to.equal(3)
})

it('is curried', () =>
expect(nAry(1)(f)(1, 1, 1, 1)).to.equal(1)
)
})
20 changes: 20 additions & 0 deletions test/take.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { expect } = require('chai')

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

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

it('takes the first n items in a list', () => {
expect(take(0, arr)).to.eql([])
expect(take(1, arr)).to.eql([1])
expect(take(2, arr)).to.eql([1, 2])
expect(take(3, arr)).to.eql([1, 2, 3])
expect(take(4, arr)).to.eql([1, 2, 3, 4])
expect(take(5, arr)).to.eql([1, 2, 3, 4])
})

it('is curried', () =>
expect(take(1)(arr)).to.eql([1])
)
})
15 changes: 15 additions & 0 deletions test/unary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { expect } = require('chai')

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

describe('unary', () => {
const f = unary((a=0, b=0) =>
a + b
)

it('only passes one arg to wrapped function', () => {
expect(f()).to.equal(0)
expect(f(1)).to.equal(1)
expect(f(1, 2)).to.equal(1)
})
})
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.

0 comments on commit 93802d2

Please sign in to comment.