
-----------------------------------
wtd
Sat Dec 16, 2006 7:36 pm

Battle of the Titans!
-----------------------------------
Java vs. C#.  The beloved children of two 800 pound gorillas go head to head.  Read the code, then fight it out over which is more appealing.  :)

C#

using System;
using System.Collections;

public class Test
{
        public static void Main() 
        {	  
                Range r = new ExclusiveRange(1, 5);
	  
                foreach (int i in r) {
                        Console.WriteLine(i);
                }
	  
                int sum = r.Reduce(Add, 0);
                Console.WriteLine("Sum: {0}", sum);
        }
   
        static int Add(int a, int b)
        {
                return a + b;
        }
}

class Range : IEnumerable
{
        class Enumerator : IEnumerator
        {
                private Range range;
                private int current;
	  
                public Enumerator(Range range) 
                {
                        this.range = range;
                        this.current = range.Start - 1;
                }
	  
                public Range TargetRange {
                        get { return range; }
                }
	  
                public object Current {
                        get { return current; }
                }
	  
                public bool MoveNext() 
                {
                        if (current >= range.EffectiveEnd) {
                                return false;
                        }
                        else {
                                current++;
                                return true;
                        }
                }
	  
                public void Reset()
                {
                        current = range.Start - 1;
                }
        }
   
        public delegate T ReductionArg(T init, int current);

        private int start, end;
   
        public Range(int start, int end)
        {
                this.start = start;
                this.end = end;
        }
   
        public IEnumerator GetEnumerator()
        {
                return new Enumerator(this);
        }
   
        public virtual int Start {
                get { return start; }
        }
   
        public virtual int End {
                get { return end; }  
        }
   
        public virtual int EffectiveEnd {
                get { return end; }
        }
   
        public virtual T Reduce(ReductionArg f, T init)
        {
                foreach (int i in this) {
                        init = f(init, i);
                }
				
                return init;
        }
}

class ExclusiveRange : Range
{
        public ExclusiveRange(int start, int end) : base(start, end) 
        { 
        }
   
        public override int EffectiveEnd {
                get { return End - 1; }
        }
}  

Java

public class RangeTest {
        public static void main(String[] args) {
                Range r = new ExclusiveRange(1, 5);
				
                for (int i : r) {
                        System.out.println(i);
                }
				
                int sum = r.reduce(add, 0);
                System.out.printf("Sum: %d\n", sum);
        }
		
        static Range.ReductionArg add = new Range.ReductionArg() {
                public Integer perform(Integer init, int current) {
                        return init + current;
                }
        };
}

class Range implements Iterable {
        static class Iterator implements java.util.Iterator {
                private Range range;
                private int current;
		
                public Iterator(Range range) {
                        this.range = range;
                        this.current = range.getStart() - 1;
                }
				
                public boolean hasNext() {
                        return current < range.getEffectiveEnd();
                }
				
                public Integer next() {
                        return ++current;
                }
				
                public void remove() {
                }
        }
		
        public static interface ReductionArg {
                public T perform(T init, int current);
        }

        private int start, end;
   
        public Range(int start, int end) {
                this.start = start;
                this.end = end;
        }
		
        public java.util.Iterator iterator() {
                return new Iterator(this);
        }
		
        public int getStart() {
                return start;
        }
		
        public int getEnd() {
                return end;
        }
		
        public int getEffectiveEnd() {
                return end;
        }
		
        public  T reduce(ReductionArg f, T init) {
                for (int i : this) {
                        init = f.perform(init, i);
                }
				
                return init;
        }
}

class ExclusiveRange extends Range {
        public ExclusiveRange(int start, int end) {
                super(start, end);
        }
		
        public int getEffectiveEnd() {
                return getEnd() - 1;
        }
}

-----------------------------------
Clayton
Sat Dec 16, 2006 7:42 pm


-----------------------------------
I would have to argue that the C# code wins here. To me it just seems more appealing. Especially when it comes to something like this:


static Range.ReductionArg add = new Range.ReductionArg() {
                public Integer perform(Integer init, int current) {
                        return init + current;
                }
        }; 


compared to:


static int Add(int a, int b)
        {
                return a + b;
        } 

