Skip to content

Instantly share code, notes, and snippets.

@haskellcamargo
Created December 8, 2015 19:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haskellcamargo/01745e75291b6145761f to your computer and use it in GitHub Desktop.
Save haskellcamargo/01745e75291b6145761f to your computer and use it in GitHub Desktop.
package br.com.ngi.ginga.business.util.seq;
/**
* Copyright (C) 2015 - NG Informática
*
* @author Marcelo Camargo
* @since 08/12/2015
*/
public interface Function<Ret, Arg> {
Ret call(Arg arg);
}
package br.com.ngi.ginga.business.util.seq;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
/**
* Copyright (C) 2015 - NG Informática
*
* @author Marcelo Camargo
* @since 08/12/2015
*/
public class Seq<T> {
private List<T> value;
@SafeVarargs
public Seq(T ...args) {
value = new ArrayList<>();
Collections.addAll(value, args);
}
public void add(T item) {
this.value.add(item);
}
public <Ret> Seq<Ret> map(Function<Ret, T> fn) {
Seq<Ret> buffer = new Seq();
for (T item : this.value) {
buffer.add(fn.call(item));
}
return buffer;
}
public Seq<T> filter(Function<Boolean, T> fn) {
Seq<T> buffer = new Seq<>();
for (T item : this.value) {
if (fn.call(item)) {
buffer.add(item);
}
}
return buffer;
}
public ArrayList<T> toArrayList() {
return (ArrayList<T>) this.value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment