capt_falamer
capt_falamer
MModular
Created by franchesoni on 10/23/2024 in #questions
dict containing sets?
I ended up modifying the stdlib for Set and added the modified file to my project. Like you mentioned, it only needs one method to be a viable CollectionElement. Then you don't have to write a wrapper and treat can use it as is
4 replies
MModular
Created by jmky on 10/7/2024 in #questions
Is there a disdain for Rust from the Mojo team?
this kind of step by step introduction from simple to advanced features would be great to add to the mojo documentation. it would be especially helpful to the python crowd or even people new to programming
23 replies
MModular
Created by taalhaataahir01022001 on 10/8/2024 in #questions
Reshape MAX API OP giving error
I'm not getting any errors when running this (same as above but with the import statements):
from max.graph import Graph, ops
from max.graph.type import TensorType, Dim, Type

fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
from max.graph import Graph, ops
from max.graph.type import TensorType, Dim, Type

fn main() raises:
dim1 = Dim(2)
dim2 = Dim(2)
x = List[Dim] ()
x.append(dim1)
x.append(dim2)

var graph0 = Graph(in_types=List[Type](TensorType(DType.float32, 1, 2, 2)))
var r = ops.reshape(graph0[0], x)
what version of mojo/max are you running?
4 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
then what I first responded was what you were looking for. Here's a fleshed out example to clarify what I was trying to communicate
from utils import Variant
alias ComponentList = Variant[BaseComponent1,BaseComponent2]
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(len(self.components)):
if self.components[i].isa[BaseComponent1]():
self.components[i][BaseComponent1].update()
elif self.components[i].isa[BaseComponent2]():
self.components[i][BaseComponent2].update()
#continue elif statements for each additional variant

fn addCpt(inout self, value: ComponentList):
self.components.append(value)


@value
struct BaseComponent1: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method1")
fn render(inout self): pass

@value
struct BaseComponent2: # example
fn __init__(inout self): print("hi2!!!!!")

fn update(inout self): print("update method2")
fn render(inout self): pass

def main():
var b1 = BaseComponent1()
var b2 = BaseComponent2()
var comp_list_entity = Entity(CptList(b1,b2))
comp_list_entity.update()
from utils import Variant
alias ComponentList = Variant[BaseComponent1,BaseComponent2]
alias CptList = List[ComponentList]

struct Entity:
var components: CptList

fn __init__(inout self, components: CptList):
self.components = components

fn update(inout self):
for i in range(len(self.components)):
if self.components[i].isa[BaseComponent1]():
self.components[i][BaseComponent1].update()
elif self.components[i].isa[BaseComponent2]():
self.components[i][BaseComponent2].update()
#continue elif statements for each additional variant

fn addCpt(inout self, value: ComponentList):
self.components.append(value)


@value
struct BaseComponent1: # example
fn __init__(inout self): print("hi!!!!!")

fn update(inout self): print("update method1")
fn render(inout self): pass

@value
struct BaseComponent2: # example
fn __init__(inout self): print("hi2!!!!!")

fn update(inout self): print("update method2")
fn render(inout self): pass

def main():
var b1 = BaseComponent1()
var b2 = BaseComponent2()
var comp_list_entity = Entity(CptList(b1,b2))
comp_list_entity.update()
Unfortunately there is no shortcut currently to blanket apply a method by doing list[x].common_method() even if all the structs within the variant have the exact same method.
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
Think I may be missing what you are trying to do. On mobile right now and it will be a bit before I can write up working code. I think if you could explain the specific use case I might be able to better answer the root of your question
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
It can be, depending on what you're trying to do you may not even need a variant
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
you'll need to have the Variant[types...] as one of the list of types in the original variant
alias my_variant_variant = Variant[Int,String,Variant[Int,String]]
alias list_of_variants = List[my_variant_variant]

def main():
var v = list_of_variants()
v.append(my_variant_variant(1))
v.append(my_variant_variant(str('hi')))
v.append(my_variant_variant( Variant[Int,String](str('variant of a variant') )))

var my_int = v[0][Int] #my_int is of type Int
var my_string = v[1][String] #my_string is of type String
var my_var = v[2][Variant[Int,String]] #my_var is of type Variant[Int, String]

print(my_int) #prints 1
print(my_string) #prints 'hi'
print(my_var[String]) #prints 'variant of a variant'
alias my_variant_variant = Variant[Int,String,Variant[Int,String]]
alias list_of_variants = List[my_variant_variant]

def main():
var v = list_of_variants()
v.append(my_variant_variant(1))
v.append(my_variant_variant(str('hi')))
v.append(my_variant_variant( Variant[Int,String](str('variant of a variant') )))

var my_int = v[0][Int] #my_int is of type Int
var my_string = v[1][String] #my_string is of type String
var my_var = v[2][Variant[Int,String]] #my_var is of type Variant[Int, String]

print(my_int) #prints 1
print(my_string) #prints 'hi'
print(my_var[String]) #prints 'variant of a variant'
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
Yeah, if I were to guess, the explicit call is for the strict typing and performance reasons
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
Here's the example from the API docs for an example:
from utils import Variant
alias IntOrString = Variant[Int, String]
fn to_string(inout x: IntOrString) -> String:
if x.isa[String]():
return x[String][]
# x.isa[Int]()
return str(x[Int][])

