الخوارزميات المعلمية
خوارزمية واحدة وإصدارات متخصصة متعددة
الخوارزميات المعلمية درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
One Algorithm, Many Shapes
A parametric algorithm describes the logic once, then lets compile-time parameters mint a specialized version for each size, type, or width you need. ⚙️
Parameters Carry the Variation
Anything that changes the code's structure, like a vector width or element type, becomes a parameter instead of a runtime argument.
fn sum[width: Int](data: SIMD[DType.float32, width]) -> Float32:
return data.reduce_add()Type Parameters Generalize
Use a type parameter so the same routine handles Int32, Float64, or any numeric type without you rewriting the body.
fn scale[T: DType, w: Int](v: SIMD[T, w], s: Scalar[T]) -> SIMD[T, w]:
return v * sThe Compiler Specializes Each Use
For every distinct set of parameter values, Mojo generates a fresh specialized function, each optimized exactly for that combination.
No Runtime Cost for Parameters
Because parameters are fixed at compile time, the generated code pays zero runtime cost to read them, unlike normal arguments.
Constraining Type Parameters
Pair a type parameter with a trait so the body can rely on the methods every conforming type promises to provide.
fn total[T: Copyable](items: List[T]) -> Int:
return len(items)Parameters Can Combine
Mix several parameters at once: a type for the data and an Int for the size or width, all resolved together before the program runs.
fn fill[T: DType, n: Int](value: Scalar[T]) -> SIMD[T, n]:
return SIMD[T, n](value)Inference Saves Typing
Mojo can often infer a parameter from the arguments you pass, so you fill in fewer brackets and the compiler completes the rest.
One Source of Truth
You keep a single readable definition while the compiler quietly emits a fast tailored build for each parameter combination behind the scenes.
Where It Shines
Parametric algorithms power Mojo's fast numeric kernels: one matmul or reduction definition, many specialized versions tuned per type and width. 🚀
Readable Yet Generic
The big win is staying readable: your logic reads like normal code, while parameters quietly handle every type and size variation for you.
Quick Check
Think about why parametric algorithms stay fast.
Recap
A parametric algorithm is written once but specialized many times: parameters carry the variation, and each build is tuned with no runtime cost. 🎯
الأسئلة الشائعة
هل درس «الخوارزميات المعلمية» مجاني؟
نعم — نص درس «الخوارزميات المعلمية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «الخوارزميات المعلمية»؟
خوارزمية واحدة وإصدارات متخصصة متعددة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الخوارزميات المعلمية»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الخوارزميات المعلمية
- فكّ حلقات وقت الترجمة
- الترجمة الشرطية
- القيود والفحوصات الساكنة