Skip to content

Commit 154bf42

Browse files
author
1399852153
committed
java实现 流
1 parent 42e8101 commit 154bf42

11 files changed

Lines changed: 165 additions & 49 deletions

File tree

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen

src/java/Test.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/js/stream-opt.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// stream map 操作
2-
function map_stream(mapper,stream){
2+
function map_stream(map,stream){
33
if(is_empty_stream(stream)){
44
return empty_stream();
55
}
66

77
return cons_stream(
8-
mapper(head(stream)),
8+
map(head(stream)),
99
function(){
10-
return map_stream(mapper,tail(stream));
10+
return map_stream(map,tail(stream));
1111
}
1212
);
1313
}
@@ -75,9 +75,9 @@ function flatten_stream(stream_in_stream){
7575
}
7676

7777
// stream flattenMap
78-
function flatten_map_stream(mapper,stream_in_stream){
78+
function flatten_map_stream(map,stream_in_stream){
7979
return flatten_stream(
80-
map_stream(mapper,stream_in_stream)
80+
map_stream(map,stream_in_stream)
8181
);
8282
}
8383

src/main/java/functional/Accumulate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
*/
99
public interface Accumulate <T>{
1010

11-
T accumulate(T t1, T t2);
11+
T apply(T t1, T t2);
1212
}

src/main/java/functional/ForEach.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
@FunctionalInterface
1010
public interface ForEach <T>{
1111

12-
void forEach(T item);
12+
void apply(T item);
1313
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
* @Author xiongyx
55
* on 2019/3/5.
66
*
7-
* map 映射操作
7+
* apply 映射操作
88
*/
99
@FunctionalInterface
10-
public interface Mapper <R,T>{
10+
public interface Map<R,T>{
1111

12-
R map(T item);
12+
R apply(T item);
1313
}

src/main/java/functional/NextItem.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* on 2019/3/5.
88
*/
99
@FunctionalInterface
10-
public interface NextItem {
10+
public interface NextItem<T> {
1111

12-
Stream next();
12+
Stream<T> apply();
1313
}

src/main/java/stream/Stream.java

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,119 @@
11
package stream;
22

33
import functional.ForEach;
4-
import functional.Mapper;
4+
import functional.Map;
55
import functional.NextItem;
66
import functional.Predicate;
77

8-
import java.util.List;
9-
108
/**
119
* @Author xiongyx
1210
* on 2019/3/5.
1311
*/
14-
public class Stream <T>{
12+
public class Stream <T> implements StreamInterface<T>{
1513

1614
//=====================================成员属性===============================
1715

1816
private T head;
1917

20-
private NextItem nextItem;
18+
private Stream<T> tail;
19+
20+
private boolean isEnd;
21+
22+
/**
23+
* 是否被求值过
24+
* */
25+
private boolean evaled;
26+
27+
private NextItem<T> eval;
28+
29+
public T getHead() {
30+
return head;
31+
}
32+
33+
public void setHead(T head) {
34+
this.head = head;
35+
}
36+
37+
public Stream<T> getTail() {
38+
return tail;
39+
}
40+
41+
public void setTail(Stream<T> tail) {
42+
this.tail = tail;
43+
}
44+
45+
public NextItem<T> getEval() {
46+
return eval;
47+
}
48+
49+
public void setEval(NextItem<T> eval) {
50+
this.eval = eval;
51+
}
2152

2253
//====================================构造函数===============================
2354

2455
public Stream() {
2556
}
2657

27-
private Stream(List<T> list){
58+
public Stream(T head, Stream<T> tail) {
59+
this.head = head;
60+
this.tail = tail;
61+
}
2862

63+
public Stream(NextItem<T> eval) {
64+
this.eval = eval;
2965
}
30-
//=====================================公共接口=================================
3166

32-
public <R> Stream<R> map(Mapper<R,T> mapper){
67+
public Stream(boolean isEnd) {
68+
this.isEnd = isEnd;
69+
}
3370

34-
return new Stream<>();
71+
//=====================================公共接口=================================
72+
73+
@Override
74+
public <R> Stream<R> map(Map<R,T> mapper){
75+
return map(mapper,this);
3576
}
3677

78+
@Override
3779
public Stream<T> filter(Predicate<T> predicate){
3880

3981
return new Stream<>();
4082
}
4183

84+
@Override
4285
public void forEach(ForEach<T> forEach){
4386
}
87+
88+
//=====================================私有方法=====================================
89+
90+
private <R> Stream<R> map(Map<R,T> mapper,Stream<T> stream){
91+
if(isEmptyStream(stream)){
92+
return StreamInterface.makeEmptyStream();
93+
}
94+
95+
R head = mapper.apply(stream.head);
96+
Stream tail = new Stream<>(
97+
()-> map(mapper,stream.force()
98+
));
99+
100+
101+
Stream newStream = new Stream(
102+
head,
103+
tail
104+
);
105+
return newStream;
106+
}
107+
108+
private void delay(NextItem<T> nextItem){
109+
110+
}
111+
112+
private Stream force(){
113+
return this.tail.eval.apply();
114+
}
115+
116+
private static boolean isEmptyStream(Stream stream){
117+
return stream.isEnd;
118+
}
44119
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package stream;
2+
3+
/**
4+
* @Author xiongyx
5+
* on 2019/3/5.
6+
*/
7+
public class StreamGenerator {
8+
9+
public static Stream<Integer> getIntegerStream(int n){
10+
if(n < 0){
11+
return StreamInterface.makeEmptyStream();
12+
}
13+
14+
Stream intStream = new Stream<>(
15+
n,
16+
new Stream(
17+
()->getIntegerStream(n-1)
18+
)
19+
);
20+
21+
22+
return intStream;
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package stream;
2+
3+
import functional.ForEach;
4+
import functional.Map;
5+
import functional.Predicate;
6+
7+
/**
8+
* @Author xiongyx
9+
* on 2019/3/5.
10+
*/
11+
public interface StreamInterface<T> {
12+
13+
<R> Stream<R> map(Map<R,T> mapper);
14+
15+
Stream<T> filter(Predicate<T> predicate);
16+
17+
void forEach(ForEach<T> forEach);
18+
19+
static <T> Stream<T> makeEmptyStream(){
20+
return new Stream<>(true);
21+
}
22+
}

0 commit comments

Comments
 (0)