javascript - haskell-js 示例在 CoffeeScript REPL 中中断,但可以从文件或节点 REPL 运行

标签 javascript coffeescript read-eval-print-loop

我一直在尝试 Javascript haskell-js库,但我偶然发现了 Coffeescript REPL 的奇怪行为。

使用节点,以下示例可以按预期工作:

$ node
require('haskell');
> [1,2,3].map('+1');
[ 2, 3, 4 ]

但是使用 CoffeeScript ,它失败了:

$ coffee -v
CoffeeScript version 1.6.1
$ coffee
require 'haskell'
[1,2,3].map('+1')
TypeError: +1 is not a function
at Array.map (native)
at repl:3:15
at REPLServer.replDefaults.eval (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:27:28)
at repl.js:239:12
at Interface.<anonymous> (/usr/local/lib/node_modules/coffee-script/lib/coffee-script/repl.js:55:9)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)

但是,当我从文件运行它时,它会再次工作:

$ cat test.coffee
require 'haskell'
console.log([1,2,3].map('+1'))

$ coffee test.coffee
[ 2, 3, 4 ]

编译为 test.js 会生成以下文件:

$ coffee -c test.coffee && cat test.js
// Generated by CoffeeScript 1.6.1
(function() {
  require('haskell');
  console.log([1, 2, 3].map('+1'));
}).call(this);

现在我很困惑。这不是我测试的吗? (console.log 包装器在 REPL 中也没有任何区别。)

你能帮我理解为什么它在coffeescript REPL中不起作用吗?

最佳答案

当我使用 nesh shell 重复您的情况时,我收到相同的错误

1800:~/myjs$ nesh -c
CoffeeScript 1.7.1 on Node v0.10.1
....
coffee> [1,2,3].map('+1')
TypeError: +1 is not a function

但是当我直接使用 coffee REPL 运行时,它工作得很好

1802:~/myjs$ coffee -v
CoffeeScript version 1.7.1
1802:~/myjs$ coffee
coffee> require 'haskell'
...
coffee> [1,2,3].map('+1')
[ 2, 3, 4 ]

在有效的情况下

coffee> console.log [1,2,3].map.toString()
function () {
        var args, fn;

        fn = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
        return _method.call.apply(_method, [this, fn.toFunction()].concat(__slice.call(args)));
      }

没有的地方:

coffee> [1,2,3].map.toString()
'function map() { [native code] }'

换句话说,.maphaskell 版本并未取代原生版本。因此我们需要检查 haskell 是如何加载的。没有 coffee 覆盖层的 nesh 也存在此加载问题。我敢打赌更新您的 Coffeescript 将解决该问题。

编辑:

haskell 通过修改 global 来安装自身。

大约 6 个月前(1.6.3 和 1.7.0 之间),咖啡中有一个拉取请求,https://github.com/jashkenas/coffee-script/pull/3150 解决 REPL 中“全局”上下文的使用 “使REPL使用全局上下文与节点REPL保持一致。”

关于javascript - haskell-js 示例在 CoffeeScript REPL 中中断,但可以从文件或节点 REPL 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22024294/

相关文章:

javascript - 为什么 JavaScript 数组返回 "Undefined"?

coffeescript - 构造新实例的类方法

mongodb - 如何使用 mongoose ORM 存储/检索到 mongodb

javascript - 在其他类之间共享两个实例

python - 覆盖 REPL 输出

function - GHCi 中的非详尽功能模式

javascript - 如何在不更改当前页面的情况下更改特定目录?

javascript - 如何使用平衡括号算法替换 JavaScript 字符串中的单词?

javascript - Jquery:获取页面的所有html源但排除一些#ids

coffeescript - 我无法在 Coffeescript 交互模式(REPL)中编写多行代码