Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Any language welcome challenge
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue Feb 05, 2008 11:38 pm   Post subject: Any language welcome challenge

Well, any language in which one can write a program that satisfies the problem description. Wink

Create a generic class Foo which takes as its type parameter T any class which answers to a method "baz" which returns an integer. The generic class Foo must have a constructor which takes an object of type T, storing that as "x". Foo must have a method qux which returns the result of multiplying x's baz method's return value by three.
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Tue Feb 05, 2008 11:40 pm   Post subject: RE:Any language welcome challenge

Are we to post our solutions here?
wtd




PostPosted: Wed Feb 06, 2008 12:28 am   Post subject: Re: Any language welcome challenge

Yes.

To kick it off:

code:
class ['t] foo (x : 't) =
    object
        method qux = x#baz * 3
    end
rdrake




PostPosted: Wed Feb 06, 2008 1:20 am   Post subject: Re: Any language welcome challenge

Good old C# 2.0:
code:
public interface IWooble
{
        int Wooble();
}

public class WoobleObject : IWooble
{
        public int Wooble()
        {
                return 42;
        }
}

public class Baz<T> where T:IWooble
{
        private T _O;

        public Baz(T o)
        {
                this._O = o;
        }

        public int Ninja()
        {
                return this._O.Wooble() * 3;
        }
}

public class Test
{
        public static void Main(string[] args)
        {
                Baz<IWooble> b = new Baz<IWooble>(new WoobleObject());
                System.Console.WriteLine(b.Ninja());
        }
}
OneOffDriveByPoster




PostPosted: Wed Feb 06, 2008 11:11 am   Post subject: Re: Any language welcome challenge

code:
template <typename T>
class Foo {

    T x;

public:
    Foo (T const &x) : x(x) { }
    int qux(void) {
        return x.baz() * 3;
    }   
};
Hopefully close enough...
wtd




PostPosted: Wed Feb 06, 2008 5:43 pm   Post subject: RE:Any language welcome challenge

Excellent.
rdrake




PostPosted: Fri Feb 08, 2008 3:46 am   Post subject: RE:Any language welcome challenge

Improved C# 3.0 version:
code:
public class WoobleObject
{
   public virtual int Wooble() { return 42; }
}

public class Baz<T> where T : WoobleObject
{
   public T O { get; set; }

   public int Ninja() { return this.O.Wooble() * 3; }
}

public class Test
{
   public static void Main(string[] args)
   {
      var b = new Baz<WoobleObject>() { O = new WoobleObject() };
      System.Console.WriteLine(b.Ninja());
   }
}

EDIT: Dan, why does uppercase Test get changed to just lowercase test?
wtd




PostPosted: Fri Feb 08, 2008 1:34 pm   Post subject: Re: Any language welcome challenge

Scala:

code:
trait Baz {
    def baz: Int
}

class Foo[T <: Baz](val x: T) {
    def qux = x.baz * 3
}

object Test extends Application {
    val f = new Foo(new Baz { def baz = 42 })
    Console println (f qux)
}
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Sat Feb 09, 2008 1:24 am   Post subject: Re: Any language welcome challenge

Another:
Ruby:
class WoobleObject
    def wooble
        42
    end
end

class Baz
    def initialize(o)
        @o = o
    end
   
    def ninja
        @o.wooble * 3
    end
end

puts Baz.new(WoobleObject.new).ninja
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: