Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit, mostly working, tests missing
- Loading branch information
0 parents
commit 063e33d
Showing
12 changed files
with
1,185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
*.iml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<name>LazySeq</name> | ||
<groupId>com.blogspot.nurkiewicz.lazyseq</groupId> | ||
<artifactId>lazyseq</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- Testing --> | ||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<version>6.8.1</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.easytesting</groupId> | ||
<artifactId>fest-assert-core</artifactId> | ||
<version>2.0M9</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.9.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# `LazySeq` - lazy sequence implementation for Java 8 | ||
|
||
## TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.blogspot.nurkiewicz.lazyseq; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author Tomasz Nurkiewicz | ||
* @since 5/8/13, 9:08 PM | ||
*/ | ||
class Cons<E> extends LazySeq<E> { | ||
private final E head; | ||
private volatile LazySeq<E> tailOrNull; | ||
private final Supplier<LazySeq<E>> tailFun; | ||
|
||
Cons(E head, Supplier<LazySeq<E>> tailFun) { | ||
this.head = head; | ||
this.tailFun = tailFun; | ||
} | ||
|
||
@Override | ||
public E head() { | ||
return head; | ||
} | ||
|
||
@Override | ||
public LazySeq<E> tail() { | ||
if (!isTailDefined()) { | ||
synchronized (this) { | ||
if (!isTailDefined()) { | ||
tailOrNull = tailFun.get(); | ||
} | ||
} | ||
} | ||
return tailOrNull; | ||
} | ||
|
||
@Override | ||
protected boolean isTailDefined() { | ||
return tailOrNull != null; | ||
} | ||
|
||
public <R> LazySeq<R> map(Function<? super E, ? extends R> mapper) { | ||
return cons(mapper.apply(head()), () -> tail().map(mapper)); | ||
} | ||
|
||
@Override | ||
public LazySeq<E> filter(Predicate<? super E> predicate) { | ||
if (predicate.test(head)) { | ||
return cons(head, () -> tail().filter(predicate)); | ||
} else { | ||
return tail().filter(predicate); | ||
} | ||
} | ||
|
||
@Override | ||
public <R> LazySeq<R> flatMap(Function<? super E, ? extends Stream<? extends R>> mapper) { | ||
final List<R> headFlattened = mapper.apply(head).collect(Collectors.<R>toList()); | ||
return concat(headFlattened, () -> tail().flatMap(mapper)); | ||
} | ||
|
||
@Override | ||
public LazySeq<E> limit(long maxSize) { | ||
if (maxSize > 0) { | ||
return cons(head, () -> tail().limit(maxSize - 1)); | ||
} else { | ||
return LazySeq.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/blogspot/nurkiewicz/lazyseq/DummyLazySeqCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.blogspot.nurkiewicz.lazyseq; | ||
|
||
import java.util.Set; | ||
import java.util.function.BiFunction; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
/** | ||
* @author Tomasz Nurkiewicz | ||
* @since 5/9/13, 11:09 AM | ||
*/ | ||
final class DummyLazySeqCollector<E> implements Collector<E, LazySeq<E>> { | ||
|
||
private static final DummyLazySeqCollector<?> INSTANCE = new DummyLazySeqCollector<>(); | ||
|
||
@SuppressWarnings("unchecked") | ||
public static <E> DummyLazySeqCollector<E> getInstance() { | ||
return (DummyLazySeqCollector<E>) INSTANCE; | ||
} | ||
|
||
@Override | ||
public Supplier<LazySeq<E>> resultSupplier() { | ||
throw new IllegalStateException("Should never be called"); | ||
} | ||
|
||
@Override | ||
public BiFunction<LazySeq<E>, E, LazySeq<E>> accumulator() { | ||
throw new IllegalStateException("Should never be called"); | ||
} | ||
|
||
@Override | ||
public BinaryOperator<LazySeq<E>> combiner() { | ||
throw new IllegalStateException("Should never be called"); | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() { | ||
throw new IllegalStateException("Should never be called"); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/blogspot/nurkiewicz/lazyseq/FixedCons.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.blogspot.nurkiewicz.lazyseq; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* @author Tomasz Nurkiewicz | ||
* @since 5/8/13, 9:09 PM | ||
*/ | ||
class FixedCons<E> extends LazySeq<E> { | ||
|
||
private final E head; | ||
private final LazySeq<E> tail; | ||
|
||
public FixedCons(E head, LazySeq<E> tail) { | ||
this.head = head; | ||
this.tail = tail; | ||
} | ||
|
||
@Override | ||
public E head() { | ||
return head; | ||
} | ||
|
||
@Override | ||
public LazySeq<E> tail() { | ||
return tail; | ||
} | ||
|
||
@Override | ||
protected boolean isTailDefined() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public <R> LazySeq<R> map(Function<? super E, ? extends R> mapper) { | ||
return cons(mapper.apply(head), tail.map(mapper)); | ||
} | ||
|
||
@Override | ||
public LazySeq<E> filter(Predicate<? super E> predicate) { | ||
if (predicate.test(head)) { | ||
return cons(head, tail.filter(predicate)); | ||
} else { | ||
return tail.filter(predicate); | ||
} | ||
} | ||
|
||
@Override | ||
public <R> LazySeq<R> flatMap(Function<? super E, ? extends Stream<? extends R>> mapper) { | ||
final List<R> headFlattened = mapper.apply(head).collect(Collectors.<R>toList()); | ||
return concat(headFlattened, tail.flatMap(mapper)); | ||
} | ||
|
||
@Override | ||
public LazySeq<E> limit(long maxSize) { | ||
if (maxSize > 0) { | ||
return cons(head, tail.limit(maxSize - 1)); | ||
} else { | ||
return LazySeq.empty(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
|
||
} |
Oops, something went wrong.