-
Notifications
You must be signed in to change notification settings - Fork 0
/
mojo_0.6_play.🔥
155 lines (118 loc) · 3.23 KB
/
mojo_0.6_play.🔥
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# https://docs.modular.com/mojo/changelog.html?utm_content=273987722&utm_medium=social&utm_source=twitter&hss_channel=tw-1483918307484848132
fn main() raises:
testtraits()
testdestructable()
testMovableCopyable()
testSaveTensor()
# Traits
fn testtraits():
print("testtraits")
let thingone = woman()
let thingtwo = mallard()
thingone.move()
thingtwo.makeNoise()
makeloudmovement(thingone)
makeloudmovement(thingtwo)
# can't have a DynamicVector of a trait even if it inherits from CollectionElement
var things: DynamicVector[woman] = DynamicVector[woman]()
things.append(thingone)
print("Done")
trait locomotable:
fn move(self):
...
trait noisy:
fn makeNoise(self):
...
trait noisymover(locomotable, noisy):
...
trait duck(noisymover, CollectionElement):
...
trait human(noisymover,CollectionElement):
...
@value
struct woman(human):
fn move(self):
print("walking")
fn makeNoise(self):
print("hello")
@value
struct mallard(duck):
fn move(self):
print("waddle")
fn makeNoise(self):
print("quack")
fn makemove[t: locomotable](tocheck: t):
tocheck.move()
fn makesound[t: noisy](tocheck: t):
tocheck.makeNoise()
fn makeloudmovement[t: noisymover](tocheck: t):
tocheck.move()
tocheck.makeNoise()
# Destructable
fn testdestructable():
print("testdestructable")
let thing = Mustdie(10)
print(thing)
print("len()",len(thing))
print("int()", int(thing))
print("Done")
@value
struct Mustdie(Destructable, Stringable, Intable, Sized):
var ptr: Pointer[Int]
var size: Int
fn __init__(inout self, size: Int):
self.size = size
self.ptr = Pointer[Int].alloc(size)
for i in range(self.size):
self.ptr[i] = i
fn __del__(owned self):
print("Deleting mustdie:", self.size)
self.ptr.free()
fn __str__(self) -> String:
var result:String = ""
for i in range(self.size):
result += String(self.ptr[i])
return result
fn __int__(self) -> Int:
var total: Int = 0
for i in range(self.size):
total += self.ptr[i]
return total
fn __len__(self) -> Int:
return self.size
# movable, copyable
struct van(Movable, Copyable, CollectionElement):
var thing: Int
fn __init__(inout self):
print("van init")
self.thing = 0
fn __moveinit__(inout self: van, owned existing: van ):
print("van moveinit")
self.thing = existing.thing
fn __copyinit__(inout self: van, existing: van ):
print("van copyinit")
self.thing = existing.thing
fn __del__(owned self):
print("del")
fn testMovableCopyable():
print("testMovableCopyable")
var vans: DynamicVector[van] = DynamicVector[van]()
vans.append(van())
print("len vans",len(vans))
print("Done")
# surprising del before
# explict calling del on vans weird also
#
fn testSaveTensor() raises:
from random import rand
from tensor import Tensor, TensorSpec, TensorShape
from utils.index import Index
let test = rand[DType.float32](10,10,3)
let spec = TensorSpec(DType.float32, 10, 10, 3)
var tensor = Tensor[DType.float32](spec)
# i'm sure there is a better way of doing this
for i in range(9):
for j in range(9):
for k in range(2):
tensor[Index(i,j,k)] = test[i,j,k]
tensor.save("./cool")