Part1-compose


  • 0
    administrators

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

    参考答案在讨论区。


  • 0
    administrators

    参考答案:

    var _ = R
      
    // Example Data
    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}
      ];
    
    // Exercise 1:
    // ============
    var isLastInStock = _.compose(_.prop('in_stock'), _.last);
    
    // Exercise 2:
    // ============
    var nameOfFirstCar = _.compose(_.prop('name'), _.head);
    
    
    // Exercise 3:
    // ============
    // Use the helper function _average to refactor averageDollarValue as a composition
    
    var _average = function(xs) { return reduce(add, 0, xs) / xs.length; }; // <- leave be
    
    var averageDollarValue = _.compose(_average, _.map(_.prop('dollar_value')));
    
    
    // Exercise 4:
    // ============
    // Write a function: sanitizeNames() using compose that returns a list of lowercase and underscored names: e.g: sanitizeNames(["Hello World"]) //=> ["hello_world"].
    
    var _underscore = replace(/\W+/g, '_'); //<-- leave this alone and use to sanitize
    
    var sanitizeNames = _.map(_.compose(_underscore, toLowerCase, _.prop('name')));
    

  • 0

    @ScriptOJ 该代码复制执行提示:请使用 Ramda 的 compose、map、prop 编写 averageDollarValue


  • 0

    @Mondo +1


  • 0

    @Mondo +1


  • 0

    此回复已被删除!

登录后回复
 

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