بتل کد Battle Codeدارد باز می‌شود…

جلسه‌ی 9: جفت‌ها و نوع تابع Pairs and Function Types

با دو نوع تازه آشنا می‌شوی: جفت، مثل [string, number]، که جای هر چیز در آن معلوم است؛ و نوع تابع، مثل (n: number) => number، که می‌گوید در یک جعبه یا یک ورودی چه جور تابعی جا می‌شود.

در این جلسه یاد می‌گیری

درس

علی هر بازیکن را در یک آرایه‌ی دوتایی نگه می‌دارد: اول اسم، بعد امتیاز، مثل ["Sara", 18]. یک روز کسی برعکس می‌نویسد: [18, "Sara"]. جاوااسکریپت چیزی نمی‌گوید و جدول امتیازها به هم می‌ریزد. تایپ‌اسکریپتِ معمولی هم کمک زیادی نمی‌کند. اجرا کن: از نظر او این آرایه‌ای است که هر خانه‌اش «یا متن است یا عدد». نمی‌داند کدام خانه کدام است، پس نمی‌گذارد با player[1] حساب کنی.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
const player = ["Sara", 18];
const next: number = player[1] + 1;
show(next);
</script>

برای این کار یک نوع مخصوص هست: جفت. [string, number] یعنی «دقیقاً دو خانه: اولی متن، دومی عدد». به این نوع tuple هم می‌گویند. حالا تایپ‌اسکریپت می‌داند player[0] متن است و player[1] عدد، و حساب کردن آزاد است.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
const player: [string, number] = ["Sara", 18];
const next: number = player[1] + 1;
show(player[0], next);
</script>

حالا همان اشتباه: جای اسم و امتیاز عوض شده. اجرا کن: هر دو خانه خط قرمز می‌گیرند. خانه‌ی سوم هم قبول نمی‌شود: جفت یعنی دو تا، نه بیشتر و نه کمتر. فرقش با string[] همین است: آرایه هر چند تا خانه از یک نوع دارد؛ جفت تعداد مشخصی خانه دارد که هر کدام نوع خودش را دارد.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
const player: [string, number] = [18, "Sara"];
show(player[0], player[1]);
</script>

جفت وقتی خیلی به درد می‌خورد که تابعی دو جواب دارد. این تابع کوتاه‌ترین و بلندترین اسم را پیدا می‌کند و هر دو را در یک جفت برمی‌گرداند: [string, string]. بیرون تابع، pair[0] اولی است و pair[1] دومی.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
function shortLong(list: string[]): [string, string] {
  let short = list[0];
  let long = list[0];
  for (const name of list) {
    if (name.length < short.length) {
      short = name;
    }
    if (name.length > long.length) {
      long = name;
    }
  }
  return [short, long];
}
const pair = shortLong(["Mina", "Bo", "Sepehr"]);
show("Shortest", pair[0]);
show("Longest", pair[1]);
</script>

حالا نوع دوم. در جاوااسکریپت می‌شود یک تابع را در جعبه گذاشت. پس جعبه‌ای که تابع نگه می‌دارد هم نوع می‌خواهد. (n: number) => number یعنی «تابعی که یک عدد می‌گیرد و یک عدد برمی‌گرداند». سمت چپِ => ورودی‌هاست و سمت راستش نوع جواب. double و addTen هر دو همین شکل را دارند، پس هر دو در op جا می‌شوند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
function double(n: number): number {
  return n * 2;
}
function addTen(n: number): number {
  return n + 10;
}
let op: (n: number) => number = double;
show(op(5));
op = addTen;
show(op(5));
</script>

و تابعی که شکلش فرق دارد جا نمی‌شود. shout متن می‌گیرد و متن می‌دهد. اگر در op می‌نشست، op(5) عدد ۵ را به تابعی می‌داد که toUpperCase می‌خواهد و برنامه وسط کار می‌ایستاد. اجرا کن و خط قرمز را ببین.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
function double(n: number): number {
  return n * 2;
}
function shout(text: string): string {
  return text.toUpperCase() + "!";
}
let op: (n: number) => number = double;
op = shout;
show(op(5));
</script>

بیشترین کاربرد نوع تابع این‌جاست: تابعی که یک تابع دیگر را ورودی می‌گیرد. twice یک عدد و یک تابع می‌گیرد و تابع را دو بار روی عدد اجرا می‌کند. می‌توانی اسم یک تابع را بدهی (double) یا همان‌جا یک تابع فلشی بنویسی. به n در تابع فلشی نوع نداده‌ایم: تایپ‌اسکریپت از نوع f می‌داند که عدد است. filter و map هم دقیقاً همین‌طور کار می‌کنند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
function double(n: number): number {
  return n * 2;
}
function twice(n: number, f: (n: number) => number): number {
  return f(f(n));
}
show(twice(3, double));
show(twice(3, (n) => n + 1));
</script>

دو نکته. اول: اسم ورودی در نوع تابع (آن n) فقط یک برچسب است؛ تابع واقعی می‌تواند اسم دیگری بگذارد. مهم نوع‌هاست. دوم: تابعی که نه ورودی دارد و نه جواب، نوعش () => void است. این‌جا repeat یک کار را چند بار انجام می‌دهد. حواست باشد: در نوع تابع => می‌نویسی، ولی در خط اول خود تابع :.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
function repeat(times: number, job: () => void): void {
  for (let i = 0; i < times; i++) {
    job();
  }
}
repeat(3, () => show("Goal!"));
</script>

مثال‌های این جلسه

چالش‌ها

  1. Swapped Places (آسان)
  2. Make It a Pair (آسان)
  3. A Function as an Input (متوسط)
  4. Two Answers (متوسط)
  5. Find the Red Lines (سخت)
  6. Count with a Test (سخت)

بیشتر بخوان

← جلسه‌ی 8: آرایه‌های نوع‌دار جلسه‌ی 10: پروژه: کارنامه‌ی کلاس →