Skip to content

Commit 8be8cde

Browse files
committed
adding array as stream
1 parent caf3b24 commit 8be8cde

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/xs/array.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Subscription, StreamOps } from './index'
2+
3+
declare module '.' {
4+
interface S_<A> {
5+
'ArrayStream': Array<A>
6+
}
7+
}
8+
9+
declare module '../fantasy/typeclasses' {
10+
interface _<A> {
11+
'ArrayStream': Array<A>
12+
}
13+
}
14+
15+
StreamOps.prototype.empty = function() {
16+
return []
17+
}
18+
19+
StreamOps.prototype.just = function(a) {
20+
return [a]
21+
}
22+
23+
StreamOps.prototype.scan = function(f, base, fa) {
24+
return fa.scan(f, base)
25+
}
26+
27+
StreamOps.prototype.combine = function <A, C>(
28+
f: (...a: any[]) => C,
29+
...v: any[]
30+
): Array<C> {
31+
return f.call(null, v)
32+
}
33+
34+
StreamOps.prototype.filter = function <A>(f: (a: A) => boolean, fa: Array<A>): Array<A> {
35+
return fa.filter(f)
36+
}
37+
StreamOps.prototype.map = function <A, B>(f: (a: A) => B, fa: Array<A>): Array<B> {
38+
return fa.map(f)
39+
}
40+
StreamOps.prototype.flatMap = function <A, B>(f: (a: A) => Array<B>, fa: Array<A>): Array<B> {
41+
return fa.reduce((acc, a) => acc.concat(f(a)), [] as B[])
42+
}
43+
44+
class Subject<T> extends Array<T> {
45+
next(a: T) {
46+
this.push(a)
47+
}
48+
complete() {
49+
}
50+
}
51+
52+
StreamOps.prototype.subject = function <A>() {
53+
return new Subject()
54+
}
55+
56+
StreamOps.prototype.subscribe = function <A>(fa: Array<A>, next: (v: A) => void, complete?: () => void) {
57+
throw Error("you don't need to subscribe a Array, just iterate it")
58+
}
59+
60+
StreamOps.prototype.merge = function <A, B>(a: Array<A>, b: Array<B>): Array<A | B> {
61+
return (<any>a).concat(b)
62+
}
63+
64+
StreamOps.prototype.fromPromise = function(p) {
65+
if (p.then) {
66+
throw Error("You're not using real Promise aren't you, expecting Id Monad")
67+
}
68+
return [p.valueOf()]
69+
}

0 commit comments

Comments
 (0)