Programming Language
Mojo is a new programming language developed by Modular Inc., designed to combine the usability of Python with the performance capabilities of systems languages like C++ and Rust. Mojo was created by Chris Lattner (creator of LLVM, Swift, and co-creator of MLIR) and his team, with a focus on AI and high-performance computing.
Mojo aims to solve critical performance, deployment, and scaling issues in the current AI infrastructure. It maintains Python compatibility while adding powerful systems programming capabilities like memory management, metaprogramming, and compiler optimizations. Mojo is often described as "Python with superpowers."
# Hello World and basic Python-compatible syntax def hello(): print("Hello, Mojo! 🔥") hello() # Variables and types x: Int = 42 y = 3.14 # Type inference works name = "Mojo" # Lists and dictionaries (like Python) my_list = [1, 2, 3, 4, 5] my_dict = { "name": "Mojo", "type": "language", "year": 2023 } # If-else statement if x > 10: print("x is greater than 10") else: print("x is less than or equal to 10")
# Value semantics with 'let' (immutable) and 'var' (mutable) let a: Int = 10 # Immutable binding var b: Int = 20 # Mutable binding b = 30 # OK, 'b' is mutable # a = 40 # This would cause a compile error as 'a' is immutable # Strong typing with parameterized functions fn add[T: Numeric](x: T, y: T) -> T: return x + y # Specifying memory ownership fn process(owned text: String) -> String: # 'owned' means we take ownership of the parameter let processed = text + " processed" return processed # SIMD vectorization with MLIR fn vector_add(inout a: DynamicVector[Float32], b: DynamicVector[Float32]): for i in range(len(a)): a[i] += b[i]
# Defining a struct struct Point: var x: Float64 var y: Float64 # Constructor fn __init__(inout self, x: Float64, y: Float64): self.x = x self.y = y # Method fn distance(self) -> Float64: return sqrt(self.x * self.x + self.y * self.y) # Mutable method fn move(inout self, dx: Float64, dy: Float64): self.x += dx self.y += dy # Using the struct fn test_point(): var p = Point(3.0, 4.0) print("Distance: ", p.distance()) # Output: 5.0 p.move(2.0, 3.0) print("New coordinates: ", p.x, ", ", p.y)
Mojo is designed for specific domains where Python is popular but performance is critical:
As a relatively new language, Mojo's timeline is brief but notable:
Mojo is being developed by Modular, a company founded by Chris Lattner (creator of LLVM, Swift) and others with deep experience in programming language design and compiler technology.
Here are resources for learning Mojo:
Technologies and languages that relate to Mojo: