Well, I figured out that if it is taking me ages to write a serious program because something always gets in the way, I could at least start coding small snippets of code. So I started to translate C# code to Cobra to help me familiarize with the language and learn both. I'll be slowly publishing the stuff at http://github.com/gradha/Cobralation.
Keep up the good work. From all the languages I have at hand now, Cobra is the only one that slurrs to my ear "My preciousss... er, code something in it, dammit!". And now, going back to Starcraft 2 (yes, that's what in the way right now).
Forums
Translating stuff to Cobra
15 posts
• Page 1 of 2 • 1, 2
Re: Translating stuff to Cobra
Thanks for your interest in Cobra. Poking around github, it looks you're going to do a series of tutorials translated from C# to Cobra which will be great. For tips on porting, we have a Porting From C# wiki page that will help you. And feel free to update it if you like.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: Translating stuff to Cobra
Wow, looks like the simple array example is proving hard to translate. From http://cobra-language.com/trac/cobra/ticket/27 I see arrays are unexplored territory. I'm unable to replicate the following in Cobra:
Given that multidimensional array don't seem supported, how do you create a fixed size array of arrays? I've tried:
EDIT: Ok, my best attempt at http://github.com/gradha/Cobralation/tr ... %20Arrays/, really ugly.
- Code: Select all
// Array-of-arrays (jagged array)
byte[][] scores = new byte[5][];
Given that multidimensional array don't seem supported, how do you create a fixed size array of arrays? I've tried:
- Code: Select all
x = [](5)
x = (uint8[])[](5)
x = Array<of uint8[]>
EDIT: Ok, my best attempt at http://github.com/gradha/Cobralation/tr ... %20Arrays/, really ugly.
- gradha
- Posts: 23
Re: Translating stuff to Cobra
Theres no direct support for jagged and multidimensional arrays in cobra yet ( search forum for 'arrays' and 'jagged') - only cos its not been needed or asked for.
With the current cobra code the problem is that you cant provide a combination type description (using multiple '[]' and typenames) meaning array-of arrays that cobra will accept ( well I couldnt at least)
so you have to finesse it a little and let it work it out without explicitly telling it using a combination of partial declarations and initialisations (clumsy but possible)
heres the grind for a 2D array
I suspect you could do this rolling your own by defining a type (even genericised)that acts like an array ( is indexable - eg ArrOfInt) and declaring an array of that type but thats even more tedious..
If you reallly really would like 2D ( or more ) arrays supported directly open a ticket for it.
With the current cobra code the problem is that you cant provide a combination type description (using multiple '[]' and typenames) meaning array-of arrays that cobra will accept ( well I couldnt at least)
so you have to finesse it a little and let it work it out without explicitly telling it using a combination of partial declarations and initialisations (clumsy but possible)
heres the grind for a 2D array
- Code: Select all
# these are the array lines
a = int[](10)
b = int[](10)
#This is the 2D array
arr = @[a,b] # initialised to an array literal composed of the array lines
print arr.typeOf # System.Int32[][]
arr[0][0] = 9
print arr[0][0]
print arr[0][1]
I suspect you could do this rolling your own by defining a type (even genericised)that acts like an array ( is indexable - eg ArrOfInt) and declaring an array of that type but thats even more tedious..
- Code: Select all
class ArrOfInt
var _ray as int[]
cue ini (size as int)
_ray = int[](size)
pro [index as int]
get
return _ray[index]
set
_ray[index] = value
# ...
...
arr as ArrOfInt[] = ArrOfInt[](2)(10) # ??? or ArrOfInt[](10)[](2) ???
arr[0][0] = 9
If you reallly really would like 2D ( or more ) arrays supported directly open a ticket for it.
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Translating stuff to Cobra
No need to have. I can see having multidimensional or jagged arrays is more useful for graphic-related operations but I won't be doing anything like that soon. Besides, I'm used to faking it with simple arrays anyway.
- gradha
- Posts: 23
Re: Translating stuff to Cobra
Does anyone see any utility at all in having (very minimal) cobra support for multidimensional and jagged arrays ??
The issue seems to be providing the type explicitly that cobra can recognise and deref as an array correctly
Given that I've got working an experimental tweak/hack/excresence to the cobra compiler so that it will recognise such types (2 and 3D only) but
to construct them you have to drop down into sharp''
everything else seems to just work
Is it worthwhile supplying this as a proper patch???
The issue seems to be providing the type explicitly that cobra can recognise and deref as an array correctly
Given that I've got working an experimental tweak/hack/excresence to the cobra compiler so that it will recognise such types (2 and 3D only) but
to construct them you have to drop down into sharp''
everything else seems to just work
- Code: Select all
def jagged
arr as int[][] = sharp'new int[2][]' # workable dcl - 2D jagged array
arr[0], arr[1] = int[](10), int[](6)
assert arr.typeOf.toString == r'System.Int32[][]'
arr[0][0] = 9
assert arr[0][0] == 9
assert arr[0][1] == 0
print arr
def multiDimen
arr as int[,] = sharp'new int[2,10]' # 2D multidimensional array (Matrix)
assert arr.typeOf.toString == r'System.Int32[,]'
arr[0,0] = 9
assert arr[0,0] == 9
assert arr[0,1] == 0
arr[1,0] = 10
assert arr[1,0] == 10
print arr
arr3D as int[,,] = sharp'new int[2,2,2]' # 3D Multidimensional array (Cubix)
assert arr3D.typeOf.toString == r'System.Int32[,,]'
arr3D[0,0,0] = 9
assert arr3D[0,0,0] == 9
assert arr3D[0,0,1] == 0
arr3D[0,1,0] = 10
assert arr3D[0,1,0] == 10
arr3D[1,1,1] = 111
assert arr3D[1,1,1] == 111
Is it worthwhile supplying this as a proper patch???
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Translating stuff to Cobra
New ticket and patch coming at ya.
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Translating stuff to Cobra
See ticket:253. patch uploaded.
Just to clarify/repeat what this gives us:
-- Syntax for a multidimension or jagged array Type (similar to the existing single dimension array type)
-- such arrays may be 2 or 3 Dimensions only - trying to specify additional dimensions will get you an obscure error message at compilation.
-- such arrays can be constructed with a fallthrough to sharp'' (per example above) or using the literal array example earlier (jagged only) - theres no additional creation support
-- jagged array types are declared
-- multidimensional array types are declared
-- thats it
Post this patch, gradhas tutorial example can be now done
Just to clarify/repeat what this gives us:
-- Syntax for a multidimension or jagged array Type (similar to the existing single dimension array type)
-- such arrays may be 2 or 3 Dimensions only - trying to specify additional dimensions will get you an obscure error message at compilation.
-- such arrays can be constructed with a fallthrough to sharp'' (per example above) or using the literal array example earlier (jagged only) - theres no additional creation support
-- jagged array types are declared
- Code: Select all
TYPE[][] or TYPE[][][]
e.g
String[size][] # 2D jagged String array uninitialised
Decimal[2][3][] # 3D jagged Decimal Array uninitialised
-- multidimensional array types are declared
- Code: Select all
TYPE[,] TYPE[,,]
e.g.
String[,] # 2D String array
File[,,] # 3 Dimension array of File
-- thats it
Post this patch, gradhas tutorial example can be now done
- Code: Select all
class DeclareArraysSample
def main
# Single-dimensional array
numbers = int[](5)
# Multidimensional arrays are minimally supported in Cobra. Use sharp for construction
names as String[,]= sharp'new string[5, 4]' # 2D string array, all nil
#print names
# Array-of-arrays (jagged array)
scores as uint8[][]= sharp'new byte[5][]' # 2D jagged byte/uint8 array
# Create the jagged or nested array.
for i in scores.length
scores[i] = uint8[](i + 3)
for i in scores.length
print "Length of row [i] is [scores[i].length]"
- hopscc
- Posts: 632
- Location: New Plymouth, Taranaki, New Zealand
Re: Translating stuff to Cobra
I just tried applying the latest release (10-18-10) to get the multidimensional support (I'm doing a test run port of something from boo to Cobra), and am perplexed.
My code is as follows:
But I get the error :<path>\Tetris.cobra(13,27): error: Expecting RBRACKET, but got "," (COMMA) instead.
Am I using the right syntax? Or perhaps the new version of Cobra didn't take (not sure how to figure out which version I have, since it just says 0.8.0 post-release). I don't think it is the use of properties, as when I removed them, I still got the error.
TIA!
My code is as follows:
- Code: Select all
class Figure
pro matr from var as int[,]
But I get the error :<path>\Tetris.cobra(13,27): error: Expecting RBRACKET, but got "," (COMMA) instead.
Am I using the right syntax? Or perhaps the new version of Cobra didn't take (not sure how to figure out which version I have, since it just says 0.8.0 post-release). I don't think it is the use of properties, as when I removed them, I still got the error.
TIA!
- torial
- Posts: 229
- Location: IA
15 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 16 guests