From a3bacd66a903ba10407a2bf910fd181c3bb62f0b Mon Sep 17 00:00:00 2001 From: "Grevcev.AS" Date: Fri, 29 Oct 2021 07:46:24 +1000 Subject: [PATCH] lab partialApplication --- Exercises/1-power.js | 12 +++--------- Exercises/2-closure.js | 6 +++--- Exercises/3-lambda.js | 7 ++++++- Exercises/4-bind.js | 4 ++-- Exercises/5-curry.js | 8 +++++++- Exercises/6-chaining.js | 13 ++++++++++++- 6 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Exercises/1-power.js b/Exercises/1-power.js index 9a99509..8b096af 100644 --- a/Exercises/1-power.js +++ b/Exercises/1-power.js @@ -1,15 +1,9 @@ 'use strict'; -// Define function power(exp, n), the same as Math.pow(n, exp) -// but with reverse order of argumants. -const power = null; +const power = (exp, n) => Math.pow.bind(null, n, exp)(); -// Implement function `square(n)`, which returns square of its argument. -// The function may or may not reuse function `power`. -const square = null; +const square = (n) => power(2, n); -// Implement function `cube(n)` using partial application -// The function should return cube of argumant (to the power of three). -const cube = null; +const cube = (n) => Math.pow.bind(null, n, 3)(); module.exports = { power, square, cube }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index d6f2435..a419f96 100644 --- a/Exercises/2-closure.js +++ b/Exercises/2-closure.js @@ -3,13 +3,13 @@ const coffee = (volume, strength) => `Coffee volume: ${volume}ml, strength: ${strength}`; -const defineCoffeeType = volume => strength => coffee(volume, strength); +const defineCoffeeType = (volume) => (strength) => coffee(volume, strength); // Use function defineCoffeeType to define new coffee types. // Define coffee type espresso which volume should equal 50ml. // Define coffee type americano which volume should equal 150ml. -const espresso = null; -const americano = null; +const espresso = defineCoffeeType(50); +const americano = defineCoffeeType(150); module.exports = { espresso, americano }; diff --git a/Exercises/3-lambda.js b/Exercises/3-lambda.js index 994caa8..c2e510d 100644 --- a/Exercises/3-lambda.js +++ b/Exercises/3-lambda.js @@ -6,6 +6,11 @@ const tagged = (pref, str) => `[${pref}] ${str}`; // E.g. tagDate('My String') === '[2019-11-14] My String' // Use function tagged to implement tagDate. -const tagDate = null; +const tagDate = (string) => { + const todayDate = new Date(); + const formatedDate = todayDate.toISOString().substring(0, 10); + + return tagged(formatedDate, string); +}; module.exports = { tagDate }; diff --git a/Exercises/4-bind.js b/Exercises/4-bind.js index 1d1849a..15b04fc 100644 --- a/Exercises/4-bind.js +++ b/Exercises/4-bind.js @@ -19,7 +19,7 @@ const H = (exp, ...args) => { // Create function `average` that returns arithmetic mean (H₁) of the arguments. // Create function `rootMeanSquare` that returns quadratic mean (H₂). -const average = null; -const rootMeanSquare = null; +const average = (...args) => H.bind(null, 1, ...args)(); +const rootMeanSquare = (...args) => H.bind(null, 2, ...args)(); module.exports = { average, rootMeanSquare }; diff --git a/Exercises/5-curry.js b/Exercises/5-curry.js index e175699..3da9cdc 100644 --- a/Exercises/5-curry.js +++ b/Exercises/5-curry.js @@ -7,7 +7,13 @@ const checkPin = (...code) => code.join('') === EXPECTED_PIN; // Define function curry that accepts the length of the function // (amount of function arguments) and the function. -const curry = (length, fn) => (...args) => null; +const curry = (length, fn) => (...args) => { + if (length !== 1) { + const f = fn.bind(null, ...args); + return curry(length - 1, f); + } + return fn(...args); +}; // Allows to enter pin code by one character, // Usage: press('3')('4')('5')('6') diff --git a/Exercises/6-chaining.js b/Exercises/6-chaining.js index b0b2d26..337c7e9 100644 --- a/Exercises/6-chaining.js +++ b/Exercises/6-chaining.js @@ -10,6 +10,17 @@ const checkPin = (...code) => code.join('') === EXPECTED_PIN; // // For hint use https://github.com/HowProgrammingWorks/Cheatsheet -const press = null; +const press = (code) => { + let pin = code; + return { + press(i) { + pin += i; + if (4 === pin.length) { + return checkPin(pin); + } + return this; + } + }; +}; module.exports = { press };