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

Username:   Password: 
 RegisterRegister   
 [Scala] A little code
Index -> Programming, Java -> Java Tutorials
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Jun 07, 2007 6:09 pm   Post subject: RE:[Scala] A little code

code:
// A MyRange object could really just be an Iterator[Int].

class MyRange(val start: Int, val end: Int) extends Iterator[Int] {
   protected var current = start
   def hasNext = current <= end
   def next = {
      val c = current
      current += 1
      c     
   }
}

// And a MyExclusiveEndedRange.

class MyExclusiveEndedRange(override val start: Int, override val end: Int)
 extends MyRange(start, end) {
   override def hasNext = current < end
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jun 07, 2007 8:57 pm   Post subject: RE:[Scala] A little code

code:
// Let's number the numbers in a range.

var count = 0
val r = new MyRange(5, 10)

while (i.hasNext) {
   Console.println(count + ": " + i.next)
   count += 1
}

// Easier?

var count = 0
val r = new MyRange(5, 10)

for (val x <- r) {
   Console.println(count + ": " + x)
   count += 1
}

// Not easy enough.

val r = new MyRange(5, 10)
val i = r.counted

for (val x <- i)
   Console.println(i.count + ": " + x)

// How about a new class?

class EnumeratedIterator[+T](val targetIterator: Iterator[T])
 extends Iterator[(Int, T)] {
   protected val iter = targetIterator.counted
   def hasNext = iter.hasNext
   def next = {
      val n = iter.next
      (iter.count, n)
   }
}

// And now...

val r = new MyRange(5, 10)

for (val (count, x) <- new EnumeratedIterator(r))
   Console.println(count + ": " + x)
wtd




PostPosted: Thu Jun 07, 2007 11:23 pm   Post subject: RE:[Scala] A little code

code:
// Use the power of the standard library!

for (val (x, count) <- new MyRange(5, 10) zipWithIndex)
   Console.println(count + ": " + x)

// Let's try some XML.

val xml = <list>
   { for (val (x, c) <- new MyRange(5, 10) zipWithIndex)
        yield <item><num>{c}</num><val>{x}</val></item> }
</list>

Console.println(xml)
rizzix




PostPosted: Sat Jun 09, 2007 3:09 am   Post subject: RE:[Scala] A little code

Scala:
class Foo {
    private[this] var n = ""
    def name = n
    def name_=(n : String) =
        this.n = if (n == "bar") "baz" else n
}

val foo = new Foo;
foo.name = "bar"
Console print foo.name // "baz"


Properties Smile
wtd




PostPosted: Thu Jun 21, 2007 7:49 pm   Post subject: RE:[Scala] A little code

Let's create our own loop.

code:
def myFor(init: Int, test: Int => Boolean, update: Int => Int)(action: Int => Unit): Unit =
   if (test(init)) {
      action(init)
      myFor(update(init), test, update)(action)
   }


And then we'll try calling it.

code:
myFor(1, _ <= 3, _ + 1) {
   case n => Console println n
}


Or perhaps...

code:
val doFromOneToThree = myFor(1,  _ <= 3, _ + 1) _
doFromOneToThree { case n => Console println n }
wtd




PostPosted: Thu Jun 21, 2007 8:11 pm   Post subject: RE:[Scala] A little code

Perhaps something inspired by education.

code:
type Name = (String, String)
type NameList = List[Name]

class MathClass(protected var teachers: NameList, protected var students: NameList) {
    def addTeacher(teacher: Name) {
        teachers = teachers ::: List(teacher)
    }

    def addStudent(student: Name) {
        students = students ::: List(student)
    }

    def foreachTeacher(action: Name => Unit) {
        for (val teacher <- teachers) action(teacher)
    }

    def foreachStudent(action: Name => Unit) {
        for (val student <- students) action(student)
    }

    def foreachTeacherStudentCombination(action: (Name, Name) => Unit) {
        for (val teacher <- teachers;
             val student <- students) action(teacher, student)
    }
}


code:
val myClass = new MathClass(List(("Foo", "Bar"), ("Baz", "Smith")), List(("Baz", "Qux"), ("Wooble", "Ninja")))
myClass foreachTeacherStudentCombination {
    case ((tFirst, tLast), (sFirst, sLast)) =>
        if (tFirst == sFirst || tLast == sLast || tFirst == sLast || tLast == sFirst) {
            Console println "Whoa!  Similar names.... groovy!"
        }
        else {
            val tName = tFirst + " " + tLast
            val sName = sFirst + " " + sLast

            Console println (tName + " teaches " + sName + ".")
        }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 21 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: