From 987fc89631a21c2f548dfd3876b76da699bf608a Mon Sep 17 00:00:00 2001 From: David Chambers Date: Wed, 13 Mar 2019 23:13:32 +0100 Subject: [PATCH] pair: add S.pair --- index.js | 25 ++++++++++++++++++++++--- test/pair~.js | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test/pair~.js diff --git a/index.js b/index.js index f4f1675c..8e370d55 100644 --- a/index.js +++ b/index.js @@ -1992,6 +1992,25 @@ impl: Pair }; + //# pair :: (a -> b -> c) -> Pair a b -> c + //. + //. Case analysis for the `Pair a b` type. + //. + //. ```javascript + //. > S.pair (S.concat) (S.Pair ('foo') ('bar')) + //. 'foobar' + //. ``` + function pair(f) { + return function(pair) { + return f (pair.fst) (pair.snd); + }; + } + _.pair = { + consts: {}, + types: [Fn (a) (Fn (b) (c)), $Pair (a) (b), c], + impl: pair + }; + //# fst :: Pair a b -> a //. //. `fst (Pair (x) (y))` is equivalent to `x`. @@ -2003,7 +2022,7 @@ _.fst = { consts: {}, types: [$Pair (a) (b), a], - impl: Pair.fst + impl: pair (K) }; //# snd :: Pair a b -> b @@ -2017,7 +2036,7 @@ _.snd = { consts: {}, types: [$Pair (a) (b), b], - impl: Pair.snd + impl: pair (C (K)) }; //# swap :: Pair a b -> Pair b a @@ -2031,7 +2050,7 @@ _.swap = { consts: {}, types: [$Pair (a) (b), $Pair (b) (a)], - impl: Pair.swap + impl: pair (C (Pair)) }; //. ### Maybe type diff --git a/test/pair~.js b/test/pair~.js new file mode 100644 index 00000000..366a7593 --- /dev/null +++ b/test/pair~.js @@ -0,0 +1,16 @@ +'use strict'; + +const S = require ('..'); + +const eq = require ('./internal/eq'); + + +test ('pair', () => { + + eq (typeof S.pair) ('function'); + eq (S.pair.length) (1); + eq (S.show (S.pair)) ('pair :: (a -> b -> c) -> Pair a b -> c'); + + eq (S.pair (S.concat) (S.Pair ('foo') ('bar'))) ('foobar'); + +});