Kotlin val initialization using when - initialization

Using Java I may want to initialize a final variable using a switch statement:
final String finalValue;
switch (condition) {
case 1:
finalValue = "One";
break;
case 2:
finalValue = "Two";
break;
case 3:
finalValue = "Three";
break;
default:
finalValue = "Undefined";
break;
}
In Kotlin, trying to do the same:
val finalValue: String
when (condition) {
1 -> finalValue = "One"
2 -> finalValue = "Two"
3 -> finalValue = "Three"
else -> finalValue = "Undefined"
}
result in a compilation error.
A solutions is using the by lazy combination, but this create a new Lazy instance.
val finalValue: String by lazy {
when (condition) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Undefined"
}
}
Is there a better way to accomplish this?

How about this construction:
val finalValue: String = when (condition) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Undefined"
}
Using when as an expression.

You can also use init block to initialize a final variable.
val finalValue: String
init {
finalValue = when (condition) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Undefined"
}
}

Actually, the following does compile, I'm not sure which problem you encountered?
fun x(condition: Int = 5) {
val finalValue: String
when (condition) {
1 -> finalValue = "One"
2 -> finalValue = "Two"
3 -> finalValue = "Three"
else -> finalValue = "Undefined"
}
}
The only possibility to me: You wrote the when clause into a class body directly, which certainly does not work. You could put it into an init block though.
But of course it's much nicer to simply use the power of when expression here (Which the IDE also suggests):
val finalValue = when (condition) {
1 -> "One"
2 -> "Two"
3 -> "Three"
else -> "Undefined"
}

Related

(Godot Engine) Compute string as PEMDAS

I've been trying to create a function in GDScript to process and calculate a string using PEMDAS rules. Below is my try on the subject. It can so far only use the MDAS rules:
Is there a better way to achieve such a function?
func _ready() -> void:
### USE CASES ###
print(Compute_String("1+2*3+3=")) # Output = 10
print(Compute_String("1+2*3*3=")) # Output = 19
print(Compute_String("1*2*3+3=")) # Output = 9
print(Compute_String("1+2+3*3=")) # Output = 12
print(Compute_String("5*2+7-3/2=")) # Output = 15.5
print(Compute_String("9+5.5*2.25=")) # Output = 21.375
print(Compute_String("5*2+7-3/2")) # Output = 1.#QNAN (Missing equals)
print(Compute_String("5*2+7-/2=")) # Output = 1.#QNAN (Adjacent operators)
print(Compute_String("*2+7-3/2=")) # Output = 1.#QNAN (Begins with operator)
print(Compute_String("")) # Output = 1.#QNAN (Empty)
print(Compute_String("=")) # Output = 1.#QNAN (Considered as empty)
print(Compute_String("1 +2=")) # Output = 1.#QNAN (Contains space)
print(Compute_String("(1+2)*3=")) # Output = 1.#QNAN (Parentheses not supported)
func Compute_String(_string: String) -> float:
var _result: float = NAN
var _elements: Array = []
if not _string.empty() and _string[_string.length() - 1] == "=":
var _current_element: String = ""
for _count in _string.length():
if _string[_count].is_valid_float() or _string[_count] == ".": _current_element += _string[_count]
else:
if _string[_count - 1].is_valid_float() and (_string[_count + 1].is_valid_float() if _string[_count] != "=" else true):
_elements.append_array([_current_element,_string[_count]]) ; _current_element = ""
else: return NAN
if not _elements.empty():
_elements.resize(_elements.size() - 1)
while _get_operators_count(_elements) != 0:
var _id: Array = [0, 0.0, 0.0]
if "*" in _elements:
_id = _add_adjacent(_elements, "*") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] * _id[2])
elif "/" in _elements:
_id = _add_adjacent(_elements, "/") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] / _id[2])
elif "+" in _elements:
_id = _add_adjacent(_elements, "+") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] + _id[2])
elif "-" in _elements:
_id = _add_adjacent(_elements, "-") ; _remove_adjacent(_elements, _id[0]) ; _elements.insert(_id[0] - 1, _id[1] - _id[2])
else: return NAN
if _elements.size() == 1: _result = _elements[0]
return _result
func _get_operators_count(_elements: Array) -> int:
var _result: int = 0 ; for _element in _elements: if not str(_element).is_valid_float(): _result += 1 ; return _result
func _add_adjacent(_elements: Array, _operator) -> Array:
return [_elements.find(_operator), float(_elements[_elements.find(_operator) - 1]), float(_elements[_elements.find(_operator) + 1])]
func _remove_adjacent(_elements: Array, _operator_idx: int) -> void:
_elements.remove(_operator_idx + 1) ; _elements.remove(_operator_idx) ; _elements.remove(_operator_idx - 1)

