Java elegant supplement
Java 8 delivered lambda expressions, but without the enhancement of basic libraries like List, Map, String, which makes lambda expression still not delightful.
Borrowered from Ruby basic library, Fig intends to supply the missing.
Elegant alternative to List: Seq
Seq<Integer> seq = Seq.of(1,2,3);
seq.shuffle(); // copy to a new seq and shuffle it
seq.shuffle$(); // shuffle the original seq
seq.forEach((value, idx) -> { // with index
// (1, 0) (2, 1) (3, 2)
});
seq.forEachCons(2, (values)->{
// [1,2] [2, 3]
});
seq.join("-"); //"1-2-3"
seq.map(a -> a+ 1).distinct().reverse().join()
Elegant alternative to Map: Hash
Hash<Integer, Integer> hash = Hash.newHash();
hash.set(1, 2).set(2, 3).set(3, 3);
hash.containsAny((k, v)-> k+v == 5 ); //true
hash.keysOf(3); // [2, 3]
Elegant alternative to String: CharSeq
CharSeq str = CharSeq.of("a b c d e f g");
str.split(" ").join("-").capitalize(); //"A-b-c-d-e-f-g"
str.partition("d e").map(CharSeq::trim); //["a b c", "d e", "f g"]
<dependency>
<groupId>com.worksap</groupId>
<artifactId>fig</artifactId>
<version>0.1.0</version>
</dependency>
Fig is different from Stream, and implemented without Stream. While, it is simpler concept -- supplement methods on basic library.
Stream has several characteristics:
- Trends to process each element independently
- Infinite that we can not get the size
- Designed for large data flow performance
Thus, Stream may not be able to support operations related with the size of it, nor operations involving multiple or even random elements.
Fig targets on "small data" operations within application logic, to provide simple & beautiful code writing about String, List, Map operation & transformation.
In traditional Java way, we use a lot of utilities (StringUtils, FileUtils) for the missing methods in standard library. While in Fig, we make an object oriented and functional way to free you from tedious code.
Fig has no external runtime dependency except JDK 8.
Fig uses conventions on method names similar with Ruby. If there is a pair of methods name, name$
, method ends with $
means calling this method will change the object itself, while calling the other won't.
Feel free to submit issues & PRs