How to split one liner object keys to multiple lines - so each key will has its own line? - visual-studio-code

I have this:
method({ a: 1, b: 2, c: 3 });
I want to find the magic key in VSCode that turns it to this:
method({
a: 1,
b: 2,
c: 3
});
And vice versa

Related

Mongo db find multiple conditions on the same key

I encountered an unexpected behavior in find() logic on mongodb community 5.0.6.
My collection is:
{ _id: 1, a: 1, b: 1 }
{ _id: 2, a: 2, b: 2 }
{ _id: 3, a: 3, b: 3 }
when I execute
db.selection.find({a:1,b:2})
I obtain an empty set as resultset, as expected for condition a=1 AND b=2 (AND logic in selection)
But when I execute
db.selection.find({a:1,a:2})
Which should represent the logic condition a=1 AND a=2
I obtain this result
{ _id: 2, a: 2, b: 2 }
Honestly I was expecting an empty set again.
Someone can explain it?

How to find duplicates in a list in Scala?

I have a list of unsorted integers and I want to find the elements which are duplicated.
val dup = List(1|1|1|2|3|4|5|5|6|100|101|101|102)
I have to find the list of unique elements and also how many times each element is repeated.
I know I can find it with below code :
val ans2 = dup.groupBy(identity).map(t => (t._1, t._2.size))
But I am not able to split the above list on "|" . I tried converting to a String then using split but I got the result below:
L
i
s
t
(
1
0
3
)
I am not sure why I am getting this result.
Reference: How to find duplicates in a list?
The symbol | is a function in scala. You can check the API here
|(x: Int): Int
Returns the bitwise OR of this value and x.
So you don't have a List, you have a single Integer (103) which is the result of operating | with all the integers in your pretended List.
Your code is fine, if you want to make a proper List you should separate its elements by commas
val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102)
If you want to convert your given String before having it on a List you can do:
"1|1|1|2|3|4|5|5|6|100|101|101|102".split("\\|").toList
Even easier, convert the list of duplicates into a set - a set is a data structure that by default does not have any duplicates.
scala> val dup = List(1,1,1,2,3,4,5,5,6,100,101,101,102)
dup: List[Int] = List(1, 1, 1, 2, 3, 4, 5, 5, 6, 100, 101, 101, 102)
scala> val noDup = dup.toSet
res0: scala.collection.immutable.Set[Int] = Set(101, 5, 1, 6, 102, 2, 3, 4, 100)
To count the elements, just call the method sizeon the resulting set:
scala> noDup.size
res3: Int = 9
Another way to solve the problem
"1|1|1|2|3|4|5|5|6|100|101|101|102".split("\|").groupBy(x => x).mapValues(_.size)
res0: scala.collection.immutable.Map[String,Int] = Map(100 -> 1, 4 -> 1, 5 -> 2, 6 -> 1, 1 -> 3, 102 -> 1, 2 -> 1, 101 -> 2, 3 -> 1)

How to remove duplicates from particular column in Scala by reading textfile

I am new to scala, I am reading textfile from local, and I want to find duplicate columns in example.
Input File:
1,2,3
2,3,4
1,3,4
2,4,5
3,4,5
I need output like this:
Select first column
1->2
2->3
3->1
program is:
val file=scala.io.Source.fromFile("D:/Files/test.txt").getLines().mkString("\n")
val d=file.groupBy(identity).mapValues(_.size)
println(d)
But I am getting output Like this
Map(-> 5, 4 -> 1, 9 -> 1, 5 -> 3, , -> 12, 1 -> 3, 0 -> 1, 2 -> 5, 3 -> 4)
Its counting all the data but I want to count duplicates in particualr column only
The issue here is because once the call mkString is made, the multiple lines on the file is 'lost'. Another approach could be to use the toArray call instead.
val file = scala.io.Source.fromFile("D:/Files/test.txt")
val lines = file.getLines().toArray
On the above example, lines would be a array of strings:
Array(1,2,3, 2,3,4, 1,3,4, 2,4,5, 3,4,5)
then to extract the first column before grouping you could use something like the slice method on each string
lines.map(_.slice(0,1)).groupBy(identity).mapValues(_.size)
Also, remember to close the file :)
Full example:
val file = scala.io.Source.fromFile("D:/Files/test.txt")
val lines = file.getLines().toArray
val grouping = lines.map(_.slice(0,1)).groupBy(identity).mapValues(_.size)
file.close
If I understand your question correctly, shouldn't the duplicate counts of the 1st column be (1->2, 2->2, 3->1)?
Here's one approach to get the counts:
// Create a list of split-column arrays
val list = scala.io.Source.
fromFile("/Users/leo/projects/scala/files/testfile.txt").
getLines.
map(_.split(",")).
toList
list: List[Array[String]] = List(Array(1, 2, 3), Array(2, 3, 4), Array(1, 3, 4), Array(2, 4, 5), Array(3, 4, 5))
// Count duplicates of the 1st split-column
val d = list.
groupBy(_(0)).
mapValues(_.size)
d: scala.collection.immutable.Map[String,Int] = Map(2 -> 2, 1 -> 2, 3 -> 1)

