请选择 进入手机版 | 继续访问电脑版
楼主: ReneeBK
760 6

[Lecture Notes]Cross-Paradigm Programming with Scala [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49392 个
通用积分
51.6904
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

ReneeBK 发表于 2016-3-31 10:24:46 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
HandoutsAssignmentsReferences
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Programming Paradigm Lecture Program notes Software

本帖被以下文库推荐

ReneeBK 发表于 2016-3-31 10:26:09 |显示全部楼层 |坛友微信交流群
  1. import scala.Random

  2. type Color = Int
  3. type Game = List[Int] => (Int, Int)

  4. /* Question: why would writing
  5. *
  6. *     def rgen = new Random
  7. *
  8. * be a bad idea?
  9. */
  10. val rgen = new Random

  11. /* Computes the set of all possible secret codes of length numPegs
  12. * containing colors (0 until numColors), using a for-comprehension.
  13. */
  14. def initialSpace(numPegs: Int, numColors: Int): Set[List[Int]] = {
  15.     if (numPegs == 0) Set(List())
  16.     else for { code  <- initialSpace(numPegs - 1, numColors)
  17.                color <- (0 until numColors) }
  18.          yield color :: code
  19. }

  20. /* Returns the Mastermind rating of "guess" c1 with respect to "code" c2,
  21. * or, equivalently, "guess" c2 with respect to "code" c1.  These game
  22. * role terms are actually meaningless.
  23. */
  24. def rate(c1: List[Color], c2: List[Color]): (Int, Int) = {
  25.     val exact = (c1 zip c2) count (pair => pair._1 == pair._2)
  26.     val mins = Set(c1:_*).toList.map(c => c1.count(c==) min c2.count(c==))
  27.     val sum = mins.foldRight(0)(_+_)
  28.     return (exact, sum - exact)
  29. }

  30. /* Picks a random secret code and returns a partially-applied version of
  31. * the rate function which may be used to compare guesses against the
  32. * secret code.  Note that the code itself never escapes from the scope of
  33. * startGame (except via println).
  34. */
  35. def startGame(numPegs: Int, numColors: Int): Game = {
  36.     val code = List.tabulate(numPegs, x => rgen.nextInt(numColors))
  37.     println("Secret code: " + code)
  38.     (guess: List[Color]) => {
  39.         println("You guessed: " + guess)
  40.         rate(code, guess)
  41.     }
  42. }

  43. /* Picks the next guess randomly from the space of possible guesses.
  44. */
  45. def nextGuess(space: Set[List[Int]]) =
  46.     space.toList(rgen.nextInt(space.size))

  47. /* The main search loop.  Recursively calls itself until the guess space
  48. * is empty.  Arm's length recursion would be required to return the final
  49. * guess; instead, this implementation relies on the game function to
  50. * print guesses along the way.
  51. */
  52. def iterate(game: Game, space: Set[List[Int]]) {
  53.     if (space.size > 0) {
  54.         val guess = nextGuess(space)
  55.         val rating = game(guess)
  56.         val filtered = (space - guess).filter(rate(_, guess) == rating)
  57.         iterate(game, filtered)
  58.     }
  59. }

  60. /* The main entry point.  E.g., playGame(4, 6) will conduct an instance of
  61. * the traditional Mastermind game.
  62. */
  63. def playGame(numPegs: Int, numColors: Int) =
  64.     iterate(startGame(numPegs, numColors),
  65.             initialSpace(numPegs, numColors))
复制代码

使用道具

ReneeBK 发表于 2016-3-31 10:26:45 |显示全部楼层 |坛友微信交流群
  1. class Rational(x: Int, y: Int) {
  2.   private def gcd(a: Int, b: Int): Int =
  3.     if (b == 0) a else gcd(b, a % b)

  4.   private val g = gcd(x, y)

  5.   val numer = x / g
  6.   val denom = y / g

  7.   def +(that: Rational) =
  8.     new Rational(numer * that.denom + that.numer * denom,
  9.       denom * that.denom)

  10.   def -(that: Rational) =
  11.     new Rational(numer * that.denom - that.numer * denom,
  12.       denom * that.denom)

  13.   def *(that: Rational) =
  14.     new Rational(numer * that.numer, denom * that.denom)

  15.   def /(that: Rational) =
  16.     new Rational(numer * that.denom, denom * that.numer)

  17.   override def toString = x + "/" + y
  18. }

  19. trait Ordered[T] {
  20.   def compare(that: T): Int

  21.   def <(that: T) = (this compare that) < 0
  22.   def >(that: T) = (this compare that) > 0
  23.   def <=(that: T) = (this compare that) <= 0
  24.   def >=(that: T) = (this compare that) >= 0
  25. }

  26. class OrderedRational(x: Int, y: Int) extends Rational(x, y) with
  27.   Ordered[OrderedRational] {

  28.   def compare(that: OrderedRational) =
  29.     (numer * that.denom) compare (denom * that.numer)
  30. }

  31. object Rational {
  32.   def apply(x: Int, y: Int) = new OrderedRational(x, y)
  33.   def apply(x: Int) = new OrderedRational(x, 1)
  34. }

  35. val a = Rational(1, 2)
  36. val b = Rational(1)

  37. println(a + " + " + b + " = " + (a + b))
  38. println(a + " - " + b + " = " + (a - b))
  39. println(a + " * " + b + " = " + (a * b))
  40. println(a + " / " + b + " = " + (a / b))
  41. println(a + " < " + b + " = " + (a < b))
  42. println(a + " > " + b + " = " + (a > b))
  43. println(a + " <= " + b + " = " + (a <= b))
  44. println(a + " >= " + b + " = " + (a >= b))
复制代码

使用道具

ReneeBK 发表于 2016-3-31 10:27:39 |显示全部楼层 |坛友微信交流群
  1. abstract class Buffer {
  2.   type T
  3.   val element: T
  4. }

  5. abstract class SeqBuffer extends Buffer {
  6.   type U
  7.   type T <: Seq[U]
  8.   def length = element.length
  9. }

  10. // IntSeqBuffer is abstract because element is abstract
  11. abstract class IntSeqBuffer extends SeqBuffer {
  12.   type U = Int
  13. }

  14. // implement IntSeqBuffer using a List as the sequence
  15. class IntListBuffer extends IntSeqBuffer {
  16.   type T = List[U] // or List[Int], U being an alias to Int
  17.   val element = List(1, 2, 3)
  18. }

  19. // because of compound types, it's not unusual to use "anonymous" classes
  20. // to implement abstract classes at instantiation time
  21. new IntSeqBuffer {
  22.   type T = List[U] // or List[Int], U being an alias to Int
  23.   val element = List(1, 2, 3)
  24. }

  25. // Buffer is abstract because element is abstract
  26. abstract class Buffer[T] {
  27.   val element: T
  28. }

  29. // implementing the abstract class at instantiation time
  30. new Buffer[Int] {
  31.   val element = 42
  32. }

  33. abstract class SeqBuffer[U, T <: Seq[U]] extends Buffer[T] {
  34.   def length = element.length
  35. }

  36. class IntListBuffer extends SeqBuffer[Int, List[Int]] {
  37.   val element = List(1, 2, 3)
  38. }
复制代码

使用道具

ReneeBK 发表于 2016-3-31 10:28:37 |显示全部楼层 |坛友微信交流群
  1. class Button {
  2.   abstract class Event
  3.   class Click extends Event
  4. }

  5. val button1 = new Button
  6. val button2 = new Button

  7. val click1: button1.Click = new button1.Click
  8. val click2: button2.Click = new button2.Click

  9. // type error
  10. val click3: button1.Click = click2
复制代码

使用道具

ReneeBK 发表于 2016-3-31 10:29:13 |显示全部楼层 |坛友微信交流群
  1. public class InnerClass {
  2.   public static void main(String[] args) {
  3.     A a1 = new A();
  4.     Class c1 = (a1.new B()).getClass(); // wierd syntax by the way
  5.     System.out.println(c1); // prints A$B
  6.    
  7.     A a2 = new A();
  8.     Class c2 = (a2.new B()).getClass();
  9.     System.out.println(c2); // prints A$B
  10.    
  11.     // types are equal? yes
  12.     System.out.println(c1.equals(c2));

  13.     // how to refer to the type of this expression? A$B doesn't work
  14.     a2.new B();
  15.   }
  16. }

  17. class A {
  18.   public class B {
  19.   }
  20.   public static class C {
  21.   }
  22. }
复制代码

使用道具

ReneeBK 发表于 2016-3-31 10:30:07 |显示全部楼层 |坛友微信交流群
  1. trait Iterable[T] {
  2.   def map[A](f: T => A): Iterable[A]
  3.   def filter(p: T => Boolean): Iterable[T]
  4.   def remove(p: T => Boolean): Iterable[T] =
  5.     filter(x => !p(x))
  6. }

  7. // copy-pasting required
  8. trait List[T] extends Iterable[T] {
  9.   def map[A](f: T => A): Iterable[A]
  10.   def filter(p: T => Boolean): List[T]
  11.   def remove(p: T => Boolean): List[T] =
  12.     filter(x => !p(x))
  13. }

  14. // U is the type of a generic container
  15. trait Iterable[T, U[x]] {
  16.   def map[A](f: T => A): U[A]
  17.   def def filter(p: T => Boolean): U[T]
  18.   def remove(p: T => Boolean): U[T] =
  19.     filter(x => !p(x))
  20. }

  21. // List is the type of the container
  22. trait List[T] extends Iterable[T, List]
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-3-29 22:05