Part1-compose 附加题


  • 0
    administrators

    根据代码注释编写相应的函数。代码引入了一个支持性质的依赖文件 support.js,源码可见 github,还有已经引入了 Ramda.js。

    参考答案在讨论区。


  • 0
    administrators

    本题参考答案:

    var _ = R; /* Ramda.js */
    
    // 使用但是不要修改这个函数
    var formatMoney = function (money) { return '$' + money.toFixed(2); } 
    
    var CARS = [
        {name: "Ferrari FF", horsepower: 660, dollar_value: 700000, in_stock: true},
        {name: "Spyker C12 Zagato", horsepower: 650, dollar_value: 648000, in_stock: false},
        {name: "Jaguar XKR-S", horsepower: 550, dollar_value: 132000, in_stock: false},
        {name: "Audi R8", horsepower: 525, dollar_value: 114200, in_stock: false},
        {name: "Aston Martin One-77", horsepower: 750, dollar_value: 1850000, in_stock: true},
        {name: "Pagani Huayra", horsepower: 700, dollar_value: 1300000, in_stock: false}
      ];
      
    // Bonus 1:
    // ============
    // Refactor availablePrices with compose.
    
    var formatPrice = _.compose(formatMoney, _.prop('dollar_value'));
    var availablePrices = _.compose(join(', '), _.map(formatPrice), _.filter(_.prop('in_stock')));
    
    // Bonus 2:
    // ============
    // Refactor to pointfree. Hint: you can use _.flip()
    
    var append = _.flip(_.concat);
    var fastestCar = _.compose(append(' is the fastest'),
                               _.prop('name'),
                               _.last,
                               _.sortBy(_.prop('horsepower')));
    
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待