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


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

Re: Comparison structure?

From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Comparison structure?
Date 2016-10-04 22:29 -0400
Organization Aioe.org NNTP Server
Message-ID <nt1oim$1646$1@gioia.aioe.org> (permalink)
References <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> <vin7vbtorg7k171plq29ve3fo4fut237d5@4ax.com> <cfb773de-671a-46e7-9d79-e821780c7bcb@googlegroups.com> <nt109m$r82$1@dont-email.me> <nt1gom$thu$1@gioia.aioe.org>

Show all headers | View raw


On 10/4/2016 8:16 PM, Arne Vajhøj wrote:
> Well - if one is competing in a code obfuscation contest then I will
> suggest:
>
>     int compareMe = 1;
>     final int assignMe = cases[Integer.signum(compareMe - compareTo) +
> 1].option(compareMe, compareTo);
>     int compareTo = 2;
>
> with:
>
>     @FunctionalInterface
>     private interface Case {
>         public int option(int compareMe, int compareTo);
>     }
>     private static Case[] cases = { (me,to) -> me, (me,to) -> 4, (me,to)
> -> me + 2 };
>
> :-)

Well - I think the concept can actually be twisted into something
that is almost readable!

     @FunctionalInterface
     private static interface Case {
         public int option(int compareMe, int compareTo);
     }

     public static abstract class Comparison {
         protected Case lt;
         protected Case eq;
         protected Case gt;
         private int compareMe;
         private int compareTo;
         public Comparison(int compareMe, int compareTo) {
             this.compareMe = compareMe;
             this.compareTo = compareTo;
         }
         public int execute() {
             switch(Integer.signum(compareMe - compareTo)) {
                 case -1:
                     return lt.option(compareMe, compareTo);
                 case 0:
                     return eq.option(compareMe, compareTo);
                 case 1:
                     return gt.option(compareMe, compareTo);
                 default:
                     throw new RuntimeException("Bye bye");
             }
         }
     }

     int compareMe = 1;
     int compareTo = 2;
     final int assignMe = new Comparison(compareMe, compareTo) {
             {
                 lt = (me,to) -> me;
                 eq = (me,to) -> 4;
                 gt = (me,to) -> me + 2;
             }
         }.execute();

Arne


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


Thread

Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 07:25 -0700
  Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 10:46 -0400
  Re: Comparison structure? Roedy Green <inquiry@mindprod.com> - 2016-10-04 09:53 -0700
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 10:29 -0700
      Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 15:35 -0400
        Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:16 -0400
          Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 22:29 -0400
            Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-05 08:08 -0400
              Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-05 20:27 -0400
  Re: Comparison structure? Martin Gregorie <martin@address-in-sig.invalid> - 2016-10-04 17:37 +0000
    Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 12:02 -0700
    Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:12 +0200
      Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:14 +0200
  Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:03 -0400
  Re: Comparison structure? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-10-05 22:41 +0200

csiph-web