1

当我在http://coffeescript.org页面上使用“Try coffeescript”时,以下咖啡脚本编译为精细的 javascript 。但是,在我的电脑上,我得到return了我不想要的地方。return前面应该没有drawAC。任何想法我可以如何解决它。看起来缩进应该是正确的,因为当我复制时,将它准确地粘贴到咖啡脚本网站上,我没有任何问题。

Coffeescript 片段

plotAC = (fc,c,d) ->
    acData = (ac(fc, c, d, q) for q in quantities)
    qACpairs = d3.zip(quantities, acData)
    qACpairs = (item for item in qACpairs when item[1] < 12)
    drawAC = canvas.append('svg:path')
                   .attr({
                        d: createsvgpath(firmXscale)(qACpairs),
                        stroke: 'steelblue'                     
                    })
    return 

在我的计算机上使用 CoffeeScript 1.4.0 版编译的 JS

  plotAC = function(fc, c, d, q) {
    var acData, drawAC, item, qACpairs;
    acData = (function() {
      var _i, _len, _results;
      _results = [];
      for (_i = 0, _len = quantities.length; _i < _len; _i++) {
        q = quantities[_i];
        _results.push(ac(params.fc, params.c, params.d, q));
      }
      return _results;
    })();
    qACpairs = d3.zip(quantities, acData);
    qACpairs = (function() {
      var _i, _len, _results;
      _results = [];
      for (_i = 0, _len = qACpairs.length; _i < _len; _i++) {
        item = qACpairs[_i];
        if (item[1] < 12) {
          _results.push(item);
        }
      }
      return _results;
    })();
    return drawAC = canvas.append('svg:path').attr({
      d: createsvgpath(firmXscale)(qACpairs),
      stroke: 'steelblue'
    });
  };

  return;

我应该补充一点,以下是有效的。但是,这对我来说似乎不是正确的缩进。

有效的代码

plotAC = (fc,c,d) ->
    acData = (ac(fc, c, d, q) for q in quantities)
    console.log(acData)
    qACpairs = d3.zip(quantities, acData)
    console.log(qACpairs);
    qACpairs = (item for item in qACpairs when item[1] < 12)
    console.log(qACpairs);

    drawAC = canvas.append('svg:path')
                   .attr({
                        d: createsvgpath(firmXscale)(qACpairs),
                        stroke: 'steelblue'                     
                    })

                    return

完整代码

dim = 
    width: 1200,
    height: 600,
    padding: 60,
    paddingL: 35,
    paddingR: 25      


canvas = d3.select('#longRunEqm')
           .append("svg:svg")
           .attr
                'width': dim.width,
                'height': dim.height

# Rectangle to show boundaries
canvas.append('svg:rect')
     .attr({
         x: 0,
         y: 0,
         height: dim.height,
         width: dim.width
     }).style({
         fill: 'none',
         stroke: 'gray',
         'stroke-width': '2px'
     })


###
========================================================================
Market Demand and Supply functions
========================================================================
###
params = 
        a: 9,
        b: 0.1,
        c: 2,
        d:0.1,
        fc: 20,
        numFirmsOrig: 100,
        numFirms: 100

quantities = d3.range(0,95)

supplyPrices = d3.range(2,11)

inverseDemand = (a,b,q) -> a - (b*q)

demand = (a,b,p) -> (a-p)/b

inverseSupply = (c,d,q) -> c + (d*q)

tc = (fc,c,d,q) -> c*q + (d*q*q)/2 + fc

ac = (fc,c,d,q) -> tc/q

supply = (c,d,p) -> (p-c)/d

marketSupply = (c, d, p) -> (params.numFirms * supply(c,d,p))/100

eqmQuantity = (a,b,c,d) -> (a-c)/(b+d)

eqmPrice = (a,b,c,d) -> ((a*d) + (b*c))/(b+d)


###
========================================================================
Scales
========================================================================
###

marketXscale = d3.scale.linear().domain([0,105])
                       .range([0+dim.paddingL, (dim.width/2)-dim.paddingR])

firmXscale = d3.scale.linear().domain([0,105])
                     .range([(dim.width/2)+dim.paddingL, \
                              dim.width-dim.paddingR])

yscale = d3.scale.linear().domain([0,12])
                          .range([dim.height-dim.padding, 0+dim.padding])

createsvgpath = (panel) ->
    d3.svg.line()
        .x (d) ->
            panel(d[0])
        .y (d) ->
            yscale(d[1])
        .interpolate('linear')



###
========================================================================
 Axes
========================================================================
###

# X-AXIS MARKET GROUP
xaxisMarket = d3.svg.axis()
              .scale(marketXscale)
              .orient('bottom')
              .ticks(10)
              .tickSubdivide(1)

xaxisMarketgroup = canvas.append('g')
                .attr
                    class: 'axis xaxis',
                    transform: "translate(0,#{dim.height-dim.padding})"
                .call(xaxisMarket)

xaxisMarketgroup.selectAll('text')
          .attr
              transform: "translate(10,0) rotate(45)"
              'text-anchor': 'start'

# xaxisMarket label
xMarketlabel = canvas.append('svg:text')
               .attr
                    x: marketXscale(80),
                    y: yscale(0),
                    dy: 50,
                    'text-anchor': 'middle',
                    class: 'textlabel'
               .text('Market Quantity')
               .style
                    'font-size': '60%'  

# xaxisFirm label
xFirmlabel = canvas.append('svg:text')
                   .attr
                        x: firmXscale(80),
                        y: yscale(0),
                        dy: 50,
                        'text-anchor': 'middle',
                        class: 'textlabel'
                   .text('Firm Quantity')
                   .style
                        'font-size': '60%'                                  

