M
Modular10mo ago
Aamir

Seeking Guidance and Best Practices for Lists and Tuples in Mojo Language

Hello Wonderful Mojo Community! I'm excited to embark on my journey with the Mojo language and am particularly interested in developing datetime and strings libraries. As a newcomer, I'm eager to learn and contribute to this vibrant community. However, I've encountered a bit of a hurdle while working with lists and tuples. Unlike Python, where I can easily access items in a list or tuple, in Mojo, I receive an error stating: it does not implement the '__getitem__'/'__setitem__' or '__refitem__' methods. I'm keen to understand the best practices for handling lists and tuples in Mojo. Could anyone provide guidance or share their experiences on how to navigate this? I'm looking forward to your insights and tips to enhance my understanding and skills in Mojo. Thank you all in advance for your support and assistance!
25 Replies
ModularBot
ModularBot10mo ago
Congrats @Aamir, you just advanced to level 1!
brainiacoutcast
brainiacoutcast10mo ago
you're probably used to using square brackets on python's lists, for mojo's ListLiteral you'll need to use the .get method https://docs.modular.com/mojo/stdlib/builtin/builtin_list.html#get or alternatively, you might be able to just use a dynamicvector if your items in the list are all the same type
Aamir
AamirOP10mo ago
Yeah, I had tried that. However, I dont know the right way to invoke get function. It didn't work for me!
No description
Aamir
AamirOP10mo ago
Is there any book or guided language tour that I can try?
Aamir
AamirOP10mo ago
Also, when I tried to import DynamicVector, I get this error!
No description
Aamir
AamirOP10mo ago
I am using mojo 0.7.0 (af002202) on windows Ubuntu WSL.
benny
benny10mo ago
dynamic vector should be in collections.vector, not Vector. Along with this, the .get function takes a compile time constant parameter, not a variable, so you would need to say something like tuple.get0, Int to get the first item of a tuple of type int
Aamir
AamirOP10mo ago
I need to pass, get value at the given 'i index.
No description
Aamir
AamirOP10mo ago
About Vector import. I followed this!
No description
Aamir
AamirOP10mo ago
After importing the DynamicVector from collections. That error went away, but it started crashing again!
No description
Aamir
AamirOP10mo ago
Compiler is crashing now! Is this language stable? I am not able to move an inch. There should be proper guided language tour with documentations. The way Go did.
brainiacoutcast
brainiacoutcast10mo ago
it's fairly common to have the parser crashing, remember that this language is pre 1.0, you are helping with early testing
Aamir
AamirOP10mo ago
I see! I understand, no worries
brainiacoutcast
brainiacoutcast10mo ago
while it may take some fiddling to get the dynamicvector working, you're on the right path to getting the program working if you need to index into a collection with a value from a variable
Aamir
AamirOP10mo ago
But, after adding dynamic vector, I can't move ahead, due to parser crash. How do you guyw deal with such issues.
brainiacoutcast
brainiacoutcast10mo ago
personally, delete things until it's back at a state where the ide help works again, i'm only a couple days into trying it myself
Aamir
AamirOP10mo ago
Removing this line, from collections.Vector import DynamicVector solves the issue. But that is important one. I need to use list or tuple and want to fetch values with dynamic variables instead of constants
brainiacoutcast
brainiacoutcast10mo ago
you can try mojo filename.mojo to attempt running it and get a slightly more specific message (and stack dump) in this specific case though, i believe vector is just lowercase, collections.vector DynamicVector may not even need to be imported, might just be in prelude is just in the prelude already, no need to import
Aamir
AamirOP10mo ago
I get this!
~/projects/mojo-date$ mojo main.mojo
/home/aamir/projects/mojo-date/main.mojo:2:6: error: unable to locate module 'Vector'
from collections.Vector import DynamicVector
^
LLVM ERROR: expected decl in symbol table to appear in lookup: collections
[13199:13199:20240219,094306.877735:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[13199:13199:20240219,094306.877804:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo main.mojo
[13199:13200:20240219,094306.878606:ERROR directory_reader_posix.cc:42] opendir /home/aamir/.modular/crashdb/attachments/64c83b66-32bf-4897-aec2-5ebe315dbdfb: No such file or directory (2)
~/projects/mojo-date$ mojo main.mojo
/home/aamir/projects/mojo-date/main.mojo:2:6: error: unable to locate module 'Vector'
from collections.Vector import DynamicVector
^
LLVM ERROR: expected decl in symbol table to appear in lookup: collections
[13199:13199:20240219,094306.877735:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq: No such file or directory (2)
[13199:13199:20240219,094306.877804:ERROR file_io_posix.cc:144] open /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq: No such file or directory (2)
Please submit a bug report to https://github.com/modularml/mojo/issues and include the crash backtrace along with all the relevant source codes.
Stack dump:
0. Program arguments: mojo main.mojo
[13199:13200:20240219,094306.878606:ERROR directory_reader_posix.cc:42] opendir /home/aamir/.modular/crashdb/attachments/64c83b66-32bf-4897-aec2-5ebe315dbdfb: No such file or directory (2)
ModularBot
ModularBot10mo ago
Congrats @Aamir, you just advanced to level 2!
brainiacoutcast
brainiacoutcast10mo ago
if you go through it slowly top to bottom, you'll see unable to locate module 'Vector' - in the docs it's lowercase
Aamir
AamirOP10mo ago
I see, but in the documentation it is in Capital case!
No description
brainiacoutcast
brainiacoutcast10mo ago
make sure you use the latest from https://docs.modular.com/mojo/ - third party sites may be weeks or more out of date
Modular Docs - Mojo🔥
A programming language that bridges the gap between AI research and production, unlocking speed and usability.
Aamir
AamirOP10mo ago
👍🏼
brainiacoutcast
brainiacoutcast10mo ago
we're both gonna make it
Want results from more Discord servers?
Add your server