Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yloiseau authored and Julien Ponge committed Jun 25, 2015
1 parent 96d54a6 commit cb17f23
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
31 changes: 14 additions & 17 deletions src/main/golo/lazylist.golo
Expand Up @@ -48,10 +48,6 @@ import java.util
# ............................................................................................... #
# Utils, constructors and conversions

local function head = |l| -> l: head()
local function tail = |l| -> l: tail()
local function isEmpty = |l| -> l: isEmpty()

----
Returns the empty list.
----
Expand Down Expand Up @@ -112,7 +108,7 @@ function fromIter = |it| -> match {
}

augment Iterable {

function asLazyList = |this| -> iteratorToLazyList(this: iterator())
}

local function iteratorToLazyList = |iterator| {
Expand All @@ -136,14 +132,12 @@ local function any = |it| {
}


function zip = |lists| {
return match {
when any(lists: map(^isEmpty)) then emptyList()
otherwise gololang.LazyList.cons(
Tuple.fromArray(lists: map(^head): toArray()),
-> zip(lists: map(^tail))
)
}
function zip = |lists| -> match {
when any(lists: map(^isEmpty)) then emptyList()
otherwise gololang.LazyList.cons(
Tuple.fromArray(lists: map(^head): toArray()),
-> zip(lists: map(^tail))
)
}


Expand Down Expand Up @@ -308,7 +302,7 @@ augment gololang.LazyList {

}

#=== HOF ===
#=== HOF and generators ===


function generator = |unspool, finished, x| {
Expand All @@ -322,13 +316,13 @@ function generator = |unspool, finished, x| {
)
}

function count = |start| ->
function count = |start| ->
gololang.LazyList.cons(start, -> gololang.lazylist.count(start + 1))

function count = -> gololang.lazylist.count(0)

----
Cycle infinitely through the lazy list
Cycle infinitely through a collection.
cycle(lazyList(1, 2, 3))
Expand All @@ -337,6 +331,9 @@ returns a infinite lazy list containing 1, 2, 3, 1, 2, 3, ...
cycle(emptyList())
returns `emptyList()`
* `list`: any object having a `head` and a `tail`
----
function cycle = |list| -> memocycle(list, list)

Expand All @@ -345,6 +342,6 @@ local function memocycle = |start, list| -> match {
when list: tail(): isEmpty() then
gololang.LazyList.cons(list: head(), -> memocycle(start, start))
otherwise
gololang.LazyList.cons(list: head(), -> memocycle(start, list:tail()))
gololang.LazyList.cons(list: head(), -> memocycle(start, list: tail()))
}

13 changes: 12 additions & 1 deletion src/main/java/gololang/LazyList.java
Expand Up @@ -34,7 +34,7 @@
* guarantee when, or even if, it will be called, this closure must be
* a pure, side-effect free, function.
*/
public class LazyList implements Collection<Object> {
public class LazyList implements Collection<Object>, HeadTail<Object> {

/**
* Represents the empty list.
Expand Down Expand Up @@ -64,6 +64,11 @@ public int size() {
public LazyList tail() {
return this;
}

@Override
public String toString() {
return "LazyList.EMPTY";
}
};

/**
Expand Down Expand Up @@ -336,6 +341,12 @@ public boolean containsAll(Collection<?> c) {
return true;
}

@Override
public String toString() {
return String.format("LazyList<head=%s, tail=%s>", head, tail);
}


@Override
public boolean add(Object e) {
throw new UnsupportedOperationException("a LazyList is immutable");
Expand Down

0 comments on commit cb17f23

Please sign in to comment.