From b40ba1e9315839efe5c7bb9c5c132eb58f31473b Mon Sep 17 00:00:00 2001 From: Katywolk Date: Wed, 30 Apr 2025 11:51:47 +0800 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5=20Pa?= =?UTF-8?q?rtial=20Application?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercises/1-power.js | 13 ++++--------- Exercises/2-closure.js | 6 +++--- Exercises/3-lambda.js | 2 +- Exercises/4-bind.js | 4 ++-- Exercises/5-curry.js | 6 +++++- Exercises/6-chaining.js | 8 +++++++- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Exercises/1-power.js b/Exercises/1-power.js index 83226d2..2da7172 100644 --- a/Exercises/1-power.js +++ b/Exercises/1-power.js @@ -1,15 +1,10 @@ 'use strict'; -// Define function power(exp, n), the same as Math.pow(n, exp) -// but with reverse order of arguments. -const power = null; +const { pow: expow } = Math; +const power = (exp, n) => expow(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 argument (to the power of three). -const cube = null; +const cube = power.bind(null, 3); module.exports = { power, square, cube }; diff --git a/Exercises/2-closure.js b/Exercises/2-closure.js index d6f2435..33f1985 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 = (strength) => defineCoffeeType(50)(strength); +const americano = (strength) => defineCoffeeType(150)(strength); module.exports = { espresso, americano }; diff --git a/Exercises/3-lambda.js b/Exercises/3-lambda.js index 994caa8..b8178c9 100644 --- a/Exercises/3-lambda.js +++ b/Exercises/3-lambda.js @@ -6,6 +6,6 @@ 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 = (str) => tagged(new Date().toISOString().substring(0, 10), str); module.exports = { tagDate }; diff --git a/Exercises/4-bind.js b/Exercises/4-bind.js index 1d1849a..3a35601 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 = H.bind(null, 1); +const rootMeanSquare = H.bind(null, 2); module.exports = { average, rootMeanSquare }; diff --git a/Exercises/5-curry.js b/Exercises/5-curry.js index e175699..3e9468d 100644 --- a/Exercises/5-curry.js +++ b/Exercises/5-curry.js @@ -7,7 +7,11 @@ 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) => { + return (args.length >= length) ? fn(...args) : + (...nextArgs) => curry(length, fn)(...args, ...nextArgs); + }; // 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 e768528..12cede1 100644 --- a/Exercises/6-chaining.js +++ b/Exercises/6-chaining.js @@ -10,6 +10,12 @@ const checkPin = (...code) => code.join('') === EXPECTED_PIN; // // For hint use https://github.com/HowProgrammingWorks/Cheatsheet -const press = null; +const press = (a) => ({ + press: (b) => ({ + press: (c) => ({ + press: (d) => checkPin(a, b, c, d) + }) + }) +}); module.exports = { press };