# They have to be mutable for now, and implement CollectionElement
var an_int = IntOrString(4)
var a_string = IntOrString(String("I'm a string!"))
var who_knows = IntOrString(0)
import random
if random.random_ui64(0, 1):
who_knows.set[String]("I'm actually a string too!")

print(to_string(an_int))
print(to_string(a_string))
print(to_string(who_knows))
from utils import Variant
alias IntOrString = Variant[Int, String]
fn to_string(inout x: IntOrString) -> String:
if x.isa[String]():
return x[String][]
# x.isa[Int]()
return str(x[Int][])

# They have to be mutable for now, and implement CollectionElement
var an_int = IntOrString(4)
var a_string = IntOrString(String("I'm a string!"))
var who_knows = IntOrString(0)
import random
if random.random_ui64(0, 1):
who_knows.set[String]("I'm actually a string too!")

print(to_string(an_int))
print(to_string(a_string))
print(to_string(who_knows))
18 replies
MModular
Created by banananas on 9/28/2024 in #questions
Calling methods from list indexes
self.components[i][BaseComponent].update()
self.components[i][BaseComponent].update()
Mojo requires you to tell it which of the variant types it is
18 replies
MModular
Created by Hylke on 9/22/2024 in #questions
How to define nested dict function
Also if you are looking at parsing (or better more fleshed out code) I recommend checking out https://www.modular.com/modverse/modverse-42 specifically Pholmola's project
6 replies
MModular
Created by Hylke on 9/22/2024 in #questions
How to define nested dict function
Probably not the best way to do what you're looking for but see the 2 examples below. From example you have though, makes me wonder if you want to use a nested dict or just store each type in its own array.
@value
struct Node:
var dict: Dict[String, Variant[String,Node]]

fn __setitem__(inout self, key: String, val:Variant[String,Node]):
self.dict[key] = val

fn __getitem__(self, key: String) raises -> Variant[String,Node]:
if key in self.dict:
return self.dict[key]
else:
return Variant[String,Node](str('Does not exist'))

alias str_int = Variant[String,Int,StringLiteral]
@value
struct Node2:
var dict: Dict[String, Node2]
var value: str_int

fn __setitem__(inout self, key: String, n: Node2):
self.dict[key] = n

fn __getitem__(self, key: String) raises -> Node2:
if key in self.dict:
return self.dict[key]
else:
return Node2(Dict[String, Node2](), '')

def main():
n = Node(Dict[String, Variant[String,Node]]())
var a1 = Variant[String,Node](str('123'))
var a2 = Variant[String,Node](str('Pike'))
var a3 = Variant[String,Node](str('NY'))
var add = Node(Dict[String, Variant[String,Node]]())
add['street'] = a1
add['city'] = a2
add['state'] = a3
n['Address'] = add
print(n['Address'][Node]['street'][String])
print(n['Address'][Node]['city'][String])
print(n['Address'][Node]['state'][String])

n2 = Node2(Dict[String, Node2](), str_int(''))

n2['user'] = Node2(Dict[String, Node2](), str_int('Allise'))
print(n2['user'].value[StringLiteral])

n2['address'] = Node2(Dict[String, Node2](), str_int(''))
n2['address']['street'] = Node2(Dict[String, Node2](), str_int(123))
n2['address']['city'] = Node2(Dict[String, Node2](), str_int('Pike'))
n2['address']['state'] = Node2(Dict[String, Node2](), str_int('NY'))
@value
struct Node:
var dict: Dict[String, Variant[String,Node]]

fn __setitem__(inout self, key: String, val:Variant[String,Node]):
self.dict[key] = val

fn __getitem__(self, key: String) raises -> Variant[String,Node]:
if key in self.dict:
return self.dict[key]
else:
return Variant[String,Node](str('Does not exist'))

alias str_int = Variant[String,Int,StringLiteral]
@value
struct Node2:
var dict: Dict[String, Node2]
var value: str_int

fn __setitem__(inout self, key: String, n: Node2):
self.dict[key] = n

fn __getitem__(self, key: String) raises -> Node2:
if key in self.dict:
return self.dict[key]
else:
return Node2(Dict[String, Node2](), '')

def main():
n = Node(Dict[String, Variant[String,Node]]())
var a1 = Variant[String,Node](str('123'))
var a2 = Variant[String,Node](str('Pike'))
var a3 = Variant[String,Node](str('NY'))
var add = Node(Dict[String, Variant[String,Node]]())
add['street'] = a1
add['city'] = a2
add['state'] = a3
n['Address'] = add
print(n['Address'][Node]['street'][String])
print(n['Address'][Node]['city'][String])
print(n['Address'][Node]['state'][String])

n2 = Node2(Dict[String, Node2](), str_int(''))

n2['user'] = Node2(Dict[String, Node2](), str_int('Allise'))
print(n2['user'].value[StringLiteral])

n2['address'] = Node2(Dict[String, Node2](), str_int(''))
n2['address']['street'] = Node2(Dict[String, Node2](), str_int(123))
n2['address']['city'] = Node2(Dict[String, Node2](), str_int('Pike'))
n2['address']['state'] = Node2(Dict[String, Node2](), str_int('NY'))
6 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
Does the list have to be ordered in relation to other types of objects? Thinking you might get away with struct with a list for each type
44 replies