fix ap
This commit is contained in:
parent
aace9609e3
commit
fe861926ce
3 changed files with 21 additions and 23 deletions
|
|
@ -214,15 +214,13 @@ Argument | Type | Required | Description
|
||||||
|
|
||||||
##### stream["fantasy-land/ap"]
|
##### 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.
|
`stream = m.prop()["fantasy-land/ap"](apply)`
|
||||||
|
|
||||||
`errorStream = m.prop()["fantasy-land/ap"](value)`
|
|
||||||
|
|
||||||
Argument | Type | Required | Description
|
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
|
**returns** | `Stream` | | Returns a stream
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ module.exports = function(log) {
|
||||||
end: {get: function() {
|
end: {get: function() {
|
||||||
if (!stream._state.endStream) {
|
if (!stream._state.endStream) {
|
||||||
var endStream = createStream()
|
var endStream = createStream()
|
||||||
endStream.map(function(value) {
|
endStream["fantasy-land/map"](function(value) {
|
||||||
if (value === true) unregisterStream(stream), unregisterStream(endStream)
|
if (value === true) unregisterStream(stream), unregisterStream(endStream)
|
||||||
return value
|
return value
|
||||||
})
|
})
|
||||||
|
|
@ -130,7 +130,7 @@ module.exports = function(log) {
|
||||||
updateState(stream, absorbable._state.value, absorbable._state.error)
|
updateState(stream, absorbable._state.value, absorbable._state.error)
|
||||||
for (var id in stream._state.deps) updateDependency(stream._state.deps[id], false)
|
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()
|
update()
|
||||||
throw {__error: e}
|
throw {__error: e}
|
||||||
})
|
})
|
||||||
|
|
@ -174,7 +174,7 @@ module.exports = function(log) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function map(fn) {return combine(function(stream) {return fn(stream())}, [this])}
|
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 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}
|
function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -347,7 +347,7 @@ o.spec("stream", function() {
|
||||||
o(mapped.error().message).equals("error")
|
o(mapped.error().message).equals("error")
|
||||||
o(count).equals(0)
|
o(count).equals(0)
|
||||||
})
|
})
|
||||||
o("error["fantasy-land/map"] works", function() {
|
o("error[fl.map] works", function() {
|
||||||
var stream = Stream(1)
|
var stream = Stream(1)
|
||||||
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
||||||
if (value) return "from" + value.message
|
if (value) return "from" + value.message
|
||||||
|
|
@ -359,7 +359,7 @@ o.spec("stream", function() {
|
||||||
|
|
||||||
o(mappedFromError()).equals("fromerror")
|
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 stream = Stream(1)
|
||||||
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
||||||
return "from" + value.message
|
return "from" + value.message
|
||||||
|
|
@ -374,7 +374,7 @@ o.spec("stream", function() {
|
||||||
|
|
||||||
o(mappedFromError()).equals("afromerror")
|
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 count = 0
|
||||||
var stream = Stream(1)
|
var stream = Stream(1)
|
||||||
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
var mappedFromError = stream.error["fantasy-land/map"](function(value) {
|
||||||
|
|
@ -408,7 +408,7 @@ o.spec("stream", function() {
|
||||||
o(stream()).equals(undefined)
|
o(stream()).equals(undefined)
|
||||||
o(count).equals(0)
|
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 stream = Stream.reject(new Error("error"))
|
||||||
var error = stream.error["fantasy-land/map"](function(value) {
|
var error = stream.error["fantasy-land/map"](function(value) {
|
||||||
return Stream(1)
|
return Stream(1)
|
||||||
|
|
@ -923,7 +923,7 @@ o.spec("stream", function() {
|
||||||
o("works", function() {
|
o("works", function() {
|
||||||
var apply = Stream(function(value) {return value * 2})
|
var apply = Stream(function(value) {return value * 2})
|
||||||
var stream = Stream(3)
|
var stream = Stream(3)
|
||||||
var applied = apply["fantasy-land/ap"](stream)
|
var applied = stream["fantasy-land/ap"](apply)
|
||||||
|
|
||||||
o(applied()).equals(6)
|
o(applied()).equals(6)
|
||||||
|
|
||||||
|
|
@ -938,7 +938,7 @@ o.spec("stream", function() {
|
||||||
o("works with undefined value", function() {
|
o("works with undefined value", function() {
|
||||||
var apply = Stream(function(value) {return String(value)})
|
var apply = Stream(function(value) {return String(value)})
|
||||||
var stream = Stream(undefined)
|
var stream = Stream(undefined)
|
||||||
var applied = apply["fantasy-land/ap"](stream)
|
var applied = stream["fantasy-land/ap"](apply)
|
||||||
|
|
||||||
o(applied()).equals("undefined")
|
o(applied()).equals("undefined")
|
||||||
|
|
||||||
|
|
@ -974,15 +974,15 @@ o.spec("stream", function() {
|
||||||
var u = Stream(function(value) {return value * 3})
|
var u = Stream(function(value) {return value * 3})
|
||||||
var v = Stream(5)
|
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(g) {
|
||||||
return function(x) {
|
return function(x) {
|
||||||
return f(g(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(30)
|
||||||
o(mapped()).equals(composed())
|
o(mapped()).equals(composed())
|
||||||
|
|
@ -993,24 +993,24 @@ o.spec("stream", function() {
|
||||||
var a = Stream()["fantasy-land/of"](function(value) {return value})
|
var a = Stream()["fantasy-land/of"](function(value) {return value})
|
||||||
var v = Stream(5)
|
var v = Stream(5)
|
||||||
|
|
||||||
o(a["fantasy-land/ap"](v)()).equals(5)
|
o(v["fantasy-land/ap"](a)()).equals(5)
|
||||||
o(a["fantasy-land/ap"](v)()).equals(v())
|
o(v["fantasy-land/ap"](a)()).equals(v())
|
||||||
})
|
})
|
||||||
o("homomorphism", function() {
|
o("homomorphism", function() {
|
||||||
var a = Stream(0)
|
var a = Stream(0)
|
||||||
var f = function(value) {return value * 2}
|
var f = function(value) {return value * 2}
|
||||||
var x = 3
|
var x = 3
|
||||||
|
|
||||||
o(a["fantasy-land/of"](f)["fantasy-land/ap"](a["fantasy-land/of"](x))()).equals(6)
|
o(a["fantasy-land/of"](x)["fantasy-land/ap"](a["fantasy-land/of"](f))()).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(a["fantasy-land/of"](f(x))())
|
||||||
})
|
})
|
||||||
o("interchange", function() {
|
o("interchange", function() {
|
||||||
var u = Stream(function(value) {return value * 2})
|
var u = Stream(function(value) {return value * 2})
|
||||||
var a = Stream()
|
var a = Stream()
|
||||||
var y = 3
|
var y = 3
|
||||||
|
|
||||||
o(u["fantasy-land/ap"](a["fantasy-land/of"](y))()).equals(6)
|
o(a["fantasy-land/of"](y)["fantasy-land/ap"](u)()).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(u["fantasy-land/ap"](a["fantasy-land/of"](function(f) {return f(y)}))())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue