Posts

Showing posts from 2013

A tuple implementation in Java

Here is a fairly simple implementation of tuples in Java. The purpose of tuples is to aggregate values of any type into a single object, which is strongly typed. This implementation follows the way .NET implements tuples. For other examples you could look in StackOverflow.com (example: here ). It begins with an interface for all tuple types: package net . intrepidis . library . tuple ; public interface Tuple { int size ( ) ; } Here's a tuple of two items: package net . intrepidis . library . tuple ; public class Tuple2<T1,T2> implements Tuple { public final T1 item1 ; public final T2 item2 ; public Tuple2 ( final T1 item_1 , final T2 item_2 ) { item1 = item_1 ; item2 = item_2 ; } @ Override public int size ( ) { return 2 ; } } Here's a tuple of three items: package net . intrepidis . library . tuple ; public class Tuple3<T1,T2,T3> implements Tuple { public final T1 item1 ; p...

Using TotallyLazy the functional library for Java (et al.)

In software development when I revisit an imperative language, after coding in a functional language, there are many idioms that I miss. Such as pattern matching, language integrated sequence manipulation, tuples and, of course, functions-as-data... the list goes on. Languages such as Java can mimic some of these “out of the box” functional features. For example, functions-as-data can be mimicked by wrapping functions in an anonymous class, although you would still lose  function composition . Creating tuple classes is quite easy, here's an example: Tuples in Java . Pattern matching is more complicated, an implementation can be found here: Pattern Matching in Java . For “sequence manipulation” there are a number of libraries out there. One of which is TotallyLazy , which I will now document. Upon first using the library I immediately hit a wall: there are so many features that I didn't know where to begin. At that time I couldn't find any helpful guides on the Int...