Simple Scala Function Not Removing Double Spaces - scala

For some reason when I input a string with double spaces such as " ", the function does not remove them from the string, nor does it remove them when they are generated by two WUB's in a row
For example:
songDecoder("WUBCATWUBWUBBALLWUB") outputs "CAT_ _BALL" (underscores represent spaces)
I could fix this by other means, but since I have no idea why my current code isn't working I figured I should ask to patch my understanding.
def songDecoder(song:String):String = {
val l = song.indexOf("WUB")
if (song.contains(" ")) {
val e = song.indexOf(" ")
songDecoder(song.patch(e,Nil,1))
}
if (l==0) {
val c = song.patch(l,Nil,3)
songDecoder(c)
}
if (l== -1)
song.trim
else {
val c = song.patch(l,Nil,2)
val b = c.patch(l," ",1)
songDecoder(b)
}
}

The reason it doesn't work is because when you call a recursive method it eventually returns with its result. The code that clears out the double-whitespace doesn't save that result.
if (song.contains(" ")) {
val e = song.indexOf(" ")
songDecoder(song.patch(e,Nil,1)) //send patched song to decoder
} //don't save returned string
//continue with unpatched song
The 2nd if block also recurses without saving the result.
if (l==0) {
val c = song.patch(l,Nil,3)
songDecoder(c) //send patched song to decoder
} //don't save returned string
//continue with unpatched song
You can remove both of those if blocks and you'll get the same results from your method. The only code that effects the output is the final if/else and that's because it is at the end of the method's code block. So whatever the if/else produces that's what the method returns.
if (l== -1)
song.trim //return the final result string
else {
val c = song.patch(l,Nil,2) //remove one WUB
val b = c.patch(l," ",1) //replace with space
songDecoder(b) //return whatever the next recursion returns
}
Just as an FYI, here's a different approach.
def songDecoder(song:String):String =
"(WUB)+".r.replaceAllIn(song, " ").trim

How about something like:
song.split(“(WUB)+”).mkString(“ “).trim

Related

Method returns unit instead of string in scala

I am trying to get the string out of the Set type in Scala. However, no matter what I try it is returning Unit as a return type instead of a string.
if (!validationMessages.isEmpty) {
dataTuple = (parsedJson.toString(), for (s <- validationMessages.toString()){
s.toString()
})
println(dataTuple)
}
else {
dataTuple = (parsedJson.toString(), "some string...")
}
Basically, I am trying to return a tuple like (String, String). What am I missing?
The value of a block is the value of the last expression in the block. In this case the value of the if part is
println(dataTuple)
and the value of the else part is
dataTuple = (parsedJson.toString(), "some string...")
Both of these expressions return Unit so the result is Unit.
The quick fix is to make dataTuple the last statement in each branch:
if (!validationMessages.isEmpty) {
dataTuple = (parsedJson.toString(), for (s <- validationMessages.toString()){
s.toString()
})
println(dataTuple)
dataTuple
} else {
dataTuple = (parsedJson.toString(), "some string...")
dataTuple
}
But also consider something like this as a simpler solution:
val msg =
if (validationMessages.nonEmpty) {
validationMessages.mkString(", ")
} else {
"some string"
}
(parsedJson.toString, msg)
The mkString is an attempt to replace this code:
for (s <- validationMessages.toString()) {
s.toString()
}
This code appears to be trying to create a string by combining all the validation messages, but it actually returns Unit because the for does not have a yield. mkString calls toString on each element of the Set and then creates a string by putting ", " between each element. This seems to be roughly what is wanted and should be easy to modify to the actual requirement.
Why is the code behaving like this?
That code has return type Unit, because that is the return type of both branches of the if statement.
The if branch ends with println(dataTuple), which returns Unit.
The else branch ends with dataTuple = ..., which is variable assignment, which returns Unit. If you wanted it to return the tuple stored in dataTuple, you'd just have to add dataTuple to the end.
What do you probably want to do instead?
(parsedJson.toString, validationMessages.mkString("[", ", ", "]"))
That will return a tuple containing first parsedJson as a string, and second, each element of validationMessages, in arbitrary order (because Set is unordered), starting with [, with a , between consecutive elements, and ] at the end.
So if parsedJson was {"key1": "value1"} and validationMessages was Set("unexpected key: key1", "missing key: key0") this would return ("{"key1":"value1"}",["unexpected key: key1", "missing key: key0"].
If you don't want to put something special at the beginning/end of your .mkString, you can call it with only the middle argument (the delimiter).
If validationMessages is empty, then validationMessages.mkString("delimiter") returns an empty string, and validationMessages.mkString("start", "delimiter", "end") returns "startend".
One of the beautiful things about scala is how you can do complex things in a single line (as long as you know the language well enough).

How to return a variable in a function in kotlin

I created a function that recieves input and compare it to a list, when find a match it return the match, in this case this match is the attribute of a class that i created.
I understand that the problem is with the return statement, so in the beginning of the function I declare the return as "Any", further more than that I'm kinda lost.
The error is this: A 'return' expression required in a function with a block body ('{...}')
class Class1(var self: String)
var test_class = Class1("")
fun giver(){
test_class.self = "Anything"
}
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self)
var lista_de_listas = listOf<String>("test_class.self")
var count = -1
for (i in lista_de_listas){
count = count + 1
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
}
}
}
}
fun main(){
giver()
var x = "test_class.self"
var funcion = Funciones()
var y = funcion.match_finder(x)
println(y)
}
To explain you what the problem is, let's consider the following code:
class MyClass {
fun doSomething(): String {
val numbers = listOf(1, 2, 3)
for (number in numbers) {
if (number % 2 == 0) {
return "There is at least one even number in the list"
}
}
}
}
If you try compiling it you'll get the same error message as in your question: A 'return' expression required in a function with a block body ('{...}'). Why is that?
Well, we defined a function doSomething returning a String (it could be any other type) but we're returning a result only if the list of numbers contains at least one even number. What should it return if there's no even number? The compiler doesn't know that (how could it know?), so it prompts us that message. We can fix the code by returning a value or by throwing an exception:
class MyClass {
fun doSomething(): String {
val numbers = listOf(1, 2, 3)
for (number in numbers) {
if (number % 2 == 0) {
return "There is at least one even number in the list"
}
}
// return something if the list doesn't contain any even number
return "There is no even number in the list"
}
}
The same logic applies to your original code: what should the function return if there is no i such that texto == i?
Please also note that the solution you proposed may be syntactically correct - meaning it compiles correctly - but will probably do something unexpected. The for loop is useless since the if/else statement will always cause the function to return during the first iteration, so the value "There is no match" could be returned even if a match actually exists later in the list.
I searched online, if someone has the same problem, the correct code is as follows:
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self)
var lista_de_listas = listOf<String>("test_class.self")
var count = -1
var variable = " "
for (i in lista_de_listas){
count = count + 1
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
} else {
return "There is no match"
}
}
return variable
}
}

