zoukankan      html  css  js  c++  java
  • [Compose] Compose exercises

    // Setup
    //==============
    const _ = R;
    const {formatMoney} = accounting;
    
    // Example Data
    const 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:
    // ============
    // use _.compose() to rewrite the function below. Hint: _.prop() is curried.
    
    const isLastInStock = _.compose(
      _.prop('in_stock'),
      _.last
    )
    
    QUnit.test("Ex1: isLastInStock", assert => {
      assert.deepEqual(isLastInStock(CARS), false);
    })
    
    ''
    
    
    // Exercise 2:
    // ============
    // use _.compose(), _.prop() and _.head() to retrieve the name of the first car
    
    const nameOfFirstCar = _.compose(
      _.prop('name'),
      _.head
    )
    
    QUnit.test("Ex2: nameOfFirstCar", assert => {
      assert.equal(nameOfFirstCar(CARS), "Ferrari FF");
    })
    
    // Exercise 3:
    // ============
    // Use the helper function _average to refactor averageDollarValue as a composition
    
    const _average = function(xs) { return _.reduce(_.add, 0, xs) / xs.length; }; // <- leave be
    
    const averageDollarValue = _.compose(
      _average,
      _.map(_.prop('dollar_value'))
    )
    
    QUnit.test("Ex3: averageDollarValue", assert => {
      assert.equal(averageDollarValue(CARS), 790700);
    })
    
    
    // Exercise 4:
    // ============
    // Write a function: sanitizeNames() using compose that returns a list of lowercase and underscored names: e.g: sanitizeNames(["Hello World"]) //=> ["hello_world"].
    
    const _underscore = _.replace(/W+/g, '_'); //<-- leave this alone and use to sanitize
    
    const sanitizeNames = _.map(
      _.compose(
        _underscore,
        _.toLower,
        _.prop('name')
      )
    )
    
    QUnit.test("Ex4: sanitizeNames", assert => {
      assert.deepEqual(sanitizeNames(CARS), ['ferrari_ff', 'spyker_c12_zagato', 'jaguar_xkr_s', 'audi_r8', 'aston_martin_one_77', 'pagani_huayra']);
    })
    
    
    
    // Bonus 1:
    // ============
    // Refactor availablePrices with compose.
    const formatDollarValue = _.compose(
          formatMoney,
          _.prop('dollar_value')
        )
    const availablePrices = _.compose(
      _.join(', '),
      _.map(formatDollarValue),
      _.filter(_.prop('in_stock'))
    )
    
    QUnit.test("Bonus 1: availablePrices", assert => {
      assert.deepEqual(availablePrices(CARS), '$700,000.00, $1,850,000.00');
    })
    
    // Bonus 2:
    // ============
    // Refactor to pointfree.
    const append = _.flip(_.concat)
    const fastestCar = _.compose(
      append(' is the fastest'),
      _.prop('name'),
      _.last,
      _.sortBy(_.prop('horsepower'))
    )
    
    QUnit.test("Bonus 2: fastestCar", assert => {
      assert.equal(fastestCar(CARS), 'Aston Martin One-77 is the fastest');
    })
    

      

  • 相关阅读:
    hdu 5387 Clock (模拟)
    CodeForces 300B Coach (并查集)
    hdu 3342 Legal or Not(拓扑排序)
    hdu 3853 LOOPS(概率DP)
    hdu 3076 ssworld VS DDD(概率dp)
    csu 1120 病毒(LICS 最长公共上升子序列)
    csu 1110 RMQ with Shifts (线段树单点更新)
    poj 1458 Common Subsequence(最大公共子序列)
    poj 2456 Aggressive cows (二分)
    HDU 1869 六度分离(floyd)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12776591.html
Copyright © 2011-2022 走看看