How can I add a blank line before some data using Ruamel.yaml

I can't seem to figure out how I can add a blank line between data using Ruamel.yaml.
Suppose I have data:
---
a: 1
b: 2
I need to add to this so that I will have:
---
a: 1
b: 2
c: 3
I understand that the blank line is implemented as a CommentToken:
Comment(comment=None,
items={'data': [None, None, CommentToken(value=u'\n\n'), None], 'b': [None, None, CommentToken(value=u'\n\n'), None]})
What I don't know is how to manipulate that structure.
That Comment object is not from the input that you give, as data is not a key in your mapping, that should be a:
import ruamel.yaml
yaml_strs = [
"""\
---
a: 1
b: 2
""",
"""\
---
a: 1
b: 2
c: 3
"""]
for yaml_str in yaml_strs:
data = ruamel.yaml.round_trip_load(yaml_str)
print(data.ca)
gives:
Comment(comment=None,
items={'a': [None, None, CommentToken(), None]})
Comment(comment=None,
items={'a': [None, None, CommentToken(), None], 'b': [None, None, CommentToken(), None]})
comparing the above comments should give you an idea of what to try:
import sys
import ruamel.yaml
yaml_str = """\
---
a: 1
b: 2
"""
data = ruamel.yaml.round_trip_load(yaml_str)
data['c'] = 3
ct = data.ca.items['a'][2]
data.ca.items['b'] = [None, None, ct, None]
ruamel.yaml.round_trip_dump(data, sys.stdout)
which gives:
a: 1
b: 2
c: 3
The CommentToken ct can also be constructed from scratch:
ct = ruamel.yaml.tokens.CommentToken('\n\n', ruamel.yaml.error.CommentMark(0), None)
as is, e.g. done in ruamel.yaml.comments.CommentedBase.yaml_set_start_comment().
The 0 parameter to CommentMark() is how far the comment is indented, which is not important in case of empty lines, but still needs to be provided.

Order of parameters to foldright and foldleft in scala

Why does the foldLeft take
f: (B, A) => B
and foldRight take
f: (A, B) => B
foldLeft could have been written to take f: (A, B) => B.
I am trying to understand the reasoning for the difference in the order of parameters.
It's supposed to show you the direction of the aggregation. FoldLeft aggregates from left to right, so you can imagine the accumulator B as bunching up stuff on the left side as it approaches each A:
If you have something like:
Vector(1,2,3,4,5).foldLeft(0)((b,a) => b + a)
Then you get this behavior
B ...As...
---------------
(0), 1, 2, 3, 4, 5
(0+1), 2, 3, 4, 5
(0+1+2), 3, 4, 5
(0+1+2+3), 4, 5
(0+1+2+3+4), 5
(0+1+2+3+4+5)
FoldRight, on the other hand, aggregates things from the right side. So if you have something like:
Vector(1,2,3,4,5).foldRight(0)((a,b) => a + b)
Then you get this behavior
...As... B
-----------------
1, 2, 3, 4, 5 (0)
1, 2, 3, 4, (5+0)
1, 2, 3, (4+5+0)
1, 2, (3+4+5+0)
1, (2+3+4+5+0)
(1+2+3+4+5+0)
#dhg already provided a great answer. My example illustrates an interesting subtlety: namely, that sometimes the the order in which the initial
value is passed to the given function matters. So I figured I'd post this on the off chance someone
is interested in cases where foldRight can behave differently than foldLeft
with the same initial value, same function, and same input list.
Consider the exponentiation below:
def verbosePower(base:Double, exp:Double) = {
println(s"base=$base / exp=$exp") ;
math.pow(base, exp)
}
var X = List(2.0,3).foldLeft(1.0) (verbosePower)
System.out.println("x:" + X);
X = List(2.0,3).foldRight(1.0) (verbosePower)
System.out.println("x:" + X);
the output and result from foldLeft is:
base=1.0 / exp=2.0
base=1.0 / exp=3.0
X: Double = 1.0
the output and result from foldRight is:
base=3.0 / exp=1.0
base=2.0 / exp=3.0
X: Double = 8.0