Created
April 4, 2011 21:49
-
-
Save mrdavidlaing/902523 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Market> markets = api.GetMarkets(MarketType.FX); | |
/* | |
* Using a primitive for loop | |
* - Opportunity for off by one errors | |
* - Syntax noise - declaring an empty List, thinking about i & markets[i] | |
*/ | |
List<Market> openMarkets = new List<Market>(); | |
for(int i=0; i < markets.Count; i++) | |
{ | |
if (markets[i].IsOpen) | |
openMarkets.Add(market[i]); | |
} | |
/* | |
* Using a higher order "each" loop | |
* - No more syntax noise with i; or possibility of off by one errors | |
* - But, still extra syntax noise - declaring an empty List, | |
* - Most important statement - market.IsOpen() obscured by foreach & List.Add syntax | |
*/ | |
List<Market> openMarkets = new List<Market>(); | |
foreach(Market market in markets) | |
{ | |
if (market.IsOpen) | |
openMarkets.Add(market); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
markets.filter( function(market) { | |
return market.IsOpen; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Using a higher order function Where() | |
* - No more noise around declaring empty list | |
* - The most important statement - market.IsOpen() - has prominence. | |
*/ | |
List<Market> openMarkets = markets.Where(market => market.IsOpen); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inflatedStockItems = stockItems.map(function(stockItem) { | |
stockItem.price = stockItem.price * INFLATION; | |
return stockItem; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var INFLATION = 1.05; | |
var stockItems = api.GetStockItems(); | |
var inflatedStockItems = []; | |
for(stockItem in StockItems) { | |
var inflatedStockItem = dojo.clone(stockItem); // http://dojotoolkit.org/api/1.5/dojo.clone | |
inflatedStockItem.price = inflatedStockItem.price * INFLATION; | |
inflatedStockItems.push(inflatedStockItem); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inflatedStockItems = stockItems.Select( (stockItem) => { | |
stockItem.Price *= INFLATION; | |
return stockItem; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment