Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35235

Re: idea for mutiple variadic function

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: idea for mutiple variadic function
Date 2016-08-09 08:12 -0400
Organization A noiseless patient Spider
Message-ID <nochak$15k$1@dont-email.me> (permalink)
References <5996822c-1276-4b27-88d4-79fcaf551259@googlegroups.com>

Show all headers | View raw


On 8/9/2016 5:10 AM, Philipp Kraus wrote:
> Hello,
>
> I have got a enum with this structure
>
> enum myEnum
> {
>      FIRST(
>              Stream.of( "hello", "foo", "bar" ),
>              Stream.of( 1, 4, 7 )
>      );
>
>
>      private final Map<String,Number> data;
>
>
>      myEnum( Stream<String> a, Stream<Number> b )
>      {
>             data = Collections.unmodifyableMap( StreamUtils.zip( a, b, (i,j) -> new AbstractMap.SimpleImmutablePair( i, j ) ).collect( Collectors.toMap( i -> i.getKey(), i ->i.getValue() ) );
>      }
> }
>
>
> I need something like
>
> myEnum( [ Stream<String>, Stream<Number> ],  [ Stream<Integer>, Stream<String> ] .... )
>
> tupel of streams with different types but as variadic arguments.
> I'm thinking about functional structure but I don't have a very good idea. In my case the streams within a tuple has got equal number of elements.
>
> Did you have any idea to create a clean constructor parameter definition?

     Most directly, you could create a class to hold the pair of
streams, and make the constructor variadic in the pairs:

	private static class Pair {
	    private final Stream<String> strings;
	    private final Stream<Number> numbers;
	    // ...
	}

	FIRST (
	    new Pair(Stream.of("hello", "foo", "bar"),
	             Stream.of(1, 4, 7)
	);
	
	myEnum(Pair... pairs) { ... }



However, this still leaves the annoying possibility that the two
streams of a pair might have different lengths.  Why not turn the
whole thing inside-out, and deal with String/Number pairs right
from the beginning?

	private static class Pair {
	    private final String string;
	    private final Number number;
	    // ...
	}

	FIRST (
	    new Pair("hello", 1),
	    new Pair("foo", 4),
	    new Pair("bar", 7)
	);

	myEnum(Pair... pairs) { ... }

     That is, in your example the streams create a problem (length
mismatch) and don't appear to offer a benefit -- so why use them?

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

idea for mutiple variadic function Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-08-09 02:10 -0700
  Re: idea for mutiple variadic function Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-09 08:12 -0400
    Re: idea for mutiple variadic function Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-08-25 16:21 -0700

csiph-web