How to find out if 'map[string][][]int' has a value

Given this code:
var a map[string][][]int
var aa map[string][][]int = map[string][][]int{"a": [][]int{{10, 10}, {20, 20}}}
var bb map[string][][]int = map[string][][]int{"b": [][]int{{30, 30}, {40, 40}}}
fmt.Println(aa) // >> map[a:[[10 10] [20 20]] b:[[30 30] [40 40]]]
how do I know if '[30, 30]' is in 'aa'?
I want to check, whether 'aa' has '[30 30]'.
You'll have to iterate over the contents of your map to check whether an element is contained in that map or not.
For example:
target := []int{30, 30}
for _, v := range myMap {
for _, sub := range v {
if len(sub) == len(target) && sub[0] == target[0] && sub[1] == target[1] {
fmt.Println("yeah")
}
}
}
With myMap as aa you'll get no output, and with myMap as bb you'll get "Yeah" printed.
If your inner-most slices get longer, you should do the check step as a loop as well instead of hard-coded like that.
Maps are only indexed by key. This means its cheap and easy (ideally constant time complexity) to find a or b, but its harder to look for a value (linear time complexity).
Therefore, it's a few for loops:
func find(searchFor [][]int, m map[string][][]int) bool {
for _, v := range m {
if sliceEq(v, searchFor) {
return true
}
}
return false
}
func sliceEq(a, b [][]int) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}

Implementing fizz buzz of higher order using maps in go lang?