Lexical Analyzer not getting the next character

So I am working on a project where we are making a small compiler program but before I can move on to the other parts I am having troubles with getting the lexical analyzer to output anything after '\BEGIN' afterwards I debugged it and it seems the value is stuck in a loop where the condition is saying the next character is always a newline. Is it because I haven't added the pattern matching yet to the defined tokens?
Here is the code
import java.util
//import com.sun.javafx.fxml.expression.Expression.Parser.Token
/*Lexical analyzer will be responsible for the following:
- finds the lexemes
- Checks each given character determining the tokens
* */
class MyLexicalAnalyzer extends LexicalAnalyzer {
//Array full of the keywords
//val SpecialCharacters = List(']', '#', '*', '+', '\\', '[', '(',')', "![", '=')
val TEXT = "[a-z] | _ | 0-9 | [A-Z]:"
private var sourceLine: String = null
private val lexeme: Array[Char] = new Array[Char](999)
private var nextChar: Char = 0
private var lexLength: Int = 0
private var position: Int = 0
private val lexems: util.List[String] = new util.ArrayList[String]
def start(line: String): Unit = {
initializeLexems()
sourceLine = line
position = 0
getChar()
getNextToken()
}
// A helper method to determine if the current character is a space.
private def isSpace(c: Char) = c == ' '
//Defined and intialized tokens
def initializeLexems(): Any = {
lexems.add("\\BEGIN")
lexems.add("\\END")
lexems.add("\\PARAB")
lexems.add("\\DEF[")
lexems.add("\\USE[")
lexems.add("\\PARAE")
lexems.add("\\TITLE[")
lexems.add("]")
lexems.add("[")
lexems.add("\\")
lexems.add("(")
lexems.add(")")
lexems.add("![")
lexems.add("=")
lexems.add("+")
lexems.add("#")
}
//val pattern = new regex("''").r
def getNextToken() ={
lexLength = 0
// Ignore spaces and add the first character to the token
getNonBlank()
addChar()
getChar()
// Continue gathering characters for the token
while ( {
(nextChar != '\n') && (nextChar != ' ')
}) {
addChar()
getChar()
}
// Convert the gathered character array token into a String
val newToken: String = new String(lexeme)
if (lookup(newToken.substring(0, lexLength)))
MyCompiler.setCurrentToken(newToken.substring(0,lexLength))
}
// A helper method to get the next non-blank character.
private def getNonBlank(): Unit = {
while ( {
isSpace(nextChar)
}) getChar()
}
/*
Method of function that adds the current character to the token
after checking to make sure that length of the token isn't too
long, a lexical error in this case.
*/
def addChar(){
if (lexLength <= 998) {
lexeme({
lexLength += 1; lexLength - 1
}) = nextChar
lexeme(lexLength) = 0
}
else
System.out.println("LEXICAL ERROR - The found lexeme is too long!")
if (!isSpace(nextChar))
while ( {
!isSpace(nextChar)
})
getChar()
lexLength = 0
getNonBlank()
addChar()
}
//Reading from the file its obtaining the tokens
def getChar() {
if (position < sourceLine.length)
nextChar = sourceLine.charAt ( {
position += 1;
position - 1
})
else nextChar = '\n'
def lookup(candidateToken: String): Boolean ={
if (!(lexems.contains(candidateToken))) {
System.out.println("LEXICAL ERROR - '" + candidateToken + "' is not recognized.")
return false
}
return true
}
}
else nextChar = '\n'<- this is where the condition goes after rendering the first character '\BEGIN' then just keeps outputting in the debug console as listed below.
This is what the debug console it outputting after '\BEGIN' is read through
Can anyone please let me know why that is? This happens after I keep stepping into it many times as well.
Here is the driver class that uses the lexical analyzer
import scala.io.Source
object MyCompiler {
//check the arguments
//check file extensions
//initialization
//get first token
//call start state
var currentToken : String = ""
def main(args: Array[String]): Unit = {
val filename = args(0)
//check if an input file provided
if(args.length == 0) {
//usage error
println("USAGE ERROR: Must provide an input file. ")
System.exit(0)
}
if(!checkFileExtension(args(0))) {
println("USAGE ERROR: Extension name is invalid make sure its .gtx ")
System.exit(0)
}
val Scanner = new MyLexicalAnalyzer
val Parser = new MySyntaxAnalyzer
//getCurrentToken(Scanner.getNextToken())
//Parser.gittex()
for (line <- Source.fromFile(filename).getLines()){
Scanner.start(line)
println()
}
//.......
//If it gets here, it is compiled
//post processing
}
//checks the file extension if valid and ends with .gtx
def checkFileExtension(filename : String) : Boolean = filename.endsWith(".gtx")
def getCurrentToken() : String = this.currentToken
def setCurrentToken(t : String ) : Unit = this.currentToken = t
}
The code is operating as it is supposed to. The first line contains only the string \BEGIN so the lexical analyser is treating the end of the first line as an '\n' as shown in this method:
def getChar() {
if (position < sourceLine.length)
nextChar = sourceLine.charAt ( {
position += 1;
position - 1
})
else nextChar = '\n'
However, the comment directly above that method does not describe what the method actually does. This could be a hint as to where your confusion lies. If the comment says it should read from the file, but it is not reading from the file, maybe that's what you've forgotten to implement.

How to block the return until a timer expires using RxJava

I'm not seeing anything ever get returned by the scan. I know it's because the mutableList gets returned right away, but how do I block the return until the time expires?
Basically, all I want to do is fill up the mutable list for as long as the take() permits then return that mutableList to the calling function.
This is what I have tried.
private val timeoutScheduler: Scheduler = Schedulers.computation()
fun scanForAllDevicesStartingWith(devicePrefix: String): List<String> {
Log.d(TAG, "Scanning for devices starting with $devicePrefix")
val mutableList = mutableListOf<String>()
val result = scanForDevices()
.take(3, TimeUnit.SECONDS, timeoutScheduler)
.subscribe { scanResult ->
val name = scanResult.bleDevice.name
Logger.d(TAG, "Potential device named $name found")
if(name != null) {
if(name.startsWith(prefix = devicePrefix)) {
Logger.d(TAG, "Match found $name")
mutableList.plus(name)
}
}
}
return mutableList
}
private fun scanForDevices(): Observable<ScanResult>
= rxBleClient.scanBleDevices(
ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES)
.build(),
ScanFilter.Builder()
.build())
}
OK, here it is boiled down for the next person who wants to do this kind of thing. In Rx, they have Singles which are Observables that just emit one value. In my case I needed a list of String values, so just need to use a Single of type List of type String. That gets just one element emitted that happens to be a list of Strings. The code looks like this...
fun returnAllDevicesStartingWith(devicePrefix: String): Single<List<String>> {
return scanForDevices()
.take(3, TimeUnit.SECONDS, timeoutScheduler)
.map { it.bleDevice.name }
.filter { it.startsWith(devicePrefix) }
.toList()
}
The function that calls it (written in Java instead of Kotlin) looks like this:
List<String> devices = bleUtility.returnAllDevicesStartingWith(prefix).blockingGet();
I tested it using a mocked function like this:
//Begin test code
var emittedList: List<String> = listOf("dev1-1", "dev1-2", "dev2-1", "dev2-2", "dev3-1", "dev3-2")
private fun scanForRoomDevices(): Observable<FoundDevice> = Observable
.intervalRange(0, emittedList.size.toLong(), 0, 1, TimeUnit.SECONDS, timeoutScheduler)
.map { index -> FoundDevice(emittedList[index.toInt()], BleDevice(emittedList[index.toInt()])) }
data class FoundDevice(val controllerId: String, val bleDevice: BleDevice)
data class BleDevice(val name: String)
Hope this helps others.

