Idiomatic way of using StringBuilder in kotlin? [closed] - string

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 3 years ago.
Improve this question
I often write pretty complex toString() methods and this question is always bothering me - which variant is more clear to read. Following examples are simplified, usually there are a lot of conditionals, so single liners are not fit.
1) like in plain java:
val sb = StringBuilder()
sb.append(data)
val string = sb.toString()
2) apply + toString() - not pretty yeah?
val string = StringBuilder().apply {
append(data)
}.toString()
3) run + toString() last statement also is not superb
val string = StringBuilder().run {
append(data)
toString()
}
4) ??

#dyukha answer is 100% best choice:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/build-string.html
It's just
val s = buildString { append(data) }

You could skip the StringBuilder and use Kotlin's built-in String Interpolation:
val string = "$data"
Or if things are more complicated:
val string = "The answer is: $data"
Or, using raw strings:
val string =
"""
{
"name": $name,
"value": $value
}
"""

Related

Converting string to float using Arduino [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am trying to convert the string in my code to float but using atof() for a sub string seems not be working. Advise on how to go about it appropriately will be appreciated.
It is for a BME280 sensor using 433MHz transmitter and receiver
void loop{
if(rfrx.recv(buff, &bufflen)){
String rxstr = String((char*)buff);
for (int i=0;i=rxstr.length();i++){
if (rxstr.substring(i,i+1) == ","){
String T = rxstr.substring(0,i);
String P = rxstr.substring(i+1);
String A = rxstr.substring(i+2);
String H = rxstr.substring(i+3);
break;
}
}
}
}
Have figured it out
The .toFloat()
Thanks

If length is 2 do Haskell [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'm new to Haskell and I'm not sure how to work around the If-Else, for example:
function str = if ((length str) = 2) then (....)
In java we would:
if (str.length =2){
str = "2"}
else { str ="1"}
How do you write it in haskell?
You can use Guards:
fnc :: String -> String
fnc s | length s == 2 = ...
| otherwise = ...
More to Guards
Or conditions
fnc :: String -> String
fnc s = if length s == 2 then ... else ...
It is also possible to use pattern matching, more here.
There are several ways to achieve conditions (e.g. case of) in Haskell.

Using value itself without the name of value in Python 3 [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 2 years ago.
Improve this question
Is there any way to use variable itself inside without it's name?
For example, I have a string like this:
someStuffVariableName = "abcdefghijklmnop..."
If I want to manipulate it, I need to write every time name of this var but it's so long:
someStuffVariableName = someStuffVariableName[0:-1]
But,anyway,can I do like this:
someStuffVariableName = self[0:-1] or someStuffVariableName = this.value[0:-1]?
There is not.
Your best options are:
Use a more concise variable name (but don't give up readability!)
Just deal with the length
Note that in some cases, the answer is actually yes. For instance, you can often write x += y instead of x = x + y, and x /= y instead of x = x / y.
But this is for assignment operators only.

Why does sorted "tan" != "ant"? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to sort the characters in a string by sorting a slice of the bytes in the string (using sort.Slice). The code I'm using gets the right results sometimes but other times produces results I can't make sense of.
package main
import (
"fmt"
"sort"
)
func main() {
for _, s := range []string{"nat", "tan", "ant"} {
b := []byte(s)
sort.Slice(b, func(i int, j int) bool { return s[i] < s[j] })
fmt.Println(s, string(b))
}
}
https://play.golang.org/p/bC9QWq7aF3G
I would expect "nat", "tan" and "ant" to all be sorted to "ant", but "tan" is sorted to "atn".
Change your sort.Slice line to:
sort.Slice(b, func(i int, j int) bool { return b[i] < b[j] })
sort.Slice needs your less function to compare values in the slice in order to sort the way you intended. Your bug is that you used s rather than b in your less function.

Getting maximum Key Values in Map [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 5 years ago.
Improve this question
How do I get maximum key values in a Map and let's say save it to a List?
For example, is there is a Map:
John, 30
Alexander, 10
Ivan, 20
Steven, 30
The result must be a List: John, Steven
Without additional List...
Double max = 0d;
for (String key : wagesList.keySet()) {
if (wagesList.get(key) > max) {
max = wagesList.get(key);
}
}
for (String key : wagesList.keySet()) {
if (wagesList.get(key).equals(max)) {
System.out.println(key);
}
}

Resources