public final class StaticImports extends Object
A mini data definition language composed of vec(), tup(), map(), set(), plus xform() which makes java.util collections transformable.
import org.organicdesign.fp.StaticImports.*
// Create a new vector of integers
vec(1, 2, 3, 4);
// Create a new set of Strings
set("a", "b", "c");
// Create a tuple of an int and a string (a type-safe heterogeneous container)
tup("a", 1);
// Create a map with a few key value pairs
map(tup("a", 1), tup("b", 2), tup("c", 3);
There are only a few methods in this project to take varargs and they are all in this file. Writing out versions that took multiple type-safe arguments caused IntelliJ to present all of them for auto-completion which was overwhelming, so I reverted to varargs. Also, varargs relax some type safety rules (variance) for data definition in a generally helpful (rarely dangerous) way.
If you're used to Clojure/JSON, you'll find that what's a map (dictionary) in those languages usually becomes a tuple in Paguro. A true map data structure in a type-safe language is homogeneous, meaning that every member is of the same type (or a descendant of a common ancestor). Tuples are designed to contain unrelated data types and enforce those types.
As with any usage of import *, there could be issues if you import 2 different versions of this file in your classpath. Java needs a data definition language so badly that I think it is worth the risk. Also, I don't anticipate this file changing much, except to add more tup() implementations, which shouldn't break anything. Let me know if you find that the danger outweighs convenience or have advice on what to do about it.
Modifier and Type | Method and Description |
---|---|
static <K,V> ImMap<K,V> |
map(Map.Entry<K,V>... kvPairs)
Returns a new PersistentHashMap of the given keys and their paired values.
|
static <K,V> MutableUnsortedMap<K,V> |
mutableMap(Map.Entry<K,V>... kvPairs)
Returns a new MutableUnsortedMap of the given keys and their paired values.
|
static <T> MutableUnsortedSet<T> |
mutableSet(T... items)
Returns a new MutableUnsortedSet of the values.
|
static <T> MutableList<T> |
mutableVec(T... items)
Returns a MutableVector of the given items.
|
static <T> ImSet<T> |
set(T... items)
Returns a new PersistentHashSet of the values.
|
static <K,V> ImSortedMap<K,V> |
sortedMap(Comparator<? super K> comp,
Iterable<Map.Entry<K,V>> kvPairs)
Returns a new PersistentTreeMap of the specified comparator and the given key/value pairs.
|
static <K extends Comparable<K>,V> |
sortedMap(Iterable<Map.Entry<K,V>> kvPairs)
Returns a new PersistentTreeMap of the given comparable keys and their paired values, sorted in
the default ordering of the keys.
|
static <T> ImSortedSet<T> |
sortedSet(Comparator<? super T> comp,
Iterable<T> elements)
Returns a new PersistentTreeSet of the given comparator and items.
|
static <T extends Comparable<T>> |
sortedSet(Iterable<T> items)
Returns a new PersistentTreeSet of the given comparable items.
|
static <T,U> Tuple2<T,U> |
tup(T t,
U u)
Returns a new Tuple2 of the given items.
|
static <T,U,V> Tuple3<T,U,V> |
tup(T t,
U u,
V v)
Returns a new Tuple3 of the given items.
|
static <T> ImList<T> |
vec(T... items)
Returns a new PersistentVector of the given items.
|
static <T> UnmodIterable<T> |
xform(Iterable<T> iterable)
If you need to wrap a regular Java collection or other iterable outside this project to perform
a transformation on it, this method is the most convenient, efficient way to do so.
|
static <T> UnmodIterable<T> |
xformArray(T... items)
If you need to wrap a regular Java array outside this project to perform
a transformation on it, this method is the most convenient, efficient way to do so.
|
@SafeVarargs public static <K,V> ImMap<K,V> map(Map.Entry<K,V>... kvPairs)
tup(Object, Object)
method to define those key/value pairs briefly and
easily. This data definition method is one of the few methods in this project that support
varargs.kvPairs
- Key/value pairs (to go into the map). In the case of a duplicate key, later
values in the input list overwrite the earlier ones. The resulting map can contain zero or one
null key and any number of null values. Null k/v pairs will be silently ignored.@SafeVarargs public static <K,V> MutableUnsortedMap<K,V> mutableMap(Map.Entry<K,V>... kvPairs)
tup(Object, Object)
method to define those key/value pairs briefly and
easily. This data definition method is one of the few methods in this project that support
varargs.kvPairs
- Key/value pairs (to go into the map). In the case of a duplicate key, later
values in the input list overwrite the earlier ones. The resulting map can contain zero or one
null key and any number of null values. Null k/v pairs will be silently ignored.@SafeVarargs public static <T> MutableUnsortedSet<T> mutableSet(T... items)
@SafeVarargs public static <T> MutableList<T> mutableVec(T... items)
@SafeVarargs public static <T> ImSet<T> set(T... items)
public static <K,V> ImSortedMap<K,V> sortedMap(Comparator<? super K> comp, Iterable<Map.Entry<K,V>> kvPairs)
comp
- A comparator (on the keys) that defines the sort order inside the new map. This
becomes a permanent part of the map and all sub-maps or appended maps derived from it. If you
want to use a null key, make sure the comparator treats nulls correctly in all circumstances!kvPairs
- Key/value pairs (to go into the map). In the case of a duplicate key, later
values in the input list overwrite the earlier ones. The resulting map can contain zero or one
null key (if your comparator knows how to sort nulls) and any number of null values. Null k/v
pairs will be silently ignored.public static <K extends Comparable<K>,V> ImSortedMap<K,V> sortedMap(Iterable<Map.Entry<K,V>> kvPairs)
kvPairs
- Key/value pairs (to go into the map). In the case of a duplicate key, later
values overwrite earlier ones.public static <T> ImSortedSet<T> sortedSet(Comparator<? super T> comp, Iterable<T> elements)
comp
- A comparator that defines the sort order of elements in the new set. This
becomes part of the set (it's not for pre-sorting).elements
- items to go into the set. In the case of duplicates, later elements overwrite
earlier ones.public static <T extends Comparable<T>> ImSortedSet<T> sortedSet(Iterable<T> items)
public static <T,U> Tuple2<T,U> tup(T t, U u)
public static <T,U,V> Tuple3<T,U,V> tup(T t, U u, V v)
@SafeVarargs public static <T> ImList<T> vec(T... items)
public static <T> UnmodIterable<T> xform(Iterable<T> iterable)
@SafeVarargs public static <T> UnmodIterable<T> xformArray(T... items)
Copyright © 2017. All rights reserved.