Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> สมาร์ทโฟน >> iPhone

เชี่ยวชาญ Swift: เข้าใจ Map, Filter, และ Reduce Functions ลึกๆ

Function รับ Input ทำอย่างนึงสร้าง Output Function มี Signature และ Body ให้ Input เดียวกัน Output จะเหมือนกันเสมอ นี่คือ Definition ของ Function สรุป

มาดู Functions ลึกขึ้น สำรวจ Higher-Order Functions ใน Swift Function ที่รับ Function อื่นเป็น Input หรือ Return Function เรียกว่า Higher-Order Function

ใน Swift ใช้ map, filter, reduce ทุกวัน ใช้แล้วเหมือน Magic จุดนี้อาจไม่เข้าใจเบื้องหลัง Map, Filter, Reduce ทำงานผ่านแนวคิด Functional Programming Swift ไม่ใช่ Pure Functional Language แต่ทำ Functional Things ได้

มาดูทีละอันเบื้องหลัง Implement Basic Versions สำหรับ Data Types เฉพาะ แล้ว Implement Generic Version

Map Function

สมมติมี Array ของ Integers ต้องการ Function คืน Array ใหม่บวก Delta Value เข้าแต่ละ Element ของ Array เดิม เขียน Function ง่ายด้วย For Loop:

ต้องการ Function อีกอันคืน Array ใหม่คูณ 2 แต่ละ Element Implement แบบนี้:

ดู 2 Functions ข้างต้น ทำอย่างเดียวกัน แตกต่างแค่ Logic ภายใน For Loop ทั้งคู่รับ Integer Array Transform แต่ละ Element ด้วย For Loop คืน Array ใหม่ สิ่งสำคัญคือ Transform แต่ละ Element เป็นค่าใหม่

Swift รองรับ Higher-Order Functions เขียน Function รับ Integer Array, Transform Function เป็น Input คืน Array ใหม่ Apply Transform Function ทุก Element

แต่ยังมีปัญหา: คืนแค่ Integer Array ถ้าต้องการ Convert Integer Array เป็น String Array ทำไม่ได้ ต้องเขียน Generic Function รองรับ Type ใดๆ

Implement Generic Function ใน Array Extension แบบนี้:

  1. ประกาศ Map Function ใน Array Extension รองรับ Generic Type T
  2. Function รับ Function Type (Element) -> T เป็น Input
  3. ประกาศ Result Array ว่างเก็บ Type T ภายใน Function
  4. For Loop Iterate เอง เรียก Transform Function แปลง Element เป็น Type T
  5. Append ค่าที่แปลงแล้วใน Result Array

นี่คือการทำงานของ Map Function ใน Swift ถ้าต้อง Implement Map Function เอง จะเขียนแบบข้างต้น คือไม่ได้ทำ Magic ใน Array — เราสามารถ Define Function เองได้ง่าย

Filter Function

สมมติมี Array Integers อยากเก็บเฉพาะเลขคู่ Implement ด้วย For Loop ง่าย:

อีกกรณี Array Strings แทนชื่อไฟล์ Class ใน Project อยากเก็บเฉพาะ .swift Files ทำได้ด้วย Loop เดียว:

ดู Implementation 2 Functions ข้างต้น ทำอย่างเดียวกัน แตกต่างแค่ Data Type ของ Array ทั้ง 2 Generalize ได้โดย Generic Filter Function รับ Array และ Function เป็น Input ขึ้นอยู่กับ Output ของ includeElement Function 决定 ใส่ Element ใน Result Array หรือไม่

Reduce Function

สมมติ Array Integers อยาก Implement 2 Functions คืน Sum และ Product ของ Elements Implement ด้วย For Loop:

แทน Array Integers สมมติ Array Strings อยาก Concatenate Elements ทั้งหมด:

ทั้ง 3 Functions ทำอย่างเดียวกัน รับ Array Input Initialize Result Variable Iterate Array Update Result Variable

จากนี้ Implement Generic Function รองรับทุกอย่าง ต้องการ Initial Value ของ Result Variable และ Function Update Variable ทุก Iteration

Implement Generic Function Definition นี้:

Implementation ข้างต้น Generic สำหรับ Input Array Type [Element] คำนวณ Result Type T ต้องการ Initial Value Type T Assign ให้ Result Variable ต้องการ Function Type (T, Element) -> T ใช้ใน For Loop ทุก Iteration Update Result Variable

ขอบคุณที่อ่าน!