Velocity is interpreting int i = 0 as boolean false - velocity

If I check var int i = 0 for existing I get wrong result:
#if($i)
<output>$i</output>
#end
results in:
i not defined --> no output
i = 0 --> no output
i = 1 --> <output>1</output>
How can I tell velocity to consider value 0 as defined?
i not defined --> no output
i = 0 --> <output>0</output>
i = 1 --> <output>1</output>

You can check with an operator
#if($i >= 0)
<output>$i</output>
#end

Related

F# type constraints indexable

I'm trying to make a type that should represent a "slice" of some indexable collection.
I know that there are some similar types in F# but not one that specifies the criteria that I need.
To do this it needs to carry a reference to the collection of type 'content and the content needs to be indexable. So I tried this constraint since a type only needs to have the member Item (get/set) so I tried this
type Slice<'a, 'content when 'content: (member Item: int -> 'a)>
This still throw the usual error
So is it possible to constrain a type to still be generic but constraint to be indexable?
I think something like this should work:
type Slice<'a, 'content when 'content: (member get_Item: int -> 'a)> =
{
Content : 'content
Start : int
Stop : int
}
with
member inline slice.get_Item(i) =
slice.Content.get_Item(slice.Start + i)
I've implemented get_Item on Slice as well, so you can take a slice of a slice. Here are some values of this type:
let strSlice =
{
Content = "hello"
Start = 1
Stop = 2
}
let arraySlice =
{
Content = [| 2; 4; 6; 8 |]
Start = 0
Stop = 3
}
let strSliceSlice =
{
Content = strSlice
Start = 0
Stop = 1
}
[<Interface>]
type Indexable<'a> =
abstract member Item: int -> 'a with get
[<Struct>]
type Slice<'a> =
{
content: Indexable<'a>
start: int
len: int
}
with
interface Indexable<'a> with
member I.Item with get(i) = I.[idx]
member S.Item with get(idx) =
if idx >= S.len
then raise(IndexOutOfRangeException())
else S.content.[S.start+idx]
This works.

Kotlin - conditional assign value in one line

Is it possible to conditional assign a value in one line in kotlin?
Setup
var foo: String
if (x != 0) {
foo = x
}
Goal
// nothing is set if condition is false
foo = if (x != 0) x
Not wanted
// null is set if condition is false
foo = takeIf{ x != 0 }.let{ x }
Is
foo = if (x != 0) x else foo
you want?
(Besides, you declared var foo: String, and the x != 0 may indicate a x: Int, then you are not able to foo = x. Maybe a typo here.)
Sure thing
if (x != 0) foo = x
The usual way of doing this would be the following:
if (x != 0) foo = x
This is not always possible, because smart casting cannot be performed if there is a chance that another thread modifies the value between the null check in the assignment. This happens for instance if your x is a nullable var property on the class.
In that case, you have this option:
x?.let { foo = it }

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.

Tail Recursion and Iteration SML

This is my assignment.
(http://prnt.sc/aa3gwd)
I've been working on this one with a tutor and this is what we have come up with so far.
fun mult(a,b) =
let
val product = 0
in
if (a = 0) then
0
else
while a > 0 do
(
product := product + b;
if (a = 1) then
product
else
a:= a -1
);
end;
; //the function did not run at end;, so we added these two semicolons below
;
Output of this is:
stdIn:102.11-103.6 Error: syntax error: deleting SEMICOLON END SEMICOLON
I've only been introduced to SML in the last 2 weeks and I just can't get my head around it. Any help is very much appreciated.
You need two (mutable) reference variables; one for the product and one for the counter.
Something like this:
fun mult(a, b) =
let val product = ref 0
val counter = ref a
in
while !counter > 0 do (
product := !product + b;
counter := !counter - 1
);
!product
end;
(This isn't exactly a translation of the recursive code you linked to, because that code was unnecessarily complicated. You may need to adjust, depending on your professor.)
(I would write the recursive version more like this:
fun mult (0, _) = 0
| mult (_, 0) = 0
| mult (a, b) = b + mult(a - 1, b);
It's unclear why the exercise has a special case for a = 1.)

How to convert bit column to int

I want to convert a bit column to a integer column, do I need a case and convert function at the same time for this?
False = 0
True = 1
You do not need a conversion, because bit is already an integer data type:
An integer data type that can take a value of 1, 0, or NULL.
You can use bits in integer expressions without conversion. Here is a short demo:
create table demo (b bit, v int);
insert into demo (b, v) values (1,5), (0,4), (1, -2), (0, -5);
SELECT b, v, b+v AS b_plus_v FROM demo
Running this produces the following output:
B V B_PLUS_V
- - --------
1 5 6
0 4 4
1 -2 -1
0 -5 -5
EDIT : (based on this comment: "I'm using Code first EF")
Entity Framework requires that a bit column mapped to a bool field. One way to work around this requirement is introducing a computed property to your entity class to hide the "Booleanness" of the underlying column, like this:
partial class MyEntity {
// This code assumes that a bool property MyBoolProperty exists,
// and that it is mapped to the table using EF
public int MyIntProperty {
get {
return MyBoolProperty ? 1 : 0;
}
set {
MyBoolProperty = value != 0;
}
}
}