Forums

Bacics help

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Bacics help

Postby Benjamin » Wed Nov 03, 2010 3:40 am

Hi,

I discovered Cobra yesterday and I like it a lot. I'm a python programmer.
To try something out I made this code.. I can not get it to work yet though. It would be great if somebody could help me with the bugs.
Also please comment on my code.

class Controller
var colonys = List<of Colony>()
cue init(amount_colonys as int, amount_bacteria as int)
base.init
for i in amount_colonys
col as Colony = Colony()
.colonys.add(col)
for j in amount_bacteria
col.add_bacteria

class Colony
var bacteria = List<of Bacteria>()

def add_bacteria
bac as Bacteria = Bacteria()
.bacteria.add(bac)

def get_bacterias as List<of Bacteria>
return .bacteria

class Bacteria
var _twin as Bacteria? = nil
cue init(twin as Bacteria?)
base.init
#age = 0
if twin
_twin = twin

class Program
def main
controller as Controller = Controller(1, 1)
print controller.colonys
Benjamin
Benjamin
 
Posts: 8
Location: Holland

Re: Bacics help

Postby Charles » Wed Nov 03, 2010 7:07 pm

When you create a "Bacteria()", the compiler complains because the only "cue init" for Bacteria takes an argument "twin". You can change the creation to "Bacteria(nil)" or you can add another "cue init" like so:
class Bacteria
cue init
.init(nil)
# ...

After that, the program works for me.

Also, wherever you write a local var such as "col as Colony = Colony()" you can just write "col = Colony()". No bug there, but you can make the code less repetitive.

If you want to repost the program after you have expanded it, feel free to do so. Will this be demonstrating some interesting principle of biology? I get the impression that is where it is headed.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Bacics help

Postby Charles » Wed Nov 03, 2010 7:09 pm

Also in the future, try to include more information, like the error message you get, the version of Cobra you are using and the operating system you are on. Thanks.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Bacics help

Postby Benjamin » Wed Nov 17, 2010 11:27 am

Hi again,

My good opinion about Cobra is growing with my experience. I am having some problems with simple things such as converting long to int.
I was wondering if there is a language reference for the builtins like: random, time, ect. I was also wondering if there is a function that returns all the locals of an object as used in python: "dir(object)"

I improved my code: see below. It contains one more bug:

Code: Select all
cobra(44): error: The best overloaded method match for "System.Threading.Thread.
Sleep(int)" has some invalid arguments
c:\Documents and Settings\Benjamin\Desktop\Cobra-2010-10-18\bacteria_simulation.
cobra(44): error: Argument "1": cannot convert from "long" to "int"
Compilation failed - 2 errors, 0 warnings
Not running due to errors above.

Operating system: Windows XP

Script:
use System.Threading

class Controller
var simulaters = List<of Simulater>()

def simulate_life(start_amount_colonys as int, start_amount_bacteria as int)
sim = Simulater(start_amount_colonys, start_amount_bacteria, 100)
sim.thread = Thread(ref sim.run)
sim.thread.start
.simulaters.add(sim)

class Simulater
var colonys = List<of Colony>()
var thread as Thread? = nil
var running as bool = false
var amount_bacteria as int = 0
var sleep_time as int

cue init(start_amount_colonys as int, start_amount_bacteria as int, sleep_time as int)
base.init

for i in start_amount_colonys
col = Colony()
.colonys.add(col)
for j in start_amount_bacteria
col.add_bacteria
.amount_bacteria += 1
.sleep_time = sleep_time

def run
.running = true
while .running
sw = System.Diagnostics.Stopwatch()
sw.start

for col in .colonys
for bac in col.bacteria[:]
bac.age += 1
if bac.age > 10
bac.subdivide
.amount_bacteria += 1
sw.stop
print .amount_bacteria
Thread.sleep(.sleep_time - sw.elapsedMilliseconds)

class Colony
var bacteria = List<of Bacteria>()

def add_bacteria
bac = Bacteria(nil, this)
.bacteria.add(bac)

class Bacteria
var twin as Bacteria? = nil
var age as int = 0
var colony as Colony? = nil
cue init
.init(nil, nil)

cue init(ref_twin as Bacteria?, colony as Colony)
base.init
.colony = colony
if ref_twin
.twin = ref_twin

def subdivide
.twin = Bacteria(this, .colony)
.colony.bacteria.add(.twin)
.age = 0

class Program
def main
controller as Controller = Controller()
controller.simulate_life(1, 1)


Thank you
Benjamin
Benjamin
 
Posts: 8
Location: Holland

Re: Bacics help

Postby Charles » Wed Nov 17, 2010 2:14 pm

Glad you like Cobra. Lots of info for you:

So Thread.sleep takes an int which 32 bits, but the use of sw.elapsedMilliseconds is causing a conversion to long/32-bits. I wrapped the expression in parens and cast to int:
Thread.sleep((.sleep_time - sw.elapsedMilliseconds) to int)
You could say it's poor API that sw.elapsedMilliseconds is 64-bits and Thread.sleep is 32-bits.

So to look up something like Thread.sleep, I just google "msdn thread sleep". You can also go to the MSDN web site and search for "thread sleep".

I prefer "col.bacteria.clone" over "col.bacteria[:]", but that's just style choices.

For smaller code like:
if ref_twin
.twin = ref_twin
You can put on one line:
if ref_twin, .twin = ref_twin

A statement like:
controller as Controller = Controller()
can just be:
controller = Controller()


I'm not sure why you're using threads as Thread.sleep here as it doesn't look necessary, but maybe your program is not complete yet, or maybe I missed something. I think you can use Thread.sleep even without spawing an explicit thread.

Regarding dir(obj), .NET/Mono provides similiar functionality through "reflection". For example, "obj.typeOf.getMembers" will return a list of the members of that type including methods, fields/vars, etc. You can loop through them and ask each member for its name. You can also get just methods, or just variables, or just declared with no inherited members, etc. Lookup ".NET reflection" for more info. Here is a sample program I cooked up:
class A

var myField = 1

get myProperty as int
return 2

get [i as int] as int
return i*2

def myMethod
pass

def main
print 'Run-time names:'
for member in .typeOf.getMembers, print member.name

print
print 'Cobra compile-time names:'
for name in .typeOf.cobraMemberNames, print name


extend Type

def cobraMemberNames as List<of String>
""" Returns a newly created list of Cobra member names for this type. """
names = List<of String>()
for member in .getMembers
name = member.name
if name == '.ctor', name = 'init (cue)'
else if name.startsWith('get_'), name = name[4:].lowered + ' (get)'
else if name.startsWith('set_'), name = name[4:].lowered + ' (set)'
else, name = name.lowered
names.add(name)
return names


extend String

def lowered as String
if .length == 0, return this
return this[0].toString.toLower + this[1:]


Regarding a library reference, we don't have a formal one, but we do have the LibraryTopics wiki page which I just substantially improved. I consider this the "entry point" for learning more about the libraries that are available.

I also encourage people to contribute to and improve the wiki whenever they discover something.

HTH
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 50 guests