42 questions
0
votes
0
answers
44
views
Why does SIMD[DType.bool, 4](True, False, True) give unexpected reduce_bit_count() results in Mojo?
I'm investigating the method reduce_bit_count() from Mojo's SIMD API.
According to the documentation:
Returns the total number of bits set in the SIMD vector.
I expected the following behavior:
3 ...
0
votes
0
answers
47
views
How to sort a List in Mojo?
sort() can be used on lists of integers without further ado.
But if I define my own type, which trait shall I implement on it so that lists made of it can be sorted?
@value
struct MyStruct(...
2
votes
1
answer
96
views
Magic fails to install Mojo dependencies
I cannot use the Mojo dependency called lightbug_http in a fresh Mojo project.
magic init hello_web --format mojoproject
cd hello_web
magic shell
Printing «Hello world» works fine.
Then I add https://...
0
votes
1
answer
103
views
String literals, List Literals and let statements not working as expected with Mojo 24.4.0
I am new to Mojo and just started learning the language. I am practicing some code on my Jupyter notebook in my Mac mini. My Mojo version is 24.4.0
CASE1:
The below simple code for ListLiterals gives ...
1
vote
1
answer
527
views
How can I write asynchronous code in mojo? ("Coroutine not copyable")
How can I implement asynchroneous code in mojo (using async etc.)?
In the Changelog, I find the following example (slightly adjusted):
async fn add_three(a: Int, b: Int, c: Int) -> Int:
return ...
3
votes
0
answers
595
views
Can not connect VSCode to WSL2 running Ubuntu-22.04
I am setting up a test environment to try the MOJO language in my Windows 11 Pro laptop (V 10.0.22631 Build 22631). Since MOJO doesn't have a Windows SDK available (yet), I installed WSL2 using ...
1
vote
0
answers
31
views
Is there a way to load a memory-only pointer type
I'm trying to write a solution to a problem I found online using Mojo. The problem is a graph theory related so I wanted to implement the data structure by hand to get more familiar with the language.
...
1
vote
1
answer
131
views
How to define overloaded function in MOJO language?
The code below fails.
fn greet(n: Bool) -> String:
return "Hello, 2!"
fn greet(name: String) -> String:
return "Hello, " + name + "!"
fn main():
...
0
votes
1
answer
75
views
Cannot return a struct implementing a trait in a function
I have this code :
trait State:
fn to_string(self) -> String:
...
struct StateTtt(State):
fn __init__(inout self):
pass
fn to_string(self) -> String:
...
0
votes
1
answer
143
views
Does eval() have performance advantage in Mojo vs. Python?
I would like to use eval() for validation of short function synthesis. The search space may be fairly large, e.g. more than a million different functions. Functions will be synthesized as a string ...
0
votes
1
answer
498
views
Issue using Python.import_module to import python numpy package in Mojo
I've just started learning Mojo, following the documentation to get started. I've run into a problem with importing the Python module numpy. Below is the code I am using which is taken from the ...
0
votes
2
answers
129
views
How do I do a typedef in mojo?
I want to implement a class with a property whose type I can change globally at compile time.
For example,
let ID_TYPE = Int
@value
struct MyStruct:
var a: ID_TYPE
However, let seems not to be ...
0
votes
1
answer
182
views
Pass arguments to a Python module in Mojo?
I have a python module : myScript.py.
Normally, I run it like this
python3 myScript.py -i example.jpg
I want to pass this argument to this python modules's main function
from python import Python
fn ...
0
votes
1
answer
225
views
installed mojo programming language but finding a repeating command when executed
I've installed mojo on my compute : and went to ran the code 'nano__.zprofile'
previously when downloading modular/mojo I ve got network issue, couple of times so I've done it multiple times
Now when ...
1
vote
1
answer
211
views
Mojo (Mac) using wrong python lib
As most Mac users, I have two separate versions of python on my machine: The one installed by Apple (version 3.9 in /usr/bin) and one installed via Homebrew (version 3.11 in /opt/homebrew/bin).
I've ...
1
vote
2
answers
492
views
String manipulation in Mojo
I have a Mojo struct with the following initialiser:
fn __init__(inout self, value: String):
"""Constructor."""
self.value = value.lower()
If I have ...
2
votes
1
answer
352
views
pressing the run button to run a mojo file does nothing
I'm trying to run mojo in VScode, however pressing the run button does absolutely nothing. No errors, no nothing.
I can run the file from terminal and it works fine but I would love to use running and ...
2
votes
3
answers
362
views
Why can't i declare a variable which does not mutated/change in mojo
why can't i import python code in mojo
from python import Python
let np = Python.import_module("numpy")
this gives me error
/home/kali/hello.mojo:2:30: error: cannot call function that may ...
1
vote
1
answer
1k
views
Mojo build for windows
I have some sample Mojo code:
fn main():
print("Hello, world!")
for i in range(10000):
print_no_newline(i)
print_no_newline(' ')
And I run mojo build test.🔥
And it ...
5
votes
4
answers
2k
views
How to do string interpolation (formatting) with Mojo?
I couldn't find how to do string formatting in Mojo in the docs.
I have tried the common Python methods (str.format(), f-strings, % placeholder), but nothing worked out so far. I know Mojo is still a ...
0
votes
1
answer
84
views
'object' does not refer to a package or module
I tried the below in Jupiter, and it is working fine,
But one I tried to use at at file.mojo as:
from python import Python
# The Python way: `import requests as http`
let http = Python.import_module(...
0
votes
1
answer
954
views
Import Python standard library with Mojo
I just started to play around with Mojo. I am having a hard time importing modules from the Python standard library even though, according to the examples reported in the quick start guide, importing ...
0
votes
2
answers
508
views
Running falcon ai model on mojo lang
How can we run https://huggingface.co/tiiuae/falcon-180B the one of the current best models on mojo lang which is faster than python 35k? When we just copy this:
`from transformers import ...
-1
votes
1
answer
2k
views
WslRegisterDistribution failed with error: XXX : mojolang [closed]
I am trying to download mojolang from the website
https://developer.modular.com/download
It says "Install Ubuntu 22.04 for WSL and open it."
I tried installing Ubuntu 22.04.2 LTS from the ...
7
votes
1
answer
423
views
How to statically declare a Tensor?
I started exploring the mojo programming language and now I am trying to figure out how to properly use the Tensor module. I can't find how to statically declare the values inside a tensor.
For the ...
0
votes
1
answer
1k
views
MOJO: How do I convert StringRef to Int?
from sys import argv
fn fibonacci(n: Int) -> Int:
if n == 0:
return 0
elif n == 1:
return 1
return n + fibonacci(n-1)
fn main():
var vls: StringRef = argv()[0]
...
1
vote
1
answer
253
views
How to add an instance of a struct to a DynamicVector
A total newbie question.
struct Item:
var name: StringLiteral
fn __init__(inout self):
self.name = "Unnamed"
fn main():
var dv = DynamicVector[Item]()
var it = Item(...
0
votes
2
answers
559
views
Mojo: "Error: An error occurred in Python."
I am trying out Mojo by Modular and more specifically having a look at the Mandelbrot.ipynb available here. When running the line np = Python.import_module("numpy") I get the rather odd ...
5
votes
0
answers
459
views
How to parametrically check if a variable is of a type in Mojo?
I am trying to create a function that takes in any object and prints it if it is _Printable, and prints "object" otherwise.
To do this I am trying to use @paramter if to check the type at ...
2
votes
1
answer
1k
views
cannot call function that may raise in a context that cannot raise: mojo
mojo version: mojo 0.2.1 (64d14e85)
the code:
struct MyPair:
var first: Int
var second: Int
# We use 'fn' instead of 'def' here - we'll explain that soon
fn __init__(inout self, first:...
16
votes
1
answer
11k
views
Performance Comparison - Mojo vs Python [closed]
Mojo, a programming language, claims to be 65000x faster than python. I am eager to understand if is there any concrete benchmark data that supports this claim? Also, how does it differ in real world ...
1
vote
1
answer
68
views
Mojo Error: package 'base64' does not contain 'b64decode'
Using Mojo's online JupyterLab instance to run Mojo code, I am unable to decode a base64 value which I encoded using mojo itself. Here is my code
from base64
print(base64.b64encode("This is a &...
0
votes
1
answer
2k
views
Create Pandas Dataframe in Mojo
I am trying to declare a Pandas DataFrame in mojo using a list of data. I have followed the examples seen for importing and using Numpy but declaring a DataFrame is only giving errors. How do I fix ...
-2
votes
1
answer
709
views
failed to download Mojo🔥 SDK
I'm using WSL.
the install command:
root@ALL:~# curl https://get.modular.com | MODULAR_AUTH=mut_c38ba851f1854399b827e0293451c24a sh -
download process:
% Total % Received % Xferd Average ...
0
votes
0
answers
162
views
failure to complete the download of Mojo SDK
when I switched to WSL(from windows) and ran:
curl https://get.modular.com | \ MODULAR_AUTH=###################### \ sh -
multiple times the download process stopped within seconds:
% Total % ...
4
votes
1
answer
1k
views
How to pass Mojo function to Python in Python interop?
The question is how to pass Mojo function to Python in Python interop?
For example,
# This is main.mojo
from python.python import Python
def callback():
return 5
def main():
Python.add_to_path(...
3
votes
2
answers
2k
views
vectorize vs parallelize in Mojo
According to the docs:
vectorize
vectorize[simd_width: Int, func: fn[Int](Int) capturing -> None](size: Int)
Maps a function which is parametrized over a simd_width over a range from 0 to size in ...
0
votes
2
answers
157
views
Mojo-lang error about fn, recursion factorial code
I'm a student. I am currently doing research about how fast Mojo Lang is. I want to run the recursion code, and compare it to python. I decided to write a simple recursion code, and an error occurred. ...
2
votes
1
answer
81
views
How can I store compound data in a struct without copying?
The following program tries to store a Thing in a Holder:
struct Holder:
var thing: Thing
fn __init__(inout self, owned thing: Thing):
self.thing = thing
pass
struct Thing:
...
5
votes
2
answers
463
views
How do I specify the return type for fn functions in Mojo
I am running a piece of code that calculates the factorial in Mojo:
fn fact(x:Int):
var mul:Int=1
for i in range(1,x+1):
mul = mul*i
return mul
print(fact(7))
But when I run this, ...
1
vote
1
answer
284
views
Mojo compile time programming - specifying a type
I run the following in the Mojo Plyaground (https://playground.modular.com/) and get an error, what am I doing wrong?
struct MyPair[type:AnyType]:
var first: type
var second: type
# We ...
2
votes
1
answer
3k
views
Load Python standard module from Modular Mojo
From the available information and docs, Mojo claims to be fully compatible with Python syntax and modules.
However, from the Playground notebook, I can't seem to be able to load any module from ...