Spurious 'variable is never used' warning? - kotlin

I'm trying to clean up my code to get rid of Kotlin warnings, but I don't understand what's causing a couple warnings- I have this code:
var slowestMps:Float? = null
for (r in this.sections.subList(i + 1, this.sections.size - 1)) {
if (!r.locs.any { l -> l.speed <= this.maxReversePointMps}) {
continue
}
val mps = r.m / r.s
if (slowest == null || (slowestMps != null && mps < slowestMps)) {
slowest = r
slowestMps = mps
}
}
And I get the warnings:
Variable 'slowestMps' is never used
The value 'mps' assigned to 'var slowestMps: Float? defined in ... is never used
Why are these warnings getting triggered - aren't these variables being used?

Related

Kotlin android if statement

Kotlin, Android studio, v. 4.0.1
I have a progress bar in my app that ranges from 0 to 10.
When it is at 0, the following code gives an error (which is logic):
val rand = Random().nextInt(seekBar.progress) + 1
resultsTextView.text = rand.toString()
So I want to add an if statement before to filter out the 0. If the progress bar is at 0, the 'rand' should be at 0 too.
I have the following but it does not work:
rollButton.setOnClickListener {
val ggg = seekBar.progress
if (ggg = 0) {
val rand = 0
} else {
val rand = Random().nextInt(seekBar.progress) + 1
}
resultsTextView.text = rand.toString()
}
Any idea?
rand is defined in the scope of if and else, you cannot use it outside that scope and instead of comparing ggg with 0 (==) you are setting its value to 0 (=). And if you want to reassign rand, it cannot be a val which can only be assigned once, make it a var instead.
Do it like this:
rollButton.setOnClickListener {
val ggg = seekBar.progress
var rand = 0;
if (ggg != 0) {
rand = Random().nextInt(seekBar.progress) + 1
}
resultsTextView.text = rand.toString()
}
Replace if (ggg = 0) { with if (ggg == 0) {.
A more Kotlin approach might be along the lines of:
rollButton.setOnClickListener {
val rand = seekBar.progress.let {
if (it == 0)
0
else
Random().nextInt(it) + 1
}
resultsTextView.text = rand.toString()
}
This uses an if() expression (not a statement) to avoid any mutable variables.  And by getting the value of seekBar.progress only once, it also avoids any issues if the bar gets moved while that's running.
However, I have to check if that's actually what you want:
Bar position | Possible values
0 | 0
1 | 1
2 | 1–2
3 | 1–3
… | …
That looks wrong to me…  Do you really want to exclude zero in all but the first case?  If not, then you could just move the addition inside the call — Random().nextInt(seekBar.progress + 1) — and avoid the special case entirely.

how do i correctly use >= and <= in code?

I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===

Verifying sum of sequence under a condition in dafny

I am having a problem with getting an invariant to be be maintained in dafny. The invariant is trying to maintain that a total is equal to the a recursive sum of elements in a sequence that match a given condition. What do i need to add/change to get this to verify. Here is my attempt:
datatype MovieTitle = A | B | C
class Movie {
var title: MovieTitle;
var run_time: int;
predicate Valid()
reads this
{
run_time >= 0
}
constructor(mt: MovieTitle, rt: int)
requires rt >= 0;
ensures Valid();
modifies this;
{
title := mt;
run_time := rt;
}
}
function movieSum(s: seq<Movie>, mt: MovieTitle, i: int): int
requires 0 <= i <= |s|;
decreases s, i;
reads s;
reads set x | x in s[..];
{
if |s| == 0 || i == 0 then 0
else if s[0].title == mt then s[0].run_time + movieSum(s[1..], mt, i - 1)
else movieSum(s[1..], mt, i - 1)
}
lemma {:induction s, mt, i} movieSumLemma(s: seq<Movie>, mt: MovieTitle, i: int)
requires 0 <= i < |s|;
ensures s[i].title == mt ==> (movieSum(s, mt, i) + s[i].run_time == movieSum(s, mt, i + 1)) &&
s[i].title != mt ==> (movieSum(s, mt, i) == movieSum(s, mt, i + 1));
decreases s;
{
}
method SumRuntimes(s: seq<Movie>)
{
var total := 0;
var i := 0;
while i < |s|
invariant 0 <= i <= |s|;
invariant total == movieSum(s, A, i);
decreases |s| - i;
{
if s[i].title == A {
total := total + s[i].run_time;
movieSumLemma(s, A, i);
}
i := i + 1;
}
}
Here invariant total == movieSum(s, A, i); won't hold. Any help in getting this to verify is appreciated. Thank you!
The problem is in function movieSum. You're both chopping off the initial element of s in the recursive call and decrementing i. That will not produce the sum of all the mt-movie elements.
You don't need the lemma. But its postcondition doesn't say what you may think it says. It currently has the form
ensures A ==> B && !A ==> C
The 2-character-wide && has higher binding power than the 3-character-wide ==>. So, your postcondition is saying
ensures A ==> ((B && !A) ==> C)
which simplifies to true. Instead, you want
ensures (A ==> B) && (!A ==> C)
which you can also write on two lines (for better readability):
ensures A ==> B
ensures !A ==> C
Your program also has a number of redundant decreases clauses and :induction attributes. I'm guessing you have added these from the blue information squiggles in the IDE. The blue squiggles are just FYI--Dafny is just trying to tell you what its default settings are.
It seems you may be using a very version of Dafny, or I expected you should have got a warning about the deprecated modifies this on the constructor.

Golang OOP architecture, passing a slice into a constructor, creating a slice object

I just started with go language, everything looks cool, I coded everything I need but I have two problems, actually it is passing an slice into a Struct or so called object, and how to create a method that I can add for example another matrix, but this is minor problem I think.
Code:
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
a := Matrix(nonsingularRandomMatrix())
fmt.Println(a)
}
type Matrix struct {
inputMatrix [][]int
}
func NewThing(inMatrix [][]int) *Matrix {
p := new(Matrix)
p.inputMatrix = inMatrix
return p
}
func nonsingularRandomMatrix() [][]int {
rand.Seed(time.Now().UTC().UnixNano())
var matrixDeterminant int = 0
nm := make([][]int, 4)
for i := 0; i < 4; i++ {
nm[i] = make([]int, 4)
for j := 0; j < 4; j++ {
nm[i][j] = rand.Intn(100)
}
}
matrixDeterminant =
nm[0][0]*nm[1][1]*nm[2][2]*nm[3][3] - nm[0][0]*nm[1][1]*nm[2][3]*nm[3][2] -
nm[0][0]*nm[1][2]*nm[2][1]*nm[3][3] + nm[0][0]*nm[1][2]*nm[2][3]*nm[3][1] +
nm[0][0]*nm[1][3]*nm[2][1]*nm[3][2] - nm[0][0]*nm[1][3]*nm[2][2]*nm[3][1] -
nm[0][1]*nm[1][0]*nm[2][2]*nm[3][3] + nm[0][1]*nm[1][0]*nm[2][3]*nm[3][2] +
nm[0][1]*nm[1][2]*nm[2][0]*nm[3][3] - nm[0][1]*nm[1][2]*nm[2][3]*nm[3][0] -
nm[0][1]*nm[1][3]*nm[2][0]*nm[3][2] + nm[0][1]*nm[1][3]*nm[2][2]*nm[3][0] +
nm[0][2]*nm[1][0]*nm[2][1]*nm[3][3] - nm[0][2]*nm[1][0]*nm[2][3]*nm[3][1] -
nm[0][2]*nm[1][1]*nm[2][0]*nm[3][3] + nm[0][2]*nm[1][1]*nm[2][3]*nm[3][0] +
nm[0][2]*nm[1][3]*nm[2][0]*nm[3][1] - nm[0][2]*nm[1][3]*nm[2][1]*nm[3][0] -
nm[0][3]*nm[1][0]*nm[2][1]*nm[3][2] + nm[0][3]*nm[1][0]*nm[2][2]*nm[3][1] +
nm[0][3]*nm[1][1]*nm[2][0]*nm[3][2] - nm[0][3]*nm[1][1]*nm[2][2]*nm[3][0] -
nm[0][3]*nm[1][2]*nm[2][0]*nm[3][1] + nm[0][3]*nm[1][2]*nm[2][1]*nm[3][0]
if matrixDeterminant == 0 {
nonsingularRandomMatrix()
}
return nm
}
Output from my console:
go run oop.go
command-line-arguments
.\oop.go:10: cannot convert nonsingularRandomMatrix() (type [][]int) to type Matrix
Any clue?
Greets!
What your compiler is saying is that [][]int and Matrix are two totally unrelated types. You can not convert from one to the other. Thats because Matrix is a struct which contains a [][]int, but Matrix is not an [][]int. To create a Matrix out of it you'd need to create a new Matrix and set the [][]int to the only member:
a := Matrix{nonsingularRandomMatrix()}
An easier way would be if Matrix were a [][]int.
type Matrix [][]int
This would just create a new name (Matrix) for [][]int, and in this case, a simple (explicit) conversion would work
Matrix(nonsingularRandomMatrix())

Numeric comparisons yielding incorrect result in awk

I've recently found a script on the site :
bash, find nearest next value, forward and backward
that is relatively old and requires 50 rep to comment on, which I do not have. I'm trying to get it to work, and don't know awk syntax very well, but I'm trying. In the test file I'm using:
-3.793 0.9804E+00 0.3000E+02
-3.560 0.1924E-01 0.3000E+02
-3.327 0.3051E-04 0.3000E+02
-3.093 0.3567E-08 0.3000E+02
-2.860 0.3765E-06 0.3000E+02
-2.627 0.1119E-02 0.3000E+02
-2.394 0.2520E+00 0.3006E+02
and Here's the script:
{
if ($fld > tgt) {
del = $fld - tgt
if ( (del < minGtDel) || (++gtHit == 1) ) {
minGtDel = del
minGtVal = $fld
}
}
else if ($fld < tgt) {
del = tgt - $fld
if ( (del < minLtDel) || (++ltHit == 1) ) {
minLtDel = del
minLtVal = $fld
}
}
else {
minEqVal = $fld
}
}
END {
print (minGtVal == "" ? "NaN" : minGtVal)
print (minLtVal == "" ? "NaN" : minLtVal)
}
which, when run as so :
$ awk -v fld=1 -v tgt=-3 -f awk DOSCAR
produces:
-2.860
NaN
even though there is a lower bound, and I'm not quite sure how to fix it. The original post didn't have negative numbers in it, so they didn't have this problem. Any help is appreciated.
You have a blank line in your input file, which is triggering a classic awk gotcha.
The core problem is the curious behaviour of awk's comparison operators, which do not require you to specify whether you want a numeric or a string comparison. (<opinion>This is precisely why automagical comparison operators are a bad idea.</opinion>)
In short, there are three scalar types in awk: numbers, strings, and "numeric strings". Literals in the program are either numbers or strings, and the result of arithmetic operators is always a number, while the result of a string concatenation is always a string. But the values you are comparing -- $fld and tgt -- are both potentially "numeric strings", because they come from user input.
A "numeric string" is a string which comes from user input, which happens to "look like" a number. On the whole, the definition of "looks like a number" is unsurprising, except for one detail: an empty string does not count.
If you compare two numbers, the comparison is numerical. If you compare two strings, the comparison is lexicographic. But if one (or both) of the things you are comparing is potentially a "numeric string", then the type of the comparison depends on whether the it is actually a "numeric string" or not. If it is a "numeric string", it gets turned into a number; otherwise, the other value gets turned into a string.
Consequently, if $fld is an empty string, then comparing it with tgt will be a string comparison rather than a numeric comparison. And the empty string is the smallest possible string for string comparison, so it will turn out to be smaller. However, when you then compute $fld - tgt, then $fld will be coerced into a number, and in this case the empty string turns into a 0.
So there are two possibilities. The simplest one is to force $fld to be changed to a number; this will at least be consistent:
{
val = $fld + 0
if (val > tgt) {
del = val - tgt
if ( (del < minGtDel) || (++gtHit == 1) ) {
minGtDel = del
minGtVal = val
}
}
else if (val < tgt) {
del = tgt - val
if ( (del < minLtDel) || (++ltHit == 1) ) {
minLtDel = del
minLtVal = val
}
}
else {
minEqVal = val
}
}
END {
print (minGtVal == "" ? "NaN" : minGtVal)
print (minLtVal == "" ? "NaN" : minLtVal)
}
The other way is to eliminate lines where the indicated field cannot be a number. A simple and generally reliable test for numeric values is to compare the value with itself as coerced to a number:
(val = $fld + 0) == $fld {
if (val > tgt) {
del = val - tgt
if ( (del < minGtDel) || (++gtHit == 1) ) {
minGtDel = del
minGtVal = val
}
}
else if (val < tgt) {
del = tgt - val
if ( (del < minLtDel) || (++ltHit == 1) ) {
minLtDel = del
minLtVal = val
}
}
else {
minEqVal = val
}
}
END {
print (minGtVal == "" ? "NaN" : minGtVal)
print (minLtVal == "" ? "NaN" : minLtVal)
}