Skip to content

Commit

Permalink
provide a placeholder value
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Sep 26, 2016
1 parent 3433839 commit d18df6e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
44 changes: 20 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,16 @@ inc(7);
One may wish to partially apply a function whose parameters are in the "wrong"
order. All functions defined via sanctuary-def accommodate this by accepting
"placeholders". A placeholder is an object with a `'@@functional/placeholder'`
property whose value is `true`. [`R.__`][1] is one such object. A placeholder
property whose value is `true`. `$.__` is one such object. A placeholder
indicates an argument yet to be provided. For example:

```javascript
// _ :: Placeholder
const _ = {'@@functional/placeholder': true};

// concatS :: String -> String -> String
const concatS =
def('concatS', {}, [$.String, $.String, $.String], (x, y) => x + y);

// exclaim :: String -> String
const exclaim = concatS(_, '!');
const exclaim = concatS($.__, '!');

exclaim('ahoy');
// => 'ahoy!'
Expand Down Expand Up @@ -179,7 +176,7 @@ Type comprising every Function value.
$.Arguments :: Type
```

Type comprising every [`arguments`][2] object.
Type comprising every [`arguments`][1] object.

#### `Array`

Expand Down Expand Up @@ -212,7 +209,7 @@ $.Error :: Type
```

Type comprising every Error value, including values of more specific
constructors such as [`SyntaxError`][3] and [`TypeError`][4].
constructors such as [`SyntaxError`][2] and [`TypeError`][3].

#### `FiniteNumber`

Expand Down Expand Up @@ -243,7 +240,7 @@ $.Integer :: Type
```

Type comprising every integer in the range
[[`Number.MIN_SAFE_INTEGER`][5] .. [`Number.MAX_SAFE_INTEGER`][6]].
[[`Number.MIN_SAFE_INTEGER`][4] .. [`Number.MAX_SAFE_INTEGER`][5]].

#### `NegativeFiniteNumber`

Expand Down Expand Up @@ -328,7 +325,7 @@ $.Object :: Type
Type comprising every "plain" Object value. Specifically, values created via:

- object literal syntax;
- [`Object.create`][7]; or
- [`Object.create`][6]; or
- the `new` operator in conjunction with `Object` or a custom
constructor function.

Expand Down Expand Up @@ -731,11 +728,11 @@ showCard(Pair('X', '♠'));

#### `EnumType`

`EnumType` is used to construct [enumerated types][8].
`EnumType` is used to construct [enumerated types][7].

To define an enumerated type one must provide:

- an array of values with distinct [`R.toString`][9] representations.
- an array of values with distinct [`R.toString`][8] representations.

```haskell
EnumType :: Array Any -> Type
Expand Down Expand Up @@ -973,8 +970,8 @@ _concat([1, 2], 'buzz');

The type of `_concat` is misleading: it suggests that it can operate on any
two values of *any* one type. In fact there's an implicit constraint, since
the type must support concatenation (in [mathematical][10] terms, the type
must have a [semigroup][11]). The run-time type errors that result when this
the type must support concatenation (in [mathematical][9] terms, the type
must have a [semigroup][10]). The run-time type errors that result when this
constraint is violated are not particularly descriptive:

```javascript
Expand Down Expand Up @@ -1029,14 +1026,13 @@ Multiple constraints may be placed on a type variable by including multiple
`TypeClass` values in the list (e.g. `{a: [Foo, Bar, Baz]}`).


[1]: http://ramdajs.com/docs/#__
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
[4]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
[5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER
[6]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
[7]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
[8]: https://en.wikipedia.org/wiki/Enumerated_type
[9]: http://ramdajs.com/docs/#toString
[10]: https://en.wikipedia.org/wiki/Semigroup
[11]: https://github.com/fantasyland/fantasy-land#semigroup
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
[2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError
[3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError
[4]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER
[5]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
[6]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
[7]: https://en.wikipedia.org/wiki/Enumerated_type
[8]: http://ramdajs.com/docs/#toString
[9]: https://en.wikipedia.org/wiki/Semigroup
[10]: https://github.com/fantasyland/fantasy-land#semigroup
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

'use strict';

var $ = {};
var $ = {__: {'@@functional/placeholder': true}};

var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
var MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER;
Expand Down
20 changes: 10 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ describe('def', function() {
var triple =
def('triple', {}, [$.Number, $.Number, $.Number, $.Array($.Number)], list);

eq(triple(R.__, R.__, 3)(R.__, 2)(1), [1, 2, 3]);
eq(triple($.__, $.__, 3)($.__, 2)(1), [1, 2, 3]);

throws(function() { triple(R.__, /x/); },
throws(function() { triple($.__, /x/); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -362,7 +362,7 @@ describe('def', function() {
'\n' +
'The value at position 1 is not a member of ‘Number’.\n'));

throws(function() { triple(R.__, R.__, /x/); },
throws(function() { triple($.__, $.__, /x/); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -374,7 +374,7 @@ describe('def', function() {
'\n' +
'The value at position 1 is not a member of ‘Number’.\n'));

throws(function() { triple(R.__, 2, 3)(/x/); },
throws(function() { triple($.__, 2, 3)(/x/); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand Down Expand Up @@ -584,14 +584,14 @@ describe('def', function() {
var a000 = def('a00', {}, [a, a, a, $.Array(a)], Array);
var anum = a000(1);
var astr = a000('a');
var bstr = a000(R.__, 'b');
var bstr = a000($.__, 'b');
var abstr = astr('b');

eq(anum(2, 3), [1, 2, 3]);
eq(anum(2)(3), [1, 2, 3]);
eq(astr('b', 'c'), ['a', 'b', 'c']);
eq(bstr('a', 'c'), ['a', 'b', 'c']);
eq(astr(R.__, 'c')('b'), ['a', 'b', 'c']);
eq(astr($.__, 'c')('b'), ['a', 'b', 'c']);
eq(abstr('c'), ['a', 'b', 'c']);
});

Expand Down Expand Up @@ -1432,7 +1432,7 @@ describe('def', function() {
def('values',
{},
[$.StrMap(a), $.Array(a)],
function(m) { return R.map(R.prop(R.__, m), keys(m)); });
function(m) { return R.map(R.prop($.__, m), keys(m)); });

var o = Object.create(null);
o.x = 1;
Expand Down Expand Up @@ -1622,7 +1622,7 @@ describe('def', function() {
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));

throws(function() { aa(R.__, 0)(/x/); },
throws(function() { aa($.__, 0)(/x/); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
Expand Down Expand Up @@ -2254,7 +2254,7 @@ describe('def', function() {
'\n' +
'‘or’ requires ‘a’ to satisfy the Alternative type-class constraint; the value at position 1 does not.\n'));

throws(function() { or(R.__, Right(1)); },
throws(function() { or($.__, Right(1)); },
errorEq(TypeError,
'Type-class constraint violation\n' +
'\n' +
Expand Down Expand Up @@ -2290,7 +2290,7 @@ describe('def', function() {
'\n' +
'‘concat’ requires ‘a’ to satisfy the Semigroup type-class constraint; the value at position 1 does not.\n'));

throws(function() { concat(R.__, /x/); },
throws(function() { concat($.__, /x/); },
errorEq(TypeError,
'Type-class constraint violation\n' +
'\n' +
Expand Down

0 comments on commit d18df6e

Please sign in to comment.