# X-AXIS FIRM GROUP
xaxisFirm = d3.svg.axis()
              .scale(firmXscale)
              .orient('bottom')
              .ticks(10)
              .tickSubdivide(1)

xaxisFirmgroup = canvas.append('g')
                       .attr
                           class: 'axis xaxis',
                           transform: "translate(0,#{dim.height-dim.padding})"
                       .call(xaxisFirm)

xaxisFirmgroup.selectAll('text')
              .attr
                  transform: "translate(10,0) rotate(45)"
                  'text-anchor': 'start'



yaxis = d3.svg.axis()
              .scale(yscale)
              .orient('left')
              .ticks(10)
              .tickSubdivide(1)

# Y-AXIS MARKET GROUP
yaxisMarketgroup = canvas.append('svg:g')
                .attr
                    class: 'axis',
                    transform: "translate(#{dim.paddingL},0)"
                .call(yaxis)


# Y-AXIS FIRM GROUP
yaxisFirmgroup = canvas.append('svg:g')
                       .attr
                           class: 'axis',
                           transform: "translate(#{dim.width/2 + dim.paddingL},0)"
                       .call(yaxis)



# yaxis label
ylabelMarketgroup = canvas.append('svg:text')
                          .attr
                               x: 0,
                               y: 0,
                               'text-anchor': 'middle',
                               transform: 'translate(40,40)',
                               class: 'textlabel'
                          .text('Price')


ylabelFirmgroup = canvas.append('svg:text')
                        .attr
                             x: 0,
                             y: 0,
                             'text-anchor': 'middle',
                             transform: "translate(#{dim.width/2 + dim.paddingL},40)",
                             class: 'textlabel'
                        .text('Price')



###
========================================================================
 Plot supply curve
========================================================================     
###

plotSupply = (c,d,panel,label) ->
    if panel == firmXscale
        supplyFunction = supply
    else
        supplyFunction = marketSupply

    quantitySupplied = (supplyFunction(params.c, params.d, p) for p in \
                            supplyPrices)

    quantityPricePairsSupply = d3.zip(quantitySupplied, supplyPrices)

    # Keep data points where quantity is positive and price is positive
    quantityPricePairsSupply = (item for item in \
        quantityPricePairsSupply when (0 <= item[0] <= 100) and item[1] >=0)

    numQuantityPricePairs = quantityPricePairsSupply.length

    drawSupplyFunction = canvas.append('svg:path')
                           .attr
                                d: createsvgpath(panel)(quantityPricePairsSupply)
                                stroke: '#7E8F7C'    

    labelSupplyFunction = canvas.append('svg:text')
                                .attr({
                                    x: panel(quantityPricePairsSupply[numQuantityPricePairs-1][0]),
                                    y: yscale(quantityPricePairsSupply[numQuantityPricePairs-1][1]),
                                    dx: -5,
                                    dy: -5,
                                    class: 'textlabel',
                                    'text-anchor': 'start'})
                                .style({
                                    stroke: '#7E8F7C' 
                                    })                                     
                                .text(label)

    return 



plotSupply(params.c, params.d, marketXscale, 'S')
plotSupply(params.c, params.d, firmXscale, 'MC')
params.numFirms = 150
plotSupply(params.c, params.d, marketXscale, 'S1')

###
========================================================================
 Plot AC curve
========================================================================
###
plotAC = (fc,c,d) ->
    acData = (ac(fc, c, d, q) for q in quantities)
    qACpairs = d3.zip(quantities, acData)
    qACpairs = (item for item in qACpairs when item[1] < 12)
    drawAC = canvas.append('svg:path')
                   .attr({
                        d: createsvgpath(firmXscale)(qACpairs),
                        stroke: 'steelblue'                     
                    })
    return

plotAC(params.fc, params.c, params.d)
4

3 回答 3

2

忽略我之前的回答。这是一个合法的语言错误。有时在 CoffeeScript 中插入非语义缩进时会遇到类似的奇怪情况——在这种情况下,是.attr子句之前的缩进。

最好的解决方案是删除非语义缩进,如下所示:

plotAC = (fc,c,d) ->
    acData = (ac(fc, c, d, q) for q in quantities)
    qACpairs = d3.zip(quantities, acData)
    qACpairs = (item for item in qACpairs when item[1] < 12)
    drawAC = canvas.append('svg:path')
    .attr({
        d: createsvgpath(firmXscale)(qACpairs),
        stroke: 'steelblue'
    })
    return

这可能看起来有点奇怪,但这是一种完全合理的风格。

请随时在https://github.com/jashkenas/coffee-script/issues报告此问题。在 Michael Fcarra 的CoffeeScript Redux编译器下,这很可能不再是问题。

于 2012-11-02T20:28:36.087 回答
1

正如其他人所建议的那样,这几乎肯定是由混合制表符和空格引起的。打开您的 ST2 用户首选项并添加该行

"translate_tabs_to_spaces": true,

它将为您节省很多很多对空格敏感的语言的麻烦。

于 2012-11-02T19:05:02.857 回答
0

如果您只想解决问题,请执行return this.

我的猜测是您使用的编译器期望返回一个值,而忽略了最终的 return 语句。http://js2coffee.org显示相同的行为,忽略最终返回。(实际上,它编译时没有最后的 return 语句,这更离奇。)

编辑:实际上,在这种情况下编译没有返回是最有意义的。无论哪种方式,这都表明您的编译器存在问题。

我的回答是错误的,但我不想失去下面的讨论。这很可能是空白问题。

于 2012-11-02T17:22:10.573 回答