freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../functional-programming/introduction-to-currying-an...

3.7 KiB

id title challengeType videoUrl localeTitle
587d7dab367417b2b2512b70 Introduction to Currying and Partial Application 1 مقدمة في التجلي والتطبيق الجزئي

Description

إن arity الوظيفة هو عدد الحجج التي يتطلبها. تعني وظيفة Currying دالة تحويل دالة N arity إلى N دالتي arity 1. وبعبارة أخرى ، فإنها تعيد هيكلة إحدى الدالتين بحيث تأخذ إحدى الحجة ، ثم ترجع دالة أخرى تأخذ الحجة التالية ، وهكذا. إليك مثال على ذلك:
// غير بالكاري وظيفة
وظيفة غير مضمنة (x، y) {
ارجع x + y؛
}

// وظيفة الكاري
الوظيفة curried (x) {
وظيفة الإرجاع (y) {
ارجع x + y؛
}
}
curried (1) (2) // Returns 3
هذا مفيد في البرنامج الخاص بك إذا كنت لا تستطيع توفير جميع الوسائط إلى وظيفة في وقت واحد. يمكنك حفظ كل استدعاء دالة في متغير ، والذي سيحافظ على مرجع الدالة المرتجعة الذي يأخذ الوسيطة التالية عندما تكون متاحة. في ما يلي مثال على ذلك باستخدام وظيفة curried في المثال أعلاه:
// استدعاء وظيفة بالكاري في أجزاء:
var funcForY = curried (1)؛
console.log (funcForY (2))؛ // المطبوعات 3
وبالمثل ، يمكن وصف partial application على أنه تطبيق عدد قليل من الوسيطات على إحدى الوظائف في وقت ما وإرجاع وظيفة أخرى يتم تطبيقها على مزيد من الوسيطات. إليك مثال على ذلك:
// وظيفة محايدة
وظيفة غير متحيزة (x، y، z) {
ارجع x + y + z؛
}
var partialFn = impartial.bind (هذا ، 1 ، 2) ؛
partialFn (10)؛ // إرجاع 13

Instructions

املأ جسم وظيفة add بحيث يستخدم currying لإضافة المعلمات x و y و z .

Tests

tests:
  - text: <code>add(10)(20)(30)</code> يجب أن ترجع <code>60</code> .
    testString: 'assert(add(10)(20)(30) === 60, "<code>add(10)(20)(30)</code> should return <code>60</code>.");'
  - text: <code>add(1)(2)(3)</code> يجب أن ترجع <code>6</code> .
    testString: 'assert(add(1)(2)(3) === 6, "<code>add(1)(2)(3)</code> should return <code>6</code>.");'
  - text: <code>add(11)(22)(33)</code> يجب أن ترجع <code>66</code> .
    testString: 'assert(add(11)(22)(33) === 66, "<code>add(11)(22)(33)</code> should return <code>66</code>.");'
  - text: يجب أن تتضمن شفرتك عبارة نهائية تُرجع <code>x + y + z</code> .
    testString: 'assert(code.match(/[xyz]\s*?\+\s*?[xyz]\s*?\+\s*?[xyz]/g), "Your code should include a final statement that returns <code>x + y + z</code>.");'

Challenge Seed

function add(x) {
  // Add your code below this line


  // Add your code above this line
}
add(10)(20)(30);

Solution

// solution required