Ruby elegant way to provide default array values for version string - arrays

This is a trivial question, unsure if I should really be asking here, but would like to see if anyone has a more elegant way to write this in ruby.
I have a version string of format x.y.z or x.y or x. I would like a simple elegant way to convert this into an array where default 0 value is inserted if segment is missing. I have a few different ways to do this, but I was hoping for something a bit cleaner. Atm my current solution is this
version_string.split('.').each_with_index.with_object(['0','0','0']) { |(segment, i), version_array| version_array[i] = segment }
Seems to work fine, and I can always move to a simple method call to make code look cleanup, but something about the use of each_with_index and `with_object kinda bugs me. Just curious to see if rest of Ruby community have anything to add

How about (min Ruby 2.6)
version_string.split('.').then { |x, y = '0', z = '0'| [x,y,z] }

Related

How do I render this Asymptote code into an image?

I don't know anything about asymptote, I asked an AI to generate the code based this geometry problem,
Prove that in a triangle ABC, if the altitudes AD, BE and CF to sides AB, AC and BC respectively, are concurrent, then the triangle is an isosceles triangle.
Here is the Asymptote code it generated,
pointpen = black;
pathpen = black+linewidth(0.7);
size(180);
pair A=(0,0),B=(1,0),C=IP(CR(A,3),CR(B,2)),D=foot(A,B,C),E=foot(B,A,C),F=foot(C,A,B);
D(MP("A",A,SW)--MP("B",B,SE)--MP("C",C,N)--cycle);
D(A--MP("D",D,S)--B);
D(B--MP("E",E,S)--C);
D(C--MP("F",F,S)--A);
D(D);D(E);
D(F);
D(circumcircle(A,B,C));
D(incircle(A,B,C));
I found this renderer online, http://asymptote.ualberta.ca/. When I enter this into the renderer I get this error,
pointpen = black;
^
workspace_1.asy: 1.1: no matching variable 'pointpen'
Update: OK on further investigation seems like the AI may be using some sort of short-hand mapping or making assumptions about pre-existing variables. I think variables like pointpen don't really exist, and maybe this D stands for draw(). Will keep trying.
I am looking at Asymptote's tutorial here (https://asymptote.sourceforge.io/asymptote_tutorial.pdf), and I can't find any of these things like pointpen, pathpen, or these D things here, so this isn't standard Asymptote code? Searched high & low can't really figure out what this is or how to visualize it. Any help would be appreciated thanks!
OK finally figured this out. I think the AI was using some older (or alternative version) of Asymptote.
I think the modern equivalent of the code it generated looks like this,
size(180);
pair A=(0,0),B=(1,0),C=(0.5,sqrt(3)/2),D=(A+B)/2,E=(B+C)/2,F=(A+C)/2;
draw(A--B--C--cycle);
draw(A--D); draw(B--E); draw(C--F);
dot("$A$",A,SW); dot("$B$",B,SE); dot("$C$",C,N); dot("$D$",D,S);
dot("$E$",E,S); dot("$F$",F,S);
Of course I can't be 100% sure this is what the AI intended (does it have intentions?) but this graphic looks like it fits what the geometry problem is asking.
Sorry my original question was super vague.

Is there a Python alternative to IDL's array_indices() function?

I am working on a port of some IDL code to Python (3.7). I have a translation working which uses whatever direct Python alternatives are available, and supplementing what I can with idlwrap. In an effort to eliminate legacy IDL functions from the code, I am looking for an alternative to ARRAY_INDICES(). Right now, I have simply translated the entire function directly and import it on its own. I've spent a good deal of time trying to understand exactly what it does, and even after translating it verbatim, it is still unclear to me, which makes coming up with a simple Python solution challenging.
The good news is I only need it to work with one specific set of arrays whose shapes wont change. An example of the code that will be run follows:
temp = np.sum(arr, axis=0)
goodval = idlwrap.where(temp > -10)
ngood = goodval.size
arr2 = np.zeros_like(arr)
for i in range(0, ngood - 1):
indices = array_indices(arr2, goodval[i])
#use indices for computation

Using multiple assignment to do what array.insert does?

I'm going through the code on a sample solution to my first Ruby Quiz (The Solitaire Cipher), and ran across this little nugget:
def move_down( index )
if index == #deck.length - 1
#deck[1..1] = #deck[index], #deck[1]
#deck.pop
else
...
end
end
The person who wrote this solution apparently used the multiple assignment in the second line to insert #deck[index] into the position before #deck[1]. Why not just use this?
#deck.insert(1, #deck[index])
Is there a difference?
OK, now I see what you mean. Sure, they will give the same result. I guess it just would be matter of choosing which style is more clear for you, or how you think your code would be easier to understand and in consequence more maintainable.
If your question is about which method is more "performant" I don't know that answer and I don't think it evens matters, as ruby is not meant to be performant but to be expressive.

ANTLR and arrays

I have question relating to implementation of arrays with Java+ANTLR combo. (I'm mainly talking about java/c style arrays).
So basically I'm asking how do you implement such feature, if there is such example already available or if someone could point me to anything that may point to solve it.
On other hand, I've searched a bit how would possible solution be. Main problem that I see
is that user may create arrays of various dimensions, even go crazy if he or she wants (like creating 5 dimension arrays or worse).
While grammar for something like this is fairly simple, like
new ID (INT (',' INT)* )
back end really gets involved a bit. As I said, user may input any number of dimensions, so array dimensions should be dynamically created. (at least as I see it, maybe I'm over complicating things?)
After searching I did found something that pretty much solves this problem perfectly, here is link to the question:
Is it possible to dynamically build a multi-dimensional array in Java?
Of course, my question is, is this viable example, it is a bit (to say at least), complicated? Is there more elegant solution to it?
Having that in mind, I was thinking maybe answer might be in the grounds of somehow transforming multidimensions
into more linear structure ? Could something like that be useful ? Simple search on stackoverflow pointed many solutions
to this, like:
Algorithm to convert a multi-dimensional array to a one-dimensional array
Would it be worth to search in that direction ?
Now, at the end, having in mind that arrays are really common feature in many languages, I must find it surprising that after searching ANTLR mailing list there is no similar question, which as I previously said leads me to believe that I'm maybe over complicating things ? (Unless I really suck at search?) I would really appreciate feedback.
Your syntax, if I'm not mistaken, corresponds to something like
new char 4,5,6,7
which is kind of strange. I expect that you really meant
new char[4,5,6,7]
However from a purely syntactic point of view, there's no reason not to just store the indices in an array and let the semantic analysis pass worry about it.

Array.isDefinedAt for n-dimensional arrays in scala

Is there an elegant way to express
val a = Array.fill(2,10) {1}
def do_to_elt(i:Int,j:Int) {
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j))
}
in scala?
I recommend that you not use arrays of arrays for 2D arrays, for three main reasons. First, it allows inconsistency: not all columns (or rows, take your pick) need to be the same size. Second, it is inefficient--you have to follow two pointers instead of one. Third, very few library functions exist that work transparently and usefully on arrays of arrays as 2D arrays.
Given these things, you should either use a library that supports 2D arrays, like scalala, or you should write your own. If you do the latter, among other things, this problem magically goes away.
So in terms of elegance: no, there isn't a way. But beyond that, the path you're starting on contains lots of inelegance; you would probably do best to step off of it quickly.
You just need to check the array at index i with isDefinedAt if it exists:
def do_to_elt(i:Int, j:Int): Unit =
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j))
EDIT: Missed that part about the elegant solution as I focused on the error in the code before your edit.
Concerning elegance: no, per se there is no way to express it in a more elegant way. Some might tell you to use the pimp-my-library-Pattern to make it look more elegant but in fact it does not in this case.
If your only use case is to execute a function with an element of a multidimensional array when the indices are valid then this code does that and you should use it. You could generalize the method by changing the signature of to take the function to apply to the element and maybe a value if the indices are invalid like this:
def do_to_elt[A](i: Int, j: Int)(f: Int => A, g: => A = ()) =
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j)) else g
but I would not change anything beyond this. This also does not look more elegant but widens your use case.
(Also: If you are working with arrays you mostly do that for performance reasons and in that case it might even be better to not use isDefinedAt but perform validity checks based on the length of the arrays.)

Resources