Why can you not specify var/val loops in Kotlin? [closed] - kotlin

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Why can't you specify a val or var type in for loop's in Kotlin. For example, I would like to be able to do
for (var i in 0...data.size - 1) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]//cant do i++ in current kotlin because "i" is val
}
//do stuff
}
But instead I have to do this
var i = 0
while (i < data.size) {
for (j in 0..bytes.size - 1) {
bytes[j] = data[i++]
}
//do stuff
}

Your example is slightly different from Java's typical for(int i=0;i<data.size;i++) example.
In the Kotlin version 'i' is actually an element of the range in which case i++ doesn't make sense. It just so happens that the range you have is a list of indexes.
The way you are using the Kotlin for loop is much closer to Java's foreach loop for(i : indexes).

I think that because Kotlin is a language that tries to make easy to respect most of the functional programming concepts, it prefers to prohibit that sort of behaviour. Also, a possible problem that your initial code could encount is an OutOfBoundException in the situation where the bytes array has more elements than the data array.

Related

awk function to compute median of an array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to have an awk function to compute the median of an array (may not be sorted).
median of column with awk
The above is a similar post but it is not the same as this one because the input on the above post is a sorted stream.
Could anybody show me the function to compute the median of an array?
I got the following solution. I am not sure if it would fail in a corner case. If it does, please let me know.
function median(x, n, d) {
n = asort(x, d, "#val_num_asc")
if(n == 0) return "NA"
if(n%2) {
return d[(n+1)/2]
} else {
return (d[n/2] + d[n/2+1])/2
}
}

Objective-C programming [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
*warning: iteration 5u invokes undefined behavior [-Waggressive-loop-optimizations]
if([user1 isEqualToString:account1[i].name])
^
main.m:33:2: note: containing loop
for(i=0;i<=6;i++)
^
please somebody correct this code
Almost certainly you have:
Thing account1[6] = { ... };
for (i = 0; i <= 6; i++)
{
if ([user1 isEqualToString:account1[i].name])
and the compiler knows that <= 6 will go beyond the bounds of the array (last index is 5 not 6).
To correct:
for (i = 0; i < 6; i++)
^

Objective-C to Swift Equivilant [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to figure out how to write this block method in swift. I can't seem to get the closure syntax right:
[self.colorPickerView setDidChangeColorBlock:^(UIColor *color){
self.selectedColor.backgroundColor = self.colorPickerView.color;
}];
Thanks in advance!
What i tried:
self.colorPickerView.didChangeColorBlock({
(color: UIColor) in self.selectedColorView.backgroundColor = self.colorPickerView.color
})
Final Solution:
self.colorPickerView.didChangeColorBlock = {
(color: UIColor!) in
self.selectedColorView.backgroundColor = color
}
You probably need to use
self.colorPickerView.didChangeColorBlock = { ...
instead of
self.colorPickerView.didChangeColorBlock({ ...
Since didChangeColorBlock is a property, not a method.

vb. net search about sentence and return the first word index [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
i was asked a question here but i forget to explain my problem well.. so my problem is that I have a huge string in arabic which i want to search about a sentences in it and if i found it return the first word index (word index not character) ..
for example:
Dim myString as String = "Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.. Fundamentally programs manipulate numbers and text."
so.. when i search about (programs manipulate) i want to return: 1 and 36 .. any suggestions by the best way ? if with linq i will appreciated .. thanx
You can use something similar to this.
class Program
{
public static void Main(string[] args)
{
var str = "Fundamentally programs manipulate numbers and text. These are the building blocks of all programs. Programming languages let you use them in different ways, eg adding numbers, etc, or storing data on disk for later retrieval.. Fundamentally programs manipulate numbers and text.";
foreach (var index in GetIndexes(str, "programs manipulate",' '))
{
Console.WriteLine(index);
}
Console.ReadKey();
}
public static IEnumerable<int> GetIndexes(string str, string search,params char[] delimiters)
{
var index = 0;
var words = str.Split(delimiters).ToList();
var searchwords = search.Split(delimiters);
while (words.Any())
{
if (words.Take(searchwords.Length).SequenceEqual(searchwords))
yield return index;
words.RemoveAt(0);
index += 1;
}
}
}

Why does this code work in Objective-C? I'm getting BAD_ACCESS [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
int RandomSource_next(int bits, double* seed) {
*seed = (((long long) *seed * 0x5DEECE66DLL) + 0xBLL) & ((1LL << 48) - 1);
return (int)((signed long long) *seed >> (48 - bits));
}
I think it got something to do with the address.
Most probably you are passing incorrect address as seed. Maybe you're passing not an address but a value?
The following should work
double seed = 0;
RandomSource_next(48, &seed);
The following should crash
RandomSource_next(48, 0);