Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ build
MANIFEST.MF
*/bin/**
*~
/.nb-gradle/
89 changes: 89 additions & 0 deletions core/src/main/java/fj/data/DList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package fj.data;

import fj.F;
import fj.P;
import fj.control.Trampoline;

/**
* Difference List. It converts left associative appends into right associative ones to improve performance.
*
* @version %build.number%
*/
public class DList<A> {
private final F<List<A>,Trampoline<List<A>>> appendFn;

private DList(F<List<A>,Trampoline<List<A>>> appendFn) {
this.appendFn = appendFn;
}

/**
* Wraps a list with a DList.
* @param <A>
* @param a the regular List
* @return the DList
*/
public static <A> DList<A> fromList(List<A> a) {
return new DList<>((List<A> tail) -> Trampoline.pure(a.append(tail)));
}

/**
* Concatenates all the internal Lists together that are held in
* the DList's lambda's state to produce a List.
* This is what converts the appending operation from left associative to right associative,
* giving DList it's speed.
* @return the final List
*/
public List<A> run() {
return appendFn.f(List.<A>nil()).run();
}

/**
* A empty DList.
* @param <A>
* @return a empty DList.
*/
public static <A> DList<A> nil() {
return new DList<>(Trampoline.<List<A>>pure());
}

/**
* Produces a DList with one element.
* @param <A>
* @param a the element in the DList.
* @return a DList with one element.
*/
public static <A> DList<A> single(A a) {
return new DList<>((List<A> tail) -> Trampoline.pure(tail.cons(a)));
}

/**
* Prepends a single element on the DList to produce a new DList.
* @param a the element to append.
* @return the new DList.
*/
public DList<A> cons(A a) {
return DList.single(a).append(this);
}

/**
* Appends a single element on the end of the DList to produce a new DList.
* @param a the element to append.
* @return the new DList.
*/
public DList<A> snoc(A a) {
return this.append(DList.single(a));
}

/**
* Appends two DLists together to produce a new DList.
* @param other the other DList to append on the end of this one.
* @return the new DList.
*/
public DList<A> append(DList<A> other) {
return new DList<>(kleisliTrampCompose(this.appendFn, other.appendFn));
}

private static <A,B,C> F<A,Trampoline<C>> kleisliTrampCompose(F<B,Trampoline<C>> bc, F<A,Trampoline<B>> ab) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be useful as a public method of Trampoline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not really a true Kleisli composition, because of the use of Trampoline.suspend(...). But a bit of careful rearrangement compose could be added and used from Trampoline.

return (A a) -> ab.f(a).bind((B b) -> Trampoline.suspend(P.lazy(() -> bc.f(b))));
}
}