Skip to content

Commit

Permalink
Merge pull request #2 from bloder/take-drop
Browse files Browse the repository at this point in the history
Add Take and Drop function
  • Loading branch information
haskellcamargo committed Apr 28, 2016
2 parents aa35c6c + 780ee5f commit f98b6fa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
28 changes: 28 additions & 0 deletions seq/src/main/java/com/jmonad/seq/Seq.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,34 @@ public <Ret> Seq<T> uniqueBy(Function<Ret, T> fn) {
return buffer;
}

/**
* Returns the first n elements of the sequence
* @param amount
* @return Seq<T>
*/
public Seq<T> take(int amount) {
Seq<T> buffer = new Seq<>();
if (amount <= 0) return buffer;
if (amount > this.list.size()) return new Seq<>(this.list);

for (int i = 0; i < amount; i++) buffer.add(this.list.get(i));
return buffer;
}

/**
* Remove the first n elements of the sequence
* @param amount
* @return Seq<T>
*/
public Seq<T> drop(int amount) {
Seq<T> buffer = new Seq<>();
if (amount <= 0) return new Seq<>(this.list);
if (amount > this.list.size()) return buffer;

for (int i = amount; i < this.list.size(); i++) buffer.add(this.list.get(i));
return buffer;
}

/**
* Takes a list of items, and using the binary function supplied, folds them
* into a single value. Requires an initial value (the second argument),
Expand Down
25 changes: 25 additions & 0 deletions seq/src/test/java/com/jmonad/seq/SeqDropTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jmonad.seq;

import org.junit.Test;

public class SeqDropTest {

private Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
private Seq<Integer> elements = new Seq<>(numbers);

@Test public void dropListElementsTest() {
assert elements.drop(5).toArrayList().toString().equals("[6, 7, 8, 9, 10]");
}

@Test public void dropElementsWithNoAmountTest() {
assert elements.drop(0).toArrayList().toString().equals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");
}

@Test public void dropElementsWithNegativeAmountTest() {
assert elements.drop(-1).toArrayList().toString().equals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");
}

@Test public void dropElementsWithAmountHigherThanListSizeTest() {
assert elements.drop(20).toArrayList().toString().equals("[]");
}
}
25 changes: 25 additions & 0 deletions seq/src/test/java/com/jmonad/seq/SeqTakeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.jmonad.seq;

import org.junit.Test;

public class SeqTakeTest {

private Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
private Seq<Integer> elements = new Seq<>(numbers);

@Test public void takeListElementsTest() {
assert elements.take(5).toArrayList().toString().equals("[1, 2, 3, 4, 5]");
}

@Test public void takeElementsWithNoAmountTest() {
assert elements.take(0).toArrayList().toString().equals("[]");
}

@Test public void takeElementsWithNegativeAmountTest() {
assert elements.take(-1).toArrayList().toString().equals("[]");
}

@Test public void takeElementsWithAmountHigherThanListSizeTest() {
assert elements.take(20).toArrayList().toString().equals("[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]");
}
}

0 comments on commit f98b6fa

Please sign in to comment.