Created
April 7, 2015 16:00
-
-
Save shigemk2/07373fe83a13c8b91f7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ ~ scala | |
Welcome to Scala version 2.11.6 (OpenJDK 64-Bit Server VM, Java 1.7.0_75). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> def partial1[A,B,C](a: A, f: (A,B) => C): B => C = | |
| (b: B) => f(a, b) | |
partial1: [A, B, C](a: A, f: (A, B) => C)B => C | |
scala> partial1(3, (x: Int, y: Int => x + y)(3) | |
| ) | |
<console>:9: error: not found: value x | |
partial1(3, (x: Int, y: Int => x + y)(3) | |
^ | |
<console>:9: error: not found: type + | |
partial1(3, (x: Int, y: Int => x + y)(3) | |
^ | |
<console>:9: error: not found: value y | |
partial1(3, (x: Int, y: Int => x + y)(3) | |
^ | |
scala> partial1(3, (x: Int, y: Int => x + y))(3) | |
<console>:9: error: not found: value x [847/870] | |
partial1(3, (x: Int, y: Int => x + y))(3) | |
^ | |
<console>:9: error: not found: type + | |
partial1(3, (x: Int, y: Int => x + y))(3) | |
^ | |
<console>:9: error: not found: value y | |
partial1(3, (x: Int, y: Int => x + y))(3) | |
^ | |
scala> partial1(3, (x: Int, y: Int) => x + y)(3) | |
res2: Int = 6 | |
scala> def curry[A,B,C](f: (A, B) => C): A => (B => C) = | |
| a => b => f(a, b) | |
curry: [A, B, C](f: (A, B) => C)A => (B => C) | |
scala> | |
scala> curry(3, (x: Int, y: Int) => x + y)(3) | |
<console>:9: error: too many arguments for method curry: (f: (A, B) => C)A => (B => C) | |
curry(3, (x: Int, y: Int) => x + y)(3) | |
^ | |
scala> curry((x: Int, y: Int) => x + y)(2)(3) [823/870] | |
res4: Int = 5 | |
scala> curry((x: Int, y: Int) => x + y)(2)(3) | |
scala> curry((x: Int, y: Int) => x + y)(2)(3) | |
res5: Int = 5 | |
scala> curry((x: Int, y: Int) => x + y)(2)(3) | |
res6: Int = 5 | |
scala> curry((x: Int, y: Int) => x + y)(4)(19) | |
res7: Int = 23 | |
scala> def uncurry[A,B,C](f: A => B => C): (A, B) => C = | |
| (a, b) => f(a)(b) | |
uncurry: [A, B, C](f: A => (B => C))(A, B) => C | |
scala> | |
scala> curry((x: Int, y: Int) => x + y)(4, 19) | |
<console>:9: error: too many arguments for method apply: (v1: Int)Int => Int in trait Function1 | |
curry((x: Int, y: Int) => x + y)(4, 19) | |
scala> uncurry((x: Int, y: Int) => x + y)(4, 19) [798/870] | |
<console>:9: error: type mismatch; | |
found : (Int, Int) => Int | |
required: ? => (? => ?) | |
uncurry((x: Int, y: Int) => x + y)(4, 19) | |
^ | |
scala> uncurry(x: Int => y: Int => x + y)(2, 3) | |
<console>:1: error: ')' expected but ':' found. | |
uncurry(x: Int => y: Int => x + y)(2, 3) | |
^ | |
scala> uncurry((x: Int) => (y: Int) => x + y)(2, 3) | |
res10: Int = 5 | |
scala> uncurry((x: Int) => (y: Int) => x + y)(4, 3) | |
res11: Int = 7 | |
scala> def compose[A,B,C](f: B => C, g: A => B): A => C = | |
| a => f(g(a)) | |
compose: [A, B, C](f: B => C, g: A => B)A => C | |
scala> | |
scala> def compose[A,B,C](f: B => C, g: A => B): A => C = [774/870] | |
| a => f(g(a)) | |
compose: [A, B, C](f: B => C, g: A => B)A => C | |
scala> compose((x: Int) => x + 2, (y: Int) => y * 2)(3) | |
res12: Int = 8 | |
scala> uncurry((x: Int) => (y: Int) => x + y)(4, 3) | |
res13: Int = 7 | |
scala> curry((x: Int, y: Int) => x + y)(4, 19) | |
<console>:9: error: too many arguments for method apply: (v1: Int)Int => Int in trait Function1 | |
curry((x: Int, y: Int) => x + y)(4, 19) | |
^ | |
scala> curry((x: Int, y: Int) => x + y)(4)(19) | |
res15: Int = 23 | |
scala> val f (x: Double) => math.Pi / 2 -x | |
<console>:1: error: '=' expected but '=>' found. | |
val f (x: Double) => math.Pi / 2 -x | |
^ | |
scala> val f (x: Double) => math.Pi / 2 - x [751/870] | |
<console>:1: error: '=' expected but '=>' found. | |
val f (x: Double) => math.Pi / 2 - x | |
^ | |
scala> val f = (x: Double) => math.Pi / 2 - x | |
f: Double => Double = <function1> | |
scala> f andThen math.sin | |
res16: Double => Double = <function1> | |
scala> def f(x: Double) => math.Pi / 2 - x | |
<console>:1: error: '=' expected but '=>' found. | |
def f(x: Double) => math.Pi / 2 - x | |
^ | |
scala> def f(x: Double): Double => math.Pi / 2 - x | |
<console>:1: error: identifier expected but integer literal found. | |
def f(x: Double): Double => math.Pi / 2 - x | |
^ | |
scala> def f(x: Double): Double = math.Pi / 2 - x | |
f: (x: Double)Double | |
scala> f(2) [727/870] | |
res17: Double = -0.42920367320510344 | |
scala> f andThen math.sin | |
<console>:9: error: missing arguments for method f; | |
follow this method with `_' if you want to treat it as a partially applied function | |
f andThen math.sin | |
^ | |
<console>:9: error: missing arguments for method sin in package math; | |
follow this method with `_' if you want to treat it as a partially applied function | |
f andThen math.sin | |
^ | |
scala> f _ andThen math.sin | |
res19: Double => Double = <function1> | |
scala> f 2 andThen math.sin | |
<console>:1: error: ';' expected but integer literal found. | |
f 2 andThen math.sin | |
^ | |
scala> f _ andThen math.sin | |
res20: Double => Double = <function1> | |
scala> f _ [703/870] | |
res21: Double => Double = <function1> | |
scala> f | |
<console>:9: error: missing arguments for method f; | |
follow this method with `_' if you want to treat it as a partially applied function | |
f | |
^ | |
scala> f 2 | |
<console>:1: error: ';' expected but integer literal found. | |
f 2 | |
^ | |
scala> f(2) | |
res23: Double = -0.42920367320510344 | |
scala> f _ | |
res24: Double => Double = <function1> | |
scala> val g = f _ | |
g: Double => Double = <function1> | |
scala> g(2) [680/870] | |
res25: Double = -0.42920367320510344 | |
scala> | |
scala> f _ andThen math.sin | |
res26: Double => Double = <function1> | |
scala> val g = f _ andThen math.sin | |
g: Double => Double = <function1> | |
scala> g(2) | |
res27: Double = -0.41614683654714246 | |
scala> val g = f andThen math.sin | |
<console>:8: error: missing arguments for method f; | |
follow this method with `_' if you want to treat it as a partially applied function | |
val g = f andThen math.sin | |
^ | |
<console>:8: error: missing arguments for method sin in package math; | |
follow this method with `_' if you want to treat it as a partially applied function | |
val g = f andThen math.sin | |
^ | |
scala> val g = f _ andThen math.sin [656/870] | |
g: Double => Double = <function1> | |
scala> def f(x: Double): Double = math.Pi / 2 - x | |
f: (x: Double)Double | |
scala> val g = f _ andThen math.sin | |
g: Double => Double = <function1> | |
scala> g(2) | |
res28: Double = -0.41614683654714246 | |
scala> | |
scala> | |
scala> object List { // `List` companion object. Contains functions for creating and working with lists. | |
| def sum(ints: List[Int]): Int = ints match { // A function that uses pattern matching to add up a list of | |
integers | |
| case Nil => 0 // The sum of the empty list is 0. | |
| case Cons(x,xs) => x + sum(xs) // The sum of a list starting with `x` is `x` plus the sum of the rest of | |
the list. | |
| } | |
| [633/870] | |
| def product(ds: List[Double]): Double = ds match { | |
| case Nil => 1.0 | |
| case Cons(0.0, _) => 0.0 | |
| case Cons(x,xs) => x * product(xs) | |
| } | |
| | |
| def apply[A](as: A*): List[A] = // Variadic function syntax | |
| if (as.isEmpty) Nil | |
| else Cons(as.head, apply(as.tail: _*)) | |
| } | |
<console>:10: error: not found: value Cons | |
case Cons(x,xs) => x + sum(xs) // The sum of a list starting with `x` is `x` plus the sum of the rest of | |
the list. | |
^ | |
<console>:15: error: not found: value Cons | |
case Cons(0.0, _) => 0.0 | |
^ | |
<console>:16: error: not found: value Cons | |
case Cons(x,xs) => x * product(xs) | |
^ | |
<console>:21: error: not found: value Cons | |
else Cons(as.head, apply(as.tail: _*)) | |
scala> sealed trait List[+A] // `List` data type, parameterized on a type, `A` [608/870] | |
defined trait List | |
scala> case object Nil extends List[Nothing] // A `List` data constructor representing the empty list | |
defined object Nil | |
scala> case class Cons[+A](head: A, tail: List[A]) extends List[A] // Another data constructor, representing nonemp | |
ty lists. Note that `tail` is another `List[A]`, which may be `Nil` or another `Cons`. | |
defined class Cons | |
scala> object List { // `List` companion object. Contains functions for creating and working with lists. | |
| def sum(ints: List[Int]): Int = ints match { // A function that uses pattern matching to add up a list of | |
integers | |
| case Nil => 0 // The sum of the empty list is 0. | |
| case Cons(x,xs) => x + sum(xs) // The sum of a list starting with `x` is `x` plus the sum of the rest of | |
the list. | |
| } | |
| | |
| def product(ds: List[Double]): Double = ds match { | |
| case Nil => 1.0 | |
| case Cons(0.0, _) => 0.0 | |
| case Cons(x,xs) => x * product(xs) | |
| } | |
| [585/870] | |
| def apply[A](as: A*): List[A] = // Variadic function syntax | |
| if (as.isEmpty) Nil | |
| else Cons(as.head, apply(as.tail: _*)) | |
| | |
| } | |
defined object List | |
warning: previously defined trait List is not a companion to object List. | |
Companions must be defined together; you may wish to use :paste mode for this. | |
scala> List.sum((1,2,3,4,5)) | |
<console>:13: error: type mismatch; | |
found : (Int, Int, Int, Int, Int) | |
required: List[Int] | |
List.sum((1,2,3,4,5)) | |
^ | |
scala> List.sum(List(1,2,3,4,5)) | |
res30: Int = 15 | |
scala> List.product(List(1,2,3,4,5)) | |
res31: Double = 120.0 | |
scala> List(List(1,2,3,4,5)) [562/870] | |
res32: List[List[Int]] = Cons(Cons(1,Cons(2,Cons(3,Cons(4,Cons(5,Nil))))),Nil) | |
scala> val ex1: List | |
scala> val ex1: List[Double] = Nil | |
ex1: List[Double] = Nil | |
scala> val ex2: List[Int] = Cons(1, Nil) | |
ex2: List[Int] = Cons(1,Nil) | |
scala> val ex3: List[String] = Cons("a", Cons("b", Nil)) | |
ex3: List[String] = Cons(a,Cons(b,Nil)) | |
scala> val ex1: List[Double] = Nil | |
ex1: List[Double] = Nil | |
scala> :quit | |
➜ ~ scala | |
Welcome to Scala version 2.11.6 (OpenJDK 64-Bit Server VM, Java 1.7.0_75). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> sealed trait List[A] [539/870] | |
defined trait List | |
scala> case object Nil extends List[Nothing] | |
defined object Nil | |
scala> case class Cons[+A](head: A, tail: List[A]) extends List[A] | |
<console>:8: error: covariant type A occurs in invariant position in type [+A]AnyRef | |
with List[A] | |
with Product | |
with Serializable { | |
val head: A | |
private[this] val head: A | |
val tail: List[A] | |
private[this] val tail: List[A] | |
def <init>(head: A,tail: List[A]): Cons[A] | |
def copy[A](head: A,tail: List[A]): Cons[A] | |
def copy$default$1[A]: A @scala.annotation.unchecked.uncheckedVariance | |
def copy$default$2[A]: List[A] @scala.annotation.unchecked.uncheckedVariance | |
override def productPrefix: String | |
def productArity: Int | |
def productElement(x$1: Int): Any | |
override def productIterator: Iterator[Any] | |
def canEqual(x$1: Any): Boolean [516/870] | |
override def hashCode(): Int | |
override def toString(): String | |
override def equals(x$1: Any): Boolean | |
} of class Cons | |
case class Cons[+A](head: A, tail: List[A]) extends List[A] | |
^ | |
<console>:8: error: covariant type A occurs in invariant position in type => List[A] of value tail | |
case class Cons[+A](head: A, tail: List[A]) extends List[A] | |
^ | |
scala> case class Cons[A](head: A, tail: List[A]) extends List[A] | |
defined class Cons | |
scala> val ex1: List[Int] = Nil | |
<console>:9: error: type mismatch; | |
found : Nil.type | |
required: List[Int] | |
Note: Nothing <: Int (and Nil.type <: List[Nothing]), but trait List is invariant in type A. | |
You may wish to define A as +A instead. (SLS 4.5) | |
val ex1: List[Int] = Nil | |
^ | |
scala> case object Nil[A] extends List[A] | |
<console>:1: error: ';' expected but '[' found. | |
case object Nil[A] extends List[A] | |
^ | |
scala> case object Nil(A) extends List[A] | |
<console>:1: error: traits or objects may not have parameters | |
case object Nil(A) extends List[A] | |
^ | |
scala> case object Nil(A) extends List[A]% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment