reactive programming 1.4 Functional Random Generators

  1. 整数生成器
package week2

trait Generator[+T] {
  def generate: T
}
object Test extends App {

  val integers = new Generator[Int] {
    val random = new java.util.Random()
    def generate = random.nextInt()
  }
  print (integers.generate)
}

2.布尔值生成器

  val bools = new Generator[Boolean] {
    def generate =  integers.generate > 0
  }
  for (i <- 1 to 10)  println (bools.generate)

3. Pair生成器

  val pairs = new Generator[(Int, Int)] {
    def generate = (integers.generate, integers.generate)
  }
  
  for (i <- 1 to 10)  println (pairs.generate)

4. 能不能更简洁些,每次都用 new Generator很麻烦

如果能这样岂不很好

  val booleans = for (x <- integers) yield x > 0
  def pairs[T, U](t: Generator[T], u: Generator[U]) = for {
    x <- t
    y <- u
  } yield (x, y)

通过上节我们知道for循环没翻译成map,flatMat,filter来执行,要想在for循环中用integers就得定义map函数

package week2

trait Generator[+T] {
  self =>
  def generate: T
  def map[S](f: T => S):Generator[S] = new Generator[S] {
    def generate = f(self.generate)
  }
}
object Test extends App {

  val integers = new Generator[Int] {
    val random = new java.util.Random()
    def generate = random.nextInt()
  }

  val bools = for (x <- integers) yield x > 0
  println( bools.generate)
}

5. Pair生成器也可以这样生成

时间: 2024-08-26 15:27:19

reactive programming 1.4 Functional Random Generators的相关文章

Functional reactive programming introduction using ReactiveCocoa中文版

今天发现了Functional reactive programming introduction using ReactiveCocoa - By AshFurrow的中文翻译版,译者为: kevinHM,以下为github地址: https://github.com/KevinHM/FunctionalReactiveProgrammingOniOS 感谢原作者和译者的贡献!

ReactiveCocoa与Functional Reactive Programming

什么是Functional Reactive Programming http://limboy.me/ios/2013/06/19/frp-reactivecocoa.html Functional Reactive Programming(以下简称FRP)是一种响应变化的编程范式.先来看一小段代码 a = 2 b = 2 c = a + b // c is 4 b = 3 // now what is the value of c? 如果使用FRP,c的值将会随着b的值改变而改变,所以叫做「

Unity基于响应式编程(Reactive programming)入门

系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoTween&Unity Native2D实现 时光煮雨 Unity3D实现2D人物动画① UGUI&Native2D序列帧动画 时光煮雨 Unity3D实现2D人物动画② Unity2D 动画系统&资源效率 背景 前有慕容小匹夫的一篇<解构C#游戏框架uFrame兼谈游戏架构设计&

[RxJS] Reactive Programming - What is RxJS?

First thing need to understand is, Reactive programming is dealing with the event stream. Event streams happens overtime, which not stay in the memory. For example, the array we have: var source = ['1', '1', 'foo', '2', '3', '5', 'bar', '8', '13']; W

[Reactive Programming] RxJS dynamic behavior

This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm for programming. See how reactive programming helps you understand the dynamic behavior of a value evolving over time. It allows you to specify the dyna

&quot;Principles of Reactive Programming&quot; 之&lt;Actors are Distributed&gt; (1)

week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Cluster自身如何工作的细节,而是更关注于如何利用Akka Cluster来把Actor分布到不同的节点上,或许这么安排是因为Akka Cluster能讲的东西太多,而Coursera的课时不够.但是,从听众的角度来说,这节课只是初步明白了下Akka Cluster能干啥,但是想要自己用起来,特别是想了解其工作的

响应式编程(Reactive programming)

响应式编程是指确保程序对事件或输入做出响应的做法.在这一节,我们将专注于图形界面方面的响应式编程,图形界面总量响应式的.然而,其他网格的编程也需要考虑响应式编程,例如,运行在服务器上的程序总是需要保持对输入作出响应,即使是在它处理其他需要长时间运行的任务期间.我们将在第十一章实现聊天服务器时,会看到在服务器编程方面也要用到这里讨论的一些方法. 大多数图形界面库使用事件循环去处理绘制图形界面,以及与用户之间的交互,就是说,一个线程既要负责绘制图形界面,也要处理其上的所有事件,我们称这种线程为图形界

&quot;reactive programming&quot;的概念

下面的内容大多是翻译来的. Reactive Programming? What is Reactive Programming? 为了了解Reactive——从编程范式至其背后的动机,有必要了解现在的开发者和公司在十年前不曾面对的挑战. 游戏的改变主要有两大方面: 硬件的提升 因特网 Why things are different now? (原文对比了一遍2005年和2014年因特用户的增长:10亿至29.5亿:Facebook用户的增长:550万至13亿: twitter从无至有,0至2

reactive programming

In computing, reactive programming is a declarative programming paradigm concerned with data streams and the propagation of change. programming around data flows an he propagation of change. 原文地址:https://www.cnblogs.com/feng9exe/p/8613384.html