I am trying to implement the fizz buzz problem using maps in go lang. However, this code requires improvement in its working. It keeps on printing undesired and redundant results due to the for loop that iterates over the map. I tried a lot of solutions but failed. Is it feasible without using any help of a slice of keys?
package main
import "fmt"
func fizzbuzz(i int) {
myMap:= make(map[int]string)
myMap[3] = "fizz"
myMap[5] = "buzz"
myMap[15] = "fizzbuzz"
for k,v:= range myMap{
if i%k==0 {fmt.Printf("%v \n",v)
} else {fmt.Printf("%v \n",i)}
}
}
func main() {
for i:=1;i<10000;i++ {
fizzbuzz(i)
}
}
With a map
With your rule set, the entire for loop should be to decide if the i number is to be replaced with a word. But you emit a result in each iteration. At most one result should be emitted by the for. If i is not dividable by any of the keys, then i should be emitted.
Keys may be multiples of others (e.g. 15 = 3 * 5), and if the i number is dividable by such a key, we want to emit the word associated with the greatest key. So the for loop should not emit anything, because if you find a good key, there may be a greater one. So the loop should just find the greatest good key.
After the loop you can check if any good key was found, and if so, emit the word associated with it, else emit the number:
var rules = map[int]string{
3: "fizz",
5: "buzz",
15: "fizzbuzz",
}
func fizzbuzz(i int) {
max := -1
for k := range rules {
if i%k == 0 && k > max {
max = k
}
}
if max < 0 {
fmt.Println(i)
} else {
fmt.Println(rules[max])
}
}
func main() {
for i := 1; i < 100; i++ {
fizzbuzz(i)
}
}
Output (try it on the Go Playground):
1
2
fizz
4
buzz
fizz
7
8
fizz
buzz
11
fizz
13
14
fizzbuzz
16
17
fizz
19
buzz
fizz
...
With an ordered slice
You can get better performance if the rules are sorted by the keys descending, in which case you can check the keys in that order (greatest first), and then the first that qualifies will be the greatest. So you can emit the result immediately, and return.
If execution continues after the loop, we know no keys were good, we can emit the i number:
var rules = []struct {
n int
word string
}{
{15, "fizzbuzz"},
{5, "buzz"},
{3, "fizz"},
}
func fizzbuzz(i int) {
for _, rule := range rules {
if i%rule.n == 0 {
fmt.Println(rule.word)
return
}
}
fmt.Println(i)
}
Try this on the Go Playground.
General (excluding multiples from rules)
Although you started with a rule set where 15 = 3 * 5 was included in the rules, this should not be the case; you should only list 3 and 5, 15 should be implicit.
In this case, you have to check all the rules of course, because each good key should emit a word. And you have to remember if a good key was found, and only emit the i number otherwise.
This is how you can do it:
var rules = []struct {
n int
word string
}{
{3, "fizz"},
{5, "buzz"},
}
func fizzbuzz(i int) {
found := false
for _, rule := range rules {
if i%rule.n == 0 {
found = true
fmt.Print(rule.word)
}
}
if !found {
fmt.Print(i)
}
fmt.Println()
}
Try it on the Go Playground.
Note: in this solution you could also use a map instead of the slice; the reason why I used a slice is so that in case of multiple good keys the emitted words will always be in the same order (defined by increasing keys), as iteration order of keys in a map is not defined. For details, see Why can't Go iterate maps in insertion order?
As mentioned, the order of items in a map, is not deterministic in Go. Though here are some simple solutions:
func fizzbuzz(n int) {
for i := 1; i <= n; i++ {
switch {
case i%15 == 0:
println("fizzbuzz")
case i%5 == 0:
println(`buzz`)
case i%3 == 0:
println(`fizz`)
default:
println(i)
}
}
}
func fizzbuzzList(n int) []string {
var res []string
for i := 1; i <= n; i++ {
switch {
case i%15 == 0:
res = append(res, `fizzbuzz`)
case i%5 == 0:
res = append(res, `buzz`)
case i%3 == 0:
res = append(res, `fizz`)
default:
res = append(res, strconv.Itoa(i))
}
}
return res
}
func fizzbuzzLazy(n int) chan string {
var res = make(chan string)
go func() {
for i := 1; i <= n; i++ {
switch {
case i%15 == 0:
res <- `fizzbuzz`
case i%5 == 0:
res <- `buzz`
case i%3 == 0:
res <- `fizz`
default:
res <- strconv.Itoa(i)
}
}
close(res)
}()
return res
}
And usage:
fizzbuzz(20)
for _, v := range fizzbuzzList(20) {
println(v)
}
for v := range fizzbuzzLazy(20) {
println(v)
}

Kotlin - How to recursively call a lambda function

I'm trying to re implement the linrec function from here in Kotlin. Here is what it currently looks like in Kotlin:
fun <A, B> linrec(indivisible: (List<A>) -> Boolean,
value: (List<A>) -> B,
divide: (List<A>) -> List<List<A>>,
combine: (A, B) -> B
) : (List<A>) -> B {
val myfunc: (List<A>) -> B = { input ->
if (indivisible(input)) {
value(input)
} else {
val split = divide(input)
val left = split[0][0]
val right = myfunc(split[1]) // Error
combine(left, right)
}
}
return myfunc
}
IntelliJ gives me the following errors, when I try to run the code:
Error:(40, 19) Kotlin: Unresolved reference: myfunc
My question is: How do I make a lambda function call itself?
You don't call a lambda ("anonymous function") from inside itself. That's what functions are for:
fun <A, B> linrec(indivisible: (List<A>) -> Boolean,
value: (List<A>) -> B,
divide: (List<A>) -> List<List<A>>,
combine: (A, A) -> B
) : (List<A>) -> B {
fun myfunc(input: List<A>): B { // rearranged things here
return if (indivisible(input)) { // added `return`
value(input)
} else {
val split = divide(input)
val left = split[0][0]
val right = myfunc(split[1])
combine(left, right) // *
}
}
return ::myfunc
}
Now this is exactly the code you've wrote, but it does not compile. On the line I marked with * kotlinc says Type mismatch: inferred type is B but A was expected.
P.S. I have no idea what that code is doing, so I only fixed the compilation error you've asked about.

