Permalink
Browse files

added some junk on functions

  • Loading branch information...
1 parent bd525d6 commit 08e47be37d3272b5f7276e87da297fd3ef099d47 @derekwyatt committed Oct 24, 2010
Showing with 37 additions and 0 deletions.
  1. +37 −0 beginning-scala/functions.scala
@@ -0,0 +1,37 @@
+def identity(i: Int) = i
+
+def funcParam1(i: Int, func: Int => Int) = "Func returns: " + func(i)
+
+def funcParam2(i: Int)(func: Int => Int) = "Func returns: " + func(i)
+
+def forPartialFunc1(i: Int, j: Int, k: Int) = i + j + k
+
+def forPartialFunc2(i: Int, j: Int)(k: Int) = i + j + k
+
+println("identity(5) = " + identity(5))
+println("funcParam1(5, r) = " + funcParam1(5, identity))
+println("funcParam1(5, {code}) = " + funcParam1(5, { i => i * i }))
+println("funcParam2(5, {code}) = " + funcParam2(5)({ i => i * i }))
+println("funcParam1(5, forPartialFunc1(1, 2, _)) = " + funcParam1(5, forPartialFunc1(1, 2, _)))
+
+def partial = forPartialFunc2(1, 2) _
+
+println("funcParam1(5, partial) = " + funcParam1(5, partial))
+
+def typeIdentity[T](t: T): T = t
+
+println("typeIdentity[String]('Hithere') = " + typeIdentity("Hithere"))
+
+class MySomeClass(val s: String) { override def toString = "\"" + s + "\"" }
+
+println("typeIdentity[MySomeClass](...) = " + typeIdentity(new MySomeClass("MySomeClass Right Here")))
+
+def typeFunction[T, U](i: T, f: T => U) = {
+ println("Running typeFunction: Calling f(i)")
+ println(f(i).toString)
+ println("Running typeFunction: Done f(i)")
+}
+
+def toBeCalled(i: Int) = "toBeCalled returning " + i
+
+typeFunction(200, toBeCalled)

0 comments on commit 08e47be

Please sign in to comment.