Swift L-System Algae

So this is just something I'm doing for fun in the IBM Swift sandbox. I did a L-system algae in Scala recently and though it would be cool to do it in Swift to see how the language compares.
https://github.com/I2obiN/L-System-Algorithms-and-Fractals/blob/master/algae.scala
That's the Scala one to show you what I'm aiming for, and this is what I have in Swift;
/**
* Created by t.hood on 26/01/16
* L-System Algae
*
*/
import Foundation
// Mapping function for string
func stringmap(x: String) -> String {
var str = x;
// Replace characters in string
str = x.stringByReplacingOccurrencesOfString("A", withString: "AB") +
str.stringByReplacingOccurrencesOfString("B", withString: "A");
// Return mapped string;
return str;
}
func lsys() {
// Declarations
var iteration : Int = 2;
var x = 0;
var lsystem: String = "A";
let charA: Character = "A";
let charB: Character = "B";
while(x != iteration) {
print(lsystem)
// Iterate through characters in string
for chars in lsystem.characters {
lsystem = stringmap(lsystem);
}
// Inc count ..
x+=1
}
}
// Run ..
lsys();
The problem I'm having is in my mapping function. I need it to map x, print the result, then do the next map on str. The problem is I have the operator + between both maps and I can't get swift to print str after the first operation.
Anyone any ideas on how to get around this? If I could print str after the first replacements I think it would work.
I created a project in Xcode so I could use the debugger to see what's going on. Here's what I found.
I moved your print statement into the for loop so it would show the value of lsystem every time the loop executed.
The while loop executes twice, when x == 0 and 1
The first time through the while loop lsystem == 'A', so the for loop executes once. In stringmap(), the 'A' becomes 'ABA'.
'ABA' is printed.
The second time through the while loop, the for loop gets executed three times.
For loop 1: 'ABA' is sent to stringmap() and 'ABBABAAA' is returned.
For loop 2: 'ABBABAAA' is sent to stringmap() and 'ABBBABBABABABAAAAAAAA' is returned.
For loop 3: 'ABBBABBABABABAAAAAAAA' is sent to stringmap() and 'ABBBBABBBABBABBABBABABABABABABABABAAAAAAAAAAAAAAAAAAAAA' is returned.
I modified your stringmap() function to iterate through the string character by character and apply the grammar to each character, appending each change to a new string that was then returned to lsys().
Seven iterations of the while loop returned this, which agrees with what I see in the Wikipedia article about L-system.
Iteration 0: A
Iteration 1: AB
Iteration 2: ABA
Iteration 3: ABAAB
Iteration 4: ABAABABA
Iteration 5: ABAABABAABAAB
Iteration 6: ABAABABAABAABABAABABA
Iteration 7: ABAABABAABAABABAABABAABAABABAABAAB
Here's the code. This worked in a playground on my iMac, I'd expect it to work in the IBM Swift sandbox too.
func stringmap(x: String) -> String
{
var returnString = ""
for char in x.characters
{
switch (char)
{
case "A" :
returnString += "AB"
break
case "B":
returnString += "A"
break
default:
break
}
}
return returnString
}
func lsys()
{
// Declarations
let iteration : Int = 7;
var x = 0;
var lsystem: String = "A";
print("Iteration \(x): \(lsystem)")
while(x != iteration)
{
lsystem = stringmap(lsystem)
x++
print("Iteration \(x): \(lsystem)")
}
}
lsys()