小编Saj*_*lva的帖子

scala匿名函数上的By-name参数

我正在努力用by-name参数编写一个匿名函数.这就是我累了.

val fun = (x: Boolean, y: =>Int) => if(x) y else 0
Run Code Online (Sandbox Code Playgroud)

这失败并出现以下错误.

Error:(106, 31) identifier expected but '=>' found.
    val fun = (x: Boolean, y: =>Int) => if(x) y else 0
                              ^
Error:(109, 3) ')' expected but '}' found.
  }
  ^
Run Code Online (Sandbox Code Playgroud)

与标准函数相同的代码是如何工作的.

  def fun1(x: Boolean, y: =>Int) = if(x) y else 0
Run Code Online (Sandbox Code Playgroud)

有什么指针吗?

- - - - - - - -编辑 - - - - - - - - -

我有两个问题.senia的答案解决了最初的案例.假设我有一个函数需要一个函数.

  def xxx[A,B](f:(A,=>B)=>B)={}
Run Code Online (Sandbox Code Playgroud)

根据senia解决方案,它的工作原理.

val fun: (Int, =>Boolean) …
Run Code Online (Sandbox Code Playgroud)

scala

9
推荐指数
1
解决办法
1245
查看次数

流畅的光滑/ scala

我正在看scala/slick流,并试图了解它是如何工作的.这是我的测试代码

    val bigdata = TableQuery[BigData] 
    val x = db.stream(bigdata.result.transactionally.withStatementParameters(fetchSize = 100)).foreach {
      (tuple: (Int, UUID)) =>
        println(tuple._1 + " " + tuple._2)
        Thread.sleep(50)//emulating slow consumer.
    }

    Await.result(x, 100000 seconds)
Run Code Online (Sandbox Code Playgroud)

在代码运行时,我启用了postgresql查询日志,以了解最新情况.我看到每100个元素发生一次重新查询

2015-11-06 15:03:24 IST [24379-3] postgres @scala_test日志:执行从S_2/C_3获取:选择x2."id",x2."data"来自"bigdata"x2 2015-11-06 15:03:29 IST [24379-4] postgres @ scala_test日志:执行

fetch from S_2/C_3: select x2."id", x2."data" from "bigdata" x2
2015-11-06 15:03:34 IST [24379-5] postgres@scala_test LOG:  execute fetch from S_2/C_3: select x2."id", x2."data" from "bigdata" x2
2015-11-06 15:03:39 IST [24379-6] postgres@scala_test LOG:  execute fetch from S_2/C_3: select x2."id", …
Run Code Online (Sandbox Code Playgroud)

streaming scala reactive-programming slick

7
推荐指数
0
解决办法
517
查看次数

斯卡拉蛋糕模式的重要性

我已经开始学习scala一段时间了,现在看着蛋糕模式.我从这里得到了例子

trait UserRepositoryComponent {
  def userLocator: UserLocator

  trait UserLocator {
    def findAll: List[User]
  }
}

trait UserRepositoryJPAComponent extends UserRepositoryComponent {
  val em: EntityManager

  def userLocator = new UserLocatorJPA(em)

  class UserLocatorJPA(val em: EntityManager) extends UserLocator {
    def findAll = {
      println("Executing a JPA query")
      List(new User, new User)
    }
  }
}

trait UserServiceComponent {
  def userService: UserService

  trait UserService {
    def findAll: List[User]
  }
}

trait DefaultUserServiceComponent extends UserServiceComponent {
  this: UserRepositoryComponent =>

  def userService = new DefaultUserService

  class …
Run Code Online (Sandbox Code Playgroud)

dependency-injection scala cake-pattern

6
推荐指数
1
解决办法
377
查看次数

golang git拉回购

我对golang非常陌生,我正尝试从go程序中进行git pull。我查看了本机库,发现https://github.com/src-d/go-git/

我具有克隆等功能。但不拉。从源头看,似乎还有一个拉动功能

func (r *Repository) Pull(o *PullOptions) 
Run Code Online (Sandbox Code Playgroud)

但是编译器警告其未定义。谁能指出我该怎么做,或指向同时支持clone和pull的替代库?

git pull go

3
推荐指数
1
解决办法
3914
查看次数

如何对 ActorPublisher 进行反压

我正在编写一些示例来理解 akka 流和背压。我正在尝试了解缓慢的消费者背压对 AkkaPublisher 有何影响

我的代码如下。

class DataPublisher extends ActorPublisher[Int] {

  import akka.stream.actor.ActorPublisherMessage._

  var items: List[Int] = List.empty

  def receive = {
    case s: String =>
      println(s"Producer buffer size ${items.size}")
      if (totalDemand == 0)
        items = items :+ s.toInt
      else
        onNext(s.toInt)

    case Request(demand) =>
      if (demand > items.size) {
        items foreach (onNext)
        items = List.empty
      }
      else {
        val (send, keep) = items.splitAt(demand.toInt)
        items = keep
        send foreach (onNext)
      }


    case other =>
      println(s"got other $other")
  }
}
Run Code Online (Sandbox Code Playgroud)

Source.fromPublisher(ActorPublisher[Int](dataPublisherRef)).runWith(sink) …
Run Code Online (Sandbox Code Playgroud)

scala reactive-programming akka reactive-streams akka-stream

1
推荐指数
1
解决办法
758
查看次数