Scala: sort comparing with adjacent elements [closed] - scala

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 5 years ago.
Improve this question
Assuming I have the following Scala classes:
Human(id: String, task: Task)
Task(id: String, time: Duration)
And having a List[(Human, Task)] with the following elements:
("H2", Task("T3", 5 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
Now I want to functionally check if close elements have the same duration, and if so, order them by the human id.
In this case, the final list would have the elements sorted like so:
("H2", Task("T3", 5 minute))
("H1", Task("T1", 10 minute))
("H3", Task("T1", 10 minute))
("H1", Task("T2", 5 minute))
I tried to use sortBy to do so, but the way I'm doing, the final list will be fully ordered by the Human ID, not comparing the times.
Does anyone have any idea how can I do this?

Your question is a bit confused. You say you have a List of (Human,Task) tuples, but then you describe a collection of (String,Task) tuples.
Here's a way to sort a List[Human] according to the rules you've described.
def sortHumans(hs: List[Human]): List[Human] =
if (hs.isEmpty) Nil
else {
val target = hs.head.task.time
hs.takeWhile(_.task.time == target).sortBy(_.id) ++
sortHumans(hs.dropWhile(_.task.time == target))
}

Related

How to Check if Variable exists in Scala [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 8 months ago.
Improve this question
I want to check if a variable is already defined/exists in scala or not. Lets say a function called checkVar do this operation:
var x = 10
checkVar(x) -> returns boolean True
checkVar(y) -> returns boolean False
I am asking this question because I want to create a mechanism to define a variable if it doesn't exist.
Variables only exist at compile time so you can't dynamically create or delete variables at runtime. So both x and y must be defined at compile time or else the compiler will reject the code.
What you can do is use Option to indicate whether a variable has a value or not:
def checkVar(v: Option[Int]) = v.nonEmpty
var x = Some(10)
checkVar(x) // True
val y = None
checkVar(y) // False
x = None
checkVar(x) // False

how to convert Flutter List<List> to List [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 9 months ago.
The community is reviewing whether to reopen this question as of 8 months ago.
Improve this question
// Here Is my Code
List data = [[0,1],[0,1],[0,1]];
List requiredList = [1,1,1];
To get the second value of the elements in the first list as second list you can do this:
List requiredList = data.expand((e)=>[e[1]]).toList();
Simply loop through the data and add the item in index 1 into output.
void main() async {
List data = [[0,1],[0,1],[0,1]];
List output = [];
for(final d in data){
output.add(d[1]);
}
print(output);
}
Output:
[1, 1, 1]
Your data contains lists, i.e to access data of list you have to pass index of list, i.e 0 1 2 etc.
List data = [[0,1],[0,1],[0,1]];
^ ^ ^
| | |
0 1 2
Above mentioned are index of each sub-list
So if you want to access data from sub-list, call the main list followed by index of sub-list and index number of elements of sub-list.
data[0][1] //this will give an output of 1
^ ^ ^
|. |. |
*. **. ***
*. => list containing lists here
** => Index of sub-list
*** => index of element in list
Check this video here to get more clarity of concept. I've attached a video related to python but same things apply in dart/flutter as well.

Print repetition in Swift while statement [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
In my while statement, I cannot understand why my output is printed twice ?
I would like to print i only one time, where is my error ?
func fetch2(){
var i: Int = 0
while i <= (self.returned-1) {
let itemLookUp = "https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?appid=\(self.appId)&itemcode=\(self.arrayCodeProduct[i])&responsegroup=large"
print(i)
i = i+1
}
}
Here is the output that I obtain :
0
1
2
3
0
1
2
3
Thank you in advance.
It looks like fetch2() is called twice.
Add a print(#function) before you var i and check that fetch2() is not called several times.

Display only some digits of an Int [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 4 years ago.
Improve this question
Let's say you have an Int.
let int = 12345
I want it to only display some of the digits.
For example: print(firstTwoDigits) --> 12
How do I do this and thank you in advance.
It depends on your specific requirements.
This prints the first two digits of an integer number
let intVal = 12345
print(String(intVal).prefix(2))
Output: 12
Another way which only prints certain ones in the number:
let intVal = 12345
let acceptableValues = ["1", "2"]
let result = String(intVal).filter {
acceptableValues.contains(String($0))
}
print(result)
Output: 12

Create key/value-­‐array pairs Scala/Spark [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 5 years ago.
Improve this question
How can I create key/value-array pair in Scala. By this I mean in place of value I need an array.
val newRdd1 = rdd1.flatMap(x=>x.split(" "))
.map({case (key, Array(String)) => Array(String) })
You can achieve it using map(), it is similar in either plain scala program or Scala-in-SparkContext.
Example, you have a list of strings:
var sRec = List("key1,a1,a2,a3", "key2,b1,b2,b3", "key3,c1,c2,c3")
You can split it & convert to key/value(array of strings) assuming key is in 0th position, using:
sRec.map(x => (x.split(",")(0), Array(x.split(",")(1), x.split(",")(2), x.split(",")(3)))).
foreach(println)
(key1,[Ljava.lang.String;#7a81197d)
(key2,[Ljava.lang.String;#5ca881b5)
(key3,[Ljava.lang.String;#24d46ca6)
If you want to read a particular array element by key:
sRec.map(x => (x.split(",")(0),Array(x.split(",")(1), x.split(",")(2), x.split(",")(3)))).
map(x => (x._1, x._2(0))).foreach(println)
Output:
(key1,a1)
(key2,b1)
(key3,c1)