Java Abuse – Currency Pattern

Here’s a sillier one from last night…

People often complain about not being able to return multiple values from a method in Java. I can’t see a good reason for wanting to do this, but some do.

The example I was given was wanting to do:

int foo, bar, baz; list(foo, bar, bar) = func(ponies);

Where func(ponies) returns 3 values which are then assigned to the local variables foo, bar, and baz.

There are plenty of sensible ways of achieving this, but what if we’re trying to get as close as possible to the above method? We can only return one thing from a method, but that could be a chain. We also can’t modify the local foo,bar,baz from inside the list method, but we could if we wrap them in references.

If we abuse the fact that currency characters are valid identifiers in Java, for the ultimate in unreadability; We can use $(x,y) to be a pair of x and y, $$ to terminate a list, and £(x) to either reference or dereference x.

Then we can do something like this ¬_¬

/** OUTPUT
Foo = 1
Bar = 2
Bingo = Badgers
Baz = 3
**/
 
package scratchpad;
 
import static scratchpad.$.$;
import static scratchpad.$$.$$;
import static scratchpad.$.expand;
import static scratchpad.£.£;
 
public class MultipleReturn
{
	public static void main(String... args)
	{
		new MultipleReturn().demo();
	}
 
	public void demo()
	{
		£<Integer> foo = £(-1), bar = £(-1), baz = £(-1);
		£<String> bingo = £("");
 
		expand(foo()).into(foo,bar,bingo,baz);
 
		System.out.println("Foo = " + £(foo));
		System.out.println("Bar = " + £(bar));
		System.out.println("Bingo = " + £(bingo));
		System.out.println("Baz = " + £(baz));
	}
 
	public $<Integer,$<Integer,$<String,$<Integer,$$>>>> foo()
	{
		return $(1,$(2,$("Badgers",$(3,$$))));
	}
}
 
interface Tuple {}
 
class $$ implements Tuple
{
	private $$() { }
	public static $$ $$ = new $$();
}
 
class $<T,U extends Tuple> implements Tuple
{
	public T _1;
	public U _2;
 
	protected $(T t,U u)
	{
		this._1 = t;
		this._2 = u;
	}
 
	public static <T> $<T,$$> $(T t)
	{
		return new $<T,$$>(t,$$);
	}
 
	public static <T,U extends Tuple> $<T,U> $(T t, U u)
	{
		return new $<T,U>(t,u);
	}
 
	public interface Expander
	{
		public void into(£... refs);
	}
 
	public static Expander expand(final $<?,? extends Tuple> vals)
	{
		return new Expander()
		{
			public void into(scratchpad.£... refs)
			{
				if (refs.length < 1)
					return;
 
				$ current = vals;
				refs[0]._1 = current._1;
				if (current._2 instanceof $$)
					return;
				int i = 1;
				while (!(current._2 instanceof $$))
				{
					if (i >= refs.length)
						return;
					current = ($)current._2;
					refs[i]._1 = current._1;
					i++;
				}
			}
 
		};
 
	}
}
 
class £<T> extends $<T,$$>
{
	protected £(T t)
	{
		super(t,$$);
	}
 
	public static <T> £<T> £(T t)
	{
		return new £<T>(t);
	}
 
	public static <T> T £(£<T> t)
	{
		return t._1;
	}
}
This entry was posted in Java and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Jonathan
    Posted September 12, 2009 at 12:51 pm | Permalink

    Ho god and here i was using this:

    //The jre does not seem to provide the equivalent of std::pair
    // or any other Implementation of a Tuple, the closest thing
    // i could find is a internal class to Map
    // a Entry and it is a Interface…
    //So without further ado basic Pair implementation,
    // because i just cannot live without it
    public final class Pair
    {
    private F mFirst;
    private S mSecond;

    public Pair(final F inFirst, final S inSecond)
    {
    super();

    mFirst = inFirst;
    mSecond = inSecond;
    }

    public F first()
    {
    return mFirst;
    }

    public S second()
    {
    return mSecond;
    }

    public void setFirst(final F inFirst)
    {
    mFirst = inFirst;
    }

    public void setSecond(final S inSecond)
    {
    mSecond = inSecond;
    }
    }

  2. Posted February 6, 2012 at 9:22 am | Permalink

    Anybody doing music production inside a professional studio or even a tiny home business must have ways to record music and methods to mix music. These can end up being analog devices. But because software is now so inexpensive, digital tools is now able to be found practically in most studios. Beyond these fundamentals there are numerous tools that could possibly be used in any studio including MIDI controllers, sequencers, tone generators, samplers, synthesizers and a lot more. Obviously it is usually important so that you can play music and hear what will be played but the focus this is on recording.

    Recorder

    Multitrack recording or tracking has existed since the the middle of 1950s. It allows for different the different parts of music to possibly be recorded on unique tracks. So, for example, drums might be during one track, guitar on yet another, vocals on a third etc. The recording is finished by line inputs to the mixer, either from amplifiers, or from the actual instruments themselves. Although old 4- in addition to 8-track analog tape recorders remain available, most recording is currently done digitally having recording software.

    Mixing Console

    A mixing console is the vital thing most people consider when they find out “recording studio. ” Also termed an audio appliance, sound board or maybe mixer, the mixing console may be the long board along with sliding controls within a traditional recording facilities. The mixer is utilized to combine or mix the several tracks of a great audio recording. It can change the amount, dynamics or timbre in the audio signals. Both digital in addition to analog mixers are acquireable. Although digital mixers tend to be inexpensive, many recording technicians still prefer analog boards for convenience. In most conditions analog mixing boards is usually connected to electronic recording equipment as well as software.

    MIDI and much more

    A variety involving other items could possibly be added to the particular studio. Some of the commonest of these are meant to supplement, augment or exchange live instruments. MIDI devices and synthesizers can be utilized for piano as well as keyboard sounds but they can double to artificially produce other sounds along with instruments for creating. Tone generators, samplers and groove machines create procession and also other sounds to product live instruments or perhaps re-create sounds about demand.

    Software

    A variety of software programs allow for the actual recording and pairing of music. Some software packages even have built-in synthesizers, rhythm machines and much more. The leaders intended for professional recording are usually AVID’s Pro Equipment and Adobe Audition, but these are just one or two of dozens connected with possibilities. Apple’s Garage Band enables multi-track recording, mixing and provides built-in synthesizers, yet it is inexpensive and in many cases comes pre-installed in many new Apple computers. Audacity by Sound Forge makes for multi-track recording in addition to mixing, has a selection of available plug ins which is free for House windows, Mac, Linux and Unix.

    Read More: music notation software http://www.music-production-software.com/

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*