How to remove all values in a list in redis? - nosql

I have a list lst which has values say
1.hi
2.hi
3.hello
4.hello
5.sfk
I need to remove all these values from the list.What is the easiest way to achieve this ?
LREM mylist 0 "hi"
I tried the above , but it deletes only hi but i need to delete all values in one shot.
How to achieve this ?
I am new to redis.

DEL is probably what you're looking for:
redis> RPUSH mylist "hi" "hi" "hello" "hello" "sfk"
(integer) 5
redis> LRANGE mylist 0 -1
1) "hi"
2) "hi"
3) "hello"
4) "hello"
5) "sfk"
redis> DEL mylist
(integer) 1
redis> LRANGE mylist 0 -1
(empty list or set)
It returns the number of keys removed.
It takes O(N) time where N is the number of elements in the list.

Related

Getting a sublist using regex

I have a list as such:
myList: ("ab";"bc";"cd","de");
I would like to get a sublist like that contains "b"
I know i can do this:
myList like "*b*"
However this returns a binary list. 1100b;
How can I return a list of ("ab";"bc") instead?
Following code returns needed sublist
myList: ("ab";"bc";"cd","de");
myList where myList like "*b*"
As you have mentioned
myList like "*b*" returns boolean list 1100
where 1100 returns list of indices with true value: 0, 1
myList 0 1 returns first two elements of myList

Scala comprehension from input

I am new to Scala and I am having troubles constructing a Map from inputs.
Here is my problem :
I am getting an input for elevators information. It consists of n lines, each one has the elevatorFloor number and the elevatorPosition on the floor.
Example:
0 5
1 3
4 5
So here I have 3 elevators, first one is on floor 0 at position 5, second one at floor 1 position 3 etc..
Is there a way in Scala to put it in a Map without using var ?
What I get so far is a Vector of all the elevators' information :
val elevators = {
for{i <- 0 until n
j <- readLine split " "
} yield j.toInt
}
I would like to be able split the lines in two variables "elevatorFloor" and "elevatorPos" and group them in a data structure (my guess is Map would be the appropriate choice) I would like to get something looking like:
elevators: SomeDataStructure[Int,Int] = ( 0->5, 1 -> 3, 4 -> 5)
I would like to clarify that I know I could write Javaish code, initialise a Map and then add the values to it, but I am trying to keep as close to functionnal programming as possible.
Thanks for the help or comments
You can do:
val res: Map[Int, Int] =
Source.fromFile("myfile.txt")
.getLines
.map { line =>
Array(floor, position) = line.split(' ')
(floor.toInt -> position.toInt)
}.toMap

Spark scala how to count values in rows

I am new to both Spark and Scala...and I have to read a data file and count the value that are contained in both columns and rows. The data set is structured like this:
0 0 2
0 2 2
0 2 0
2 0 0
0 0 0
0 1 0
In order to count the number of "2" in each column:
I imported the file:
val ip = sc.textFile("/home/../data-scala.txt")
I created an array to save my results
var ArrayCol = Array.ofDim[Long](3)
val cols = ip.map(line => line.split(" "))
for (i <- 0 to 2) {
ArrayCol(i) = cols.map(col => col(i)).filter(_.contains("2")).count()
}
and I counted the number of "2" contained in each column.
Now I would like to do the same for each row. Do you have any suggestion?
cols.map(r => r.count(_ == "2"))
Or shell example:
scala> val cols = sc.parallelize(List("0 1 2", "2 0 2")).map(_.split(" "))
scala> cols.map(_.count(_ == "2")).collect()
res1: Array[Int] = Array(1, 2)
Ok thank you
cols.map(r => r.count(_ == "2"))
works fine to count how many "2" there are in each row.
How would you do to count how many "2" there are in each column?
I think there is a more clear solution than mine.
Thanks.

scala deal with string and do accumulative

I have variable:
val list= rows.sortBy(- _._2).map{case (user , list) => list}.take(20).mkString("::")
the result println(list) should be:
60::58::51::48::47::47::45::45::43::43::42::42::42::41::41::41::40::40::40::39
And now I have to deal with these numbers (like histogram concept)
If I set the break is 10, it should divide the max number (60) by 10 and make 6 buckets:
the scope between 0~ 10(0<x<=10) have 0 numbers match
the scope between 10~ 20(10<x<=20) have 0 numbers match
the scope between 20~ 30(20<x<=30) have 0 numbers match
the scope between 30~ 40(30<x<=40) have 4 numbers match
the scope between 40~ 50(40<x<=50) have 13 numbers match
the scope between 50~ 60(50<x<=60) have 3 numbers match
And then I have to save with 2 variables x and y :
x: 0~10::10~20::20~30::30~40::40~50::50~60
y: 0::0::0::4::13::3
How can I do this?
val actualList = list.split("::").map(_.toInt).toList
val step = 10
val steps = step to actualList.max by step
//for each step, output a count of all items in the list that are
//between the current step and currentStep - stepVal
val counts = steps.map(x=>actualList.count(y=>y <= x && y > x - step))
val stepsAsString = steps.map(x=>s"${x-step}~$x")
And you can map them too:
steps.zip(counts).toMap
Note that this could be made more performant if the list were sorted first, but I wouldn't worry about tuning unless you need it

How does "for (i <- 1 to x; j <- 1 to y)" actually increment the variables in Scala?

How is below loop being incremented ?
for(i <- 1 to 3; j <- 1 to 3) print((10 * i + j) + " ")
Is there an implicit counter using 'to' ?
for is actually shorthand for applying a bunch of collections methods. In particular, if you are not using yield, each statement in the for selector is translated to foreach. So
for (i <- 1 to 3; j <- 1 to 4) f(i,j)
turns into
(1 to 3).foreach{ i => (1 to 4).foreach{ j => f(i,j) } }
foreach is a method on all collections--Range included, which is what 1 to 3 turns into--which loops through each item in the collection, calling a provided function each time. A Range's items are the numbers listed (endpoints included, in this case)--in fact, Range doesn't actually store the numbers in a separate list, so it's main purpose is precisely to hold ranges of numbers for exactly this sort of iteration.
In scala, the for construct is like the "foreach" construct in Java. The following sets i to be each successive item in the given Iterable.
scala> for(i <- Seq(1, 2, 3)) println(i)
1
2
3
The to operator, as in 1 to 3 constructs a Range from 1 to 3:
scala> 1 to 3
res3: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)
There is an implicit conversion from Int to RichInt.
RichInt defines the function to() which returns a Range.
Range is a collection, and has foreach() hence it can be used in a for comprehension (which is just syntactic sugar for foreach()).