当我在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)