From fe861926ce01acbf590d946f336e96dc0e424d55 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Wed, 26 Oct 2016 18:37:48 -0400 Subject: [PATCH] fix ap --- docs/prop.md | 8 +++----- util/stream.js | 6 +++--- util/tests/test-stream.js | 30 +++++++++++++++--------------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/docs/prop.md b/docs/prop.md index 3a898ed9..ebf01942 100644 --- a/docs/prop.md +++ b/docs/prop.md @@ -214,15 +214,13 @@ Argument | Type | Required | Description ##### stream["fantasy-land/ap"] -[FIXME: `ap` does not conform to Fantasy Land 2.0 spec - do not use] +The name of this method stands for `apply`. If a stream `a` has a function as its value, another stream `b` can use it as the argument to `b.ap(a)`. Calling `ap` will call the function with the value of stream `b` as its argument, and it will return another stream whose value is the result of the function call. This method exists to conform to [Fantasy Land's Applicative specification](https://github.com/fantasyland/fantasy-land). For more information, see the [What is Fantasy Land](#what-is-fantasy-land) section. -The name of this method stands for `apply`. If a stream has a function as its value, calling `ap` will call the function with the value of the input stream as its argument, and it will return another stream whose value is the result of the function call. This method exists to conform to [Fantasy Land's Applicative specification](https://github.com/fantasyland/fantasy-land). For more information, see the [What is Fantasy Land](#what-is-fantasy-land) section. - -`errorStream = m.prop()["fantasy-land/ap"](value)` +`stream = m.prop()["fantasy-land/ap"](apply)` Argument | Type | Required | Description ----------- | -------------------- | -------- | --- -`value` | `Stream` | Yes | If this argument is present, the value of the prop is set to it +`apply` | `Stream` | Yes | A stream whose value is a function **returns** | `Stream` | | Returns a stream --- diff --git a/util/stream.js b/util/stream.js index 5bcaee33..9e8e8102 100644 --- a/util/stream.js +++ b/util/stream.js @@ -36,7 +36,7 @@ module.exports = function(log) { end: {get: function() { if (!stream._state.endStream) { var endStream = createStream() - endStream.map(function(value) { + endStream["fantasy-land/map"](function(value) { if (value === true) unregisterStream(stream), unregisterStream(endStream) return value }) @@ -130,7 +130,7 @@ module.exports = function(log) { updateState(stream, absorbable._state.value, absorbable._state.error) for (var id in stream._state.deps) updateDependency(stream._state.deps[id], false) } - absorbable.map(update).catch(function(e) { + absorbable["fantasy-land/map"](update).catch(function(e) { update() throw {__error: e} }) @@ -174,7 +174,7 @@ module.exports = function(log) { } function map(fn) {return combine(function(stream) {return fn(stream())}, [this])} - function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [this, stream])} + function ap(stream) {return combine(function(s1, s2) {return s1()(s2())}, [stream, this])} function valueOf() {return this._state.value} function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value} diff --git a/util/tests/test-stream.js b/util/tests/test-stream.js index 945a34cf..b6b0bedd 100644 --- a/util/tests/test-stream.js +++ b/util/tests/test-stream.js @@ -347,7 +347,7 @@ o.spec("stream", function() { o(mapped.error().message).equals("error") o(count).equals(0) }) - o("error["fantasy-land/map"] works", function() { + o("error[fl.map] works", function() { var stream = Stream(1) var mappedFromError = stream.error["fantasy-land/map"](function(value) { if (value) return "from" + value.message @@ -359,7 +359,7 @@ o.spec("stream", function() { o(mappedFromError()).equals("fromerror") }) - o("error from error["fantasy-land/map"] propagates", function() { + o("error from error[fl.map] propagates", function() { var stream = Stream(1) var mappedFromError = stream.error["fantasy-land/map"](function(value) { return "from" + value.message @@ -374,7 +374,7 @@ o.spec("stream", function() { o(mappedFromError()).equals("afromerror") }) - o("error thrown from error["fantasy-land/map"] propagates downstream", function() { + o("error thrown from error[fl.map] propagates downstream", function() { var count = 0 var stream = Stream(1) var mappedFromError = stream.error["fantasy-land/map"](function(value) { @@ -408,7 +408,7 @@ o.spec("stream", function() { o(stream()).equals(undefined) o(count).equals(0) }) - o("error["fantasy-land/map"] can return streams", function() { + o("error[fl.map] can return streams", function() { var stream = Stream.reject(new Error("error")) var error = stream.error["fantasy-land/map"](function(value) { return Stream(1) @@ -923,7 +923,7 @@ o.spec("stream", function() { o("works", function() { var apply = Stream(function(value) {return value * 2}) var stream = Stream(3) - var applied = apply["fantasy-land/ap"](stream) + var applied = stream["fantasy-land/ap"](apply) o(applied()).equals(6) @@ -938,7 +938,7 @@ o.spec("stream", function() { o("works with undefined value", function() { var apply = Stream(function(value) {return String(value)}) var stream = Stream(undefined) - var applied = apply["fantasy-land/ap"](stream) + var applied = stream["fantasy-land/ap"](apply) o(applied()).equals("undefined") @@ -974,15 +974,15 @@ o.spec("stream", function() { var u = Stream(function(value) {return value * 3}) var v = Stream(5) - var mapped = a["fantasy-land/map"](function(f) { + var mapped = v["fantasy-land/ap"](u["fantasy-land/ap"](a["fantasy-land/map"](function(f) { return function(g) { return function(x) { return f(g(x)) } } - })["fantasy-land/ap"](u)["fantasy-land/ap"](v) + }))) - var composed = a["fantasy-land/ap"](u["fantasy-land/ap"](v)) + var composed = v["fantasy-land/ap"](u)["fantasy-land/ap"](a) o(mapped()).equals(30) o(mapped()).equals(composed()) @@ -993,24 +993,24 @@ o.spec("stream", function() { var a = Stream()["fantasy-land/of"](function(value) {return value}) var v = Stream(5) - o(a["fantasy-land/ap"](v)()).equals(5) - o(a["fantasy-land/ap"](v)()).equals(v()) + o(v["fantasy-land/ap"](a)()).equals(5) + o(v["fantasy-land/ap"](a)()).equals(v()) }) o("homomorphism", function() { var a = Stream(0) var f = function(value) {return value * 2} var x = 3 - o(a["fantasy-land/of"](f)["fantasy-land/ap"](a["fantasy-land/of"](x))()).equals(6) - o(a["fantasy-land/of"](f)["fantasy-land/ap"](a["fantasy-land/of"](x))()).equals(a["fantasy-land/of"](f(x))()) + o(a["fantasy-land/of"](x)["fantasy-land/ap"](a["fantasy-land/of"](f))()).equals(6) + o(a["fantasy-land/of"](x)["fantasy-land/ap"](a["fantasy-land/of"](f))()).equals(a["fantasy-land/of"](f(x))()) }) o("interchange", function() { var u = Stream(function(value) {return value * 2}) var a = Stream() var y = 3 - o(u["fantasy-land/ap"](a["fantasy-land/of"](y))()).equals(6) - o(u["fantasy-land/ap"](a["fantasy-land/of"](y))()).equals(a["fantasy-land/of"](function(f) {return f(y)})["fantasy-land/ap"](u)()) + o(a["fantasy-land/of"](y)["fantasy-land/ap"](u)()).equals(6) + o(a["fantasy-land/of"](y)["fantasy-land/ap"](u)()).equals(u["fantasy-land/ap"](a["fantasy-land/of"](function(f) {return f(y)}))()) }) }) })