0Pricing
Mojo Academy · Lección

Algoritmos paramétricos

Un algoritmo, muchas versiones especializadas.

Algoritmos paramétricos es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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 * s

The 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. 🎯

Preguntas frecuentes

¿La lección «Algoritmos paramétricos» es gratis?

Sí — el texto completo de «Algoritmos paramétricos» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Algoritmos paramétricos»?

Un algoritmo, muchas versiones especializadas. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Algoritmos paramétricos»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Algoritmos paramétricos
  2. Desenrollado de bucles en tiempo de compilación
  3. Compilación condicional
  4. Restricciones y comprobaciones estáticas
← Volver a Mojo Academy