Implement String() on a map with interface{} values

How could I write a function to print a map object in Go (Golang)? Right now I have this, but it doesn't compile. It returns cannot convert value (type interface {}) to type reflect.Kind: need type assertion.
package main
type MyDictionary map[string]interface{}
func (d MyDictionary) String() string {
var stringBuffer bytes.Buffer
for key, value := range d {
stringBuffer.WriteString(key)
stringBuffer.WriteString(": ")
valueType := reflect.Kind(value)
switch valueType {
case reflect.String:
log.Println("string") // just to check if this block gets executed
// Add to stringBuffer
case reflect.Float64:
log.Println("float64") // just to check if this block gets executed
// Add to stringBuffer
default:
log.Println("Error: type was", valueType)
}
}
return stringBuffer.String()
}
func main() {
var dict MyDictionary = make(MyDictionary)
dict["hello"] = "world"
dict["floating"] = 10.0
dict["whole"] = 12
fmt.Println(dict)
}
I want String() to return a string like hello: world\nfloating: 10.0\nwhole: 12\n. That I can then pass to fmt.Println() to print this. In Java, I would use StringBuilder for this.
hello: world
floating: 10.0
whole: 12
I also tried switching on value.(type) with case string: and case float64, but then I didn't know how to write those values to stringBuffer.
Here's an idiomatic solution.
func (d MyDictionary) String() string {
var buf bytes.Buffer
for k, v := range d {
buf.WriteString(k + ": ")
// v is an interface{} here
switch v := v.(type) {
// The inner v is typed. It shadows the outer interface{} v. That's
// the idiomatic part.
case string:
buf.WriteString(v + "\n") // v is a string
case int:
buf.WriteString(fmt.Sprintln(v)) // v is an int
case float64:
buf.WriteString(fmt.Sprintln(v)) // v is a float64
}
}
return buf.String()
}
You can potentially simplify it to this (playground):
func (d MyDictionary) String() string {
var result string
for key, value := range d {
result += fmt.Sprintf("%s: %v\n", key, value)
}
return result
}
Which prints:
hello: world
floating: 10
whole: 12
Obviously, the "whole" floating point has the decimals removed (if you set it to 10.5 it will print properly). If that's required, then you'll want to switch on the float and specify precision as well (playground):
func (d MyDictionary) String() string {
var result string
for key, value := range d {
switch value.(type) {
case float64:
result += fmt.Sprintf("%s: %.2f\n", key, value)
default:
result += fmt.Sprintf("%s: %v\n", key, value)
}
}
return result
}
Which prints:
floating: 10.00
whole: 12
hello: world
You need to get the type of the interface and then switch on the kind of the type.
valueType := reflect.TypeOf(value).Kind()
Working Example: http://play.golang.org/p/a-7SePUzZ-
package main
import (
"bytes"
"fmt"
"log"
"reflect"
)
type MyDictionary map[string]interface{}
func (d MyDictionary) String() string {
var stringBuffer bytes.Buffer
for key, value := range d {
stringBuffer.WriteString(key)
stringBuffer.WriteString(": ")
valueType := reflect.TypeOf(value).Kind()
switch valueType {
case reflect.String:
log.Println("string")
default:
log.Println("Type was:", valueType)
}
}
return stringBuffer.String()
}
func main() {
var dict MyDictionary = make(MyDictionary)
dict["hello"] = "world"
dict["floating"] = 10.0
dict["whole"] = 12
fmt.Println(dict)
}
Output
2009/11/10 23:00:00 string
2009/11/10 23:00:00 Type was: float64
2009/11/10 23:00:00 Type was: int
hello: floating: whole:

Resources