Does Cobra have any operator to allow list concatenation?
I found some discussions on these forums that indicated why + is not concatenation for lists, and talked about using &. But I don't see any indication that the & functionality was ever added. Am I missing something? If not, is this still planned? It sure feels clumsy to use the .NET level functionality (and I think it gave me a lot of headaches with dynamic and ? versions of lists and list elements).
I can go back and work out the confusion I ran into trying to concatenate string lists, if it would be helpful.
- Caligari
Forums
List concatenation
19 posts
• Page 1 of 2 • 1, 2
Re: List concatenation
We're sticking with + for concatenation and it currently works with strings.
.NET has .addRange, but it modifies the receiver and returns nothing, so it is not expression friendly. Other than that, it works fine.
In response to your inquiry, I whipped up the following extension method which is expression friendly:
If we add + support for lists, what should it do if both sides are the nilable type (List<of T>?) and end up being nil at run-time?
.NET has .addRange, but it modifies the receiver and returns nothing, so it is not expression friendly. Other than that, it works fine.
In response to your inquiry, I whipped up the following extension method which is expression friendly:
extend IList<of T>
def concated(other as IList<of T>?) as IList<of T>
"""
Returns a new list with the contents of this list and the other.
Does not modify this list or the other.
The returned list is the same class as the receiver.
Assumes the receiving type has an initializer that takes a .
"""
type = .typeOf
newList = type(.count + if(other,other.count,0))
newList.addRange(this)
if other, newList.addRange(other)
return newList
class X
def main is shared
t = [1, 2]
u = [3, 4]
# print t + u # not supported yet
# print t.addRange(u) # no good. addRange returns nothing
trace t, u
trace t.concated(u)
If we add + support for lists, what should it do if both sides are the nilable type (List<of T>?) and end up being nil at run-time?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: List concatenation
If both are nilable, and nil at runtime, shouldn't we return nil? We haven't got any actual data, so there isn't really anything else we can do, is there?
I'll grab the changes (assuming they are in the repository) and try my code with the new Cobra and let you know if there was anything else that still confuses me.
- Caligari
I'll grab the changes (assuming they are in the repository) and try my code with the new Cobra and let you know if there was anything else that still confuses me.
- Caligari
- Caligari
- Posts: 33
Re: List concatenation
Sorry I didn't check in that extension yet. I wrote it fast and I'm tired. I'll take a fresh look at it this week.
Maybe you can copy it and test it out?
Maybe you can copy it and test it out?
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: List concatenation
Well, I've taken an initial stab at this, which has reminded me about one of the issues I encountered.
I've added the concated extension as above. My test is:
And when I compile I get:
So, I'm not sure why I've getting List<of ...> rather than IList<of ...>. This definitely confused me when I was first tangling with this, and I ended up with:
Which seems like a lot more "manual" setup than I expected I'd need to do in order to use lists of strings. Am I missing something here?
- Caligari
I've added the concated extension as above. My test is:
- Code: Select all
class TestList
def main is shared
listArgs = ["-l", "-list"]
ignoreArgs = ["-i", "-ignore"]
nameArgs = ["-n", "-name", "-names"]
summaryArgs = ["-s", "-summary"]
helpArgs = ["-h", "-help"]
allArgs = listArgs.concated(ignoreArgs.concated(nameArgs.concated(summaryArgs.concated(helpArgs))))
print allArgs
And when I compile I get:
- Code: Select all
TestList.cobra(23): error: Cannot find a definition for "concated" in "summaryArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "nameArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "ignoreArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "listArgs" whose type is "List<of String>".
Compilation failed - 4 errors, 0 warnings
So, I'm not sure why I've getting List<of ...> rather than IList<of ...>. This definitely confused me when I was first tangling with this, and I ended up with:
- Code: Select all
class TestList
def main is shared
listArgs as List<of String?> = ["-l", "-list"]
ignoreArgs as List<of String?> = ["-i", "-ignore"]
nameArgs as List<of String?> = ["-n", "-name", "-names"]
summaryArgs as List<of String?> = ["-s", "-summary"]
helpArgs as List<of String?> = ["-h", "-help"]
allArgs = List<of String?>()
allArgs.addRange(listArgs)
allArgs.addRange(ignoreArgs)
allArgs.addRange(nameArgs)
allArgs.addRange(summaryArgs)
allArgs.addRange(helpArgs)
Which seems like a lot more "manual" setup than I expected I'd need to do in order to use lists of strings. Am I missing something here?
- Caligari
- Caligari
- Posts: 33
Re: List concatenation
Caligari wrote:Well, I've taken an initial stab at this, which has reminded me about one of the issues I encountered.
I've added the concated extension as above. My test is:
- Code: Select all
class TestList
def main is shared
listArgs = ["-l", "-list"]
ignoreArgs = ["-i", "-ignore"]
nameArgs = ["-n", "-name", "-names"]
summaryArgs = ["-s", "-summary"]
helpArgs = ["-h", "-help"]
allArgs = listArgs.concated(ignoreArgs.concated(nameArgs.concated(summaryArgs.concated(helpArgs))))
print allArgs
And when I compile I get:
- Code: Select all
TestList.cobra(23): error: Cannot find a definition for "concated" in "summaryArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "nameArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "ignoreArgs" whose type is "List<of String>".
TestList.cobra(23): error: Cannot find a definition for "concated" in "listArgs" whose type is "List<of String>".
Compilation failed - 4 errors, 0 warnings
It works for me. No compilation errors or warnings.
What version of Cobra are you on?
Also, I was picturing that .concated would be used:
a.concated(b).concated(c).concated(d)
# analogous to:
a + b + c + d
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: List concatenation
Ah. I've been running install-from-workspace.bat by clicking on it.
I run it from a command prompt now, and I find that
Which I will go out on a limb and suggest might be the reason that things are not behaving quite as you'd expect.
I'll have a look. My main drive is I:, which I've patched in the past, but perhaps something has changed in there that I missed this time around.
- Caligari
I run it from a command prompt now, and I find that
- Code: Select all
==== Build new Cobra compiler
run: Snapshot\cobra.exe -compile -turbo -ert:yes -files:files-to-compile.text
: CommandLine.cobra(501): error: Cannot find "CobraMain".
: Compilation failed - 1 error, 0 warnings
** ERROR: Exit code from running SnapshotCobra: 1
Which I will go out on a limb and suggest might be the reason that things are not behaving quite as you'd expect.
I'll have a look. My main drive is I:, which I've patched in the past, but perhaps something has changed in there that I missed this time around.
- Caligari
- Caligari
- Posts: 33
Re: List concatenation
OK, commenting out that line (501 in CommandLine.cobra), I now run into this, which seems odd not to have caused trouble earlier:
From what I can tell, indeed Utils.cobra doesn't contain a Main of any sort, and hasn't been altered recently to remove it. So I'm not sure why this would only be a problem now.
- Caligari
- Code: Select all
run: Snapshot\cobra.exe -compile -turbo -ert:yes -files:files-to-compile.text
: i:\Cobra\cobra-workspace\Source\Utils.cobra(368): warning: "System.Reflection.Assembly.LoadWithPartialName(string)" is obsolete: "This method has been deprecated. Please use Assembly.Load() instead. http://go.microsoft.com/fwlink/?linkid=14202"
: error: Program "i:\Cobra\cobra-workspace\Source\Utils.exe" does not contain a static "Main" method suitable for an entry point
: Compilation failed - 1 error, 1 warning
** ERROR: Exit code from running SnapshotCobra: 1
From what I can tell, indeed Utils.cobra doesn't contain a Main of any sort, and hasn't been altered recently to remove it. So I'm not sure why this would only be a problem now.
- Caligari
- Caligari
- Posts: 33
Re: List concatenation
Sorry, I broke this a couple of days ago when "cobra.cobra", which contains the .main, was extracted out of files-to-compile.text to put on the command line.
Although this problem is not in the last informal release, it affects people working straight out of subversion.
Now fixed. Update and try again.
Although this problem is not in the last informal release, it affects people working straight out of subversion.
Now fixed. Update and try again.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
Re: List concatenation
Oh and if you have modified the installer, the install paths were extracted to be more towards the top in a var:
So if you want a different path, you'll want to review that.
var installDirectories = [r'C:\Cobra', '/usr/local/cobra']
So if you want a different path, you'll want to review that.
- Charles
- Posts: 2515
- Location: Los Angeles, CA
19 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 73 guests