小编DrP*_*per的帖子

Coffeescript将函数包装在一个函数中

出于某种原因,coffeescript编译器在编译时将所有.coffee文件包装在函数中.例如,如果我有test.coffee:

class TestClass
    constructor: (@value) ->

    printValue: () ->
        alert(@value)

printAValue = () -> 
    test = new TestClass()
    test.printValue()
Run Code Online (Sandbox Code Playgroud)

然后我得到test.js:

(function() {
  var TestClass, printAValue;
  TestClass = (function() {
    function TestClass(value) {
      this.value = value;
    }
    TestClass.prototype.printValue = function() {
      return alert(this.value);
    };
    return TestClass;
  })();
  printAValue = function() {
    var test;
    test = new TestClass();
    return test.printValue();
  };
}).call(this);
Run Code Online (Sandbox Code Playgroud)

我的简单html文件不适用于此:

<html>
    <head>
        <script src="test.js"></script>
    </head>
    <body onload="printAValue()">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我之前没有使用过很多JS,我不会怀疑咖啡编译器,但它应该如何工作?怎么样

javascript coffeescript

8
推荐指数
2
解决办法
4922
查看次数

概括Scala类型的问题

在处理使用Type Class模式的Scala项目时,我遇到了语言如何实现模式的严重问题:由于Scala类型类实现必须由程序员而不是语言管理,因此任何变量属于类类的类永远不会被注释为父类型,除非它采用类型类实现.

为了说明这一点,我编写了一个快速示例程序.想象一下,您正在尝试编写一个程序,可以为公司处理不同类型的员工,并可以打印有关其进度的报告.要使用Scala中的类型类模式解决此问题,您可以尝试这样的方法:

abstract class Employee
class Packer(boxesPacked: Int, cratesPacked: Int) extends Employee
class Shipper(trucksShipped: Int) extends Employee
Run Code Online (Sandbox Code Playgroud)

一个类层次结构模拟不同类型的员工,足够简单.现在我们实现ReportMaker类型类.

trait ReportMaker[T] {
    def printReport(t: T): Unit
}

implicit object PackerReportMaker extends ReportMaker[Packer] {
    def printReport(p: Packer) { println(p.boxesPacked + p.cratesPacked) }
}

implicit object ShipperReportMaker extends ReportMaker[Shipper] {
    def printReport(s: Shipper) { println(s.trucksShipped) }
}
Run Code Online (Sandbox Code Playgroud)

这一切都很好,我们现在可以编写一些可能如下所示的Roster类:

class Roster {
    private var employees: List[Employee] = List()

    def reportAndAdd[T <: Employee](e: T)(implicit rm: ReportMaker[T]) {
       rm.printReport(e)
       employees = employees :+ e
    } …
Run Code Online (Sandbox Code Playgroud)

scala typeclass

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

如何在buildr项目中使用Ruby代码?

如何在构建器项目中使用Ruby?

我在很多单独的项目中使用过Ruby,JRuby,Java和Clojure.我目前正在使用标准Ruby中的模拟应用程序,我想尝试使用Clojure后端(我喜欢功能代码),以及JRuby gui和测试套件.我还可以看到,将Scala用作未来不同项目的后端.

我想我会为我的项目尝试buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目本身内部使用JRuby代码!这看起来有点愚蠢,因为该工具旨在统一常见的JVM语言并且是 ruby 构建.

有没有人知道如何实现这一目标,除了将输出的jar包含在一个独特的红宝石项目中?

ruby scala clojure buildr

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

Scala中的并发Akka代理

我正在研究一个scala项目,我决定使用Akka的代理库而不是actor模型,因为它允许更多功能性的并发方法.但是,我遇到了运行许多不同代理的问题.时间.看起来我只能同时运行三到四个代理.

import akka.actor._
import akka.agent._
import scala.concurrent.ExecutionContext.Implicits.global

object AgentTester extends App {
// Create the system for the actors that power the agents
implicit val system = ActorSystem("ActorSystem")

// Create an agent for each int between 1 and 10
val agents = Vector.tabulate[Agent[Int]](10)(x=>Agent[Int](1+x))

// Define a function for each agent to execute
def printRecur(a: Agent[Int])(x: Int): Int = {
    // Print out the stored number and sleep.
    println(x)
    Thread.sleep(250)

    // Recur the agent
    a sendOff printRecur(a) _

    // Keep the …
Run Code Online (Sandbox Code Playgroud)

scala agent akka

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

标签 统计

scala ×3

agent ×1

akka ×1

buildr ×1

clojure ×1

coffeescript ×1

javascript ×1

ruby ×1

typeclass ×1