From d60b7570d68cb027001ed3a14c36dd95f896c600 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Wed, 25 May 2016 13:33:16 -0600 Subject: [PATCH] combinator: add S.T --- index.js | 24 ++++++++++++++++++++++-- test/T.js | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/T.js diff --git a/index.js b/index.js index cc4ed736..a39c3cd5 100644 --- a/index.js +++ b/index.js @@ -471,8 +471,8 @@ //. function. //. //. ```javascript - //. > S.A(S.inc, 1) - //. 2 + //. > S.A(S.inc, 42) + //. 43 //. //. > R.map(S.A(R.__, 100), [S.inc, Math.sqrt]) //. [101, 10] @@ -483,6 +483,25 @@ [$.Function, a, b], function(f, x) { return f(x); }); + //# T :: a -> (a -> b) -> b + //. + //. The T ([thrush][]) combinator. Takes a value and a function, and returns + //. the result of applying the function to the value. Equivalent to Haskell's + //. `(&)` function. + //. + //. ```javascript + //. > S.T(42, S.inc) + //. 43 + //. + //. > R.map(S.T(100), [S.inc, Math.sqrt]) + //. [101, 10] + //. ``` + S.T = + def('T', + {}, + [a, $.Function, b], + function(x, f) { return f(x); }); + //# C :: (a -> b -> c) -> b -> a -> c //. //. The C combinator. Takes a curried binary function and two values, and @@ -3432,3 +3451,4 @@ //. [UnaryType]: https://github.com/sanctuary-js/sanctuary-def#unarytype //. [parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt //. [sanctuary-def]: https://github.com/sanctuary-js/sanctuary-def +//. [thrush]: https://github.com/raganwald-deprecated/homoiconic/blob/master/2008-10-30/thrush.markdown diff --git a/test/T.js b/test/T.js new file mode 100644 index 00000000..f140b956 --- /dev/null +++ b/test/T.js @@ -0,0 +1,26 @@ +'use strict'; + +var R = require('ramda'); + +var eq = require('./utils').eq; +var S = require('..'); + + +describe('T', function() { + + it('is a binary function', function() { + eq(typeof S.T, 'function'); + eq(S.T.length, 2); + }); + + it('T(x, f) is equivalent to f(x)', function() { + eq(S.T(42, S.inc), 43); + eq(R.map(S.T(100), [S.inc, Math.sqrt]), [101, 10]); + }); + + it('is curried', function() { + eq(S.T(42).length, 1); + eq(S.T(42)(S.inc), 43); + }); + +});