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 9 years ago.
Improve this question
I am new to scala language, i am using anorm to insert data into DB, please give an example to insert data into my table userTable
Table :
userId,
userName
Thanks
Try this code.. accept answer if it solves your problem.
DB.withConnection { implicit c =>
SQL("INSERT INTO userTable values({userId},{userName})").on("userId" -> "User's id", "userName" -> "your_name").executeInsert();
}
DB.withConnection { implicit c =>
SQL("insert into userTable values({userId},{userName})").on("userId" -> value1, "userName" -> value2).executeInsert();
}
Related
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 11 months ago.
Improve this question
I have a piece of code implemented in 2 ways:
val c = if(x.y.isDefined){
methodA(....)
} else {
method(....)
}
c
And this:
val c = x.y.
map(methodA(....))
.orElse(Some(mehodB(....))
.getOrElse("")
c
x.y is an Option and methodA, methodB return String values.
Which of these approaches is more preferred in Scala? Personally I find first one to be more easy to understand, but my more scala proficient colleague prefers second one.
use the if or something based on pattern match (by the way, no need for assigning to c if you return the expression)
x.y match {
case Some(_) => methodA()
case None => methodB()
}
x.fold(methodB) { _ => methodA }
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 1 year ago.
Improve this question
Found this stepik task and have no idea what to do:
Write a function which gets a Map[Char, Int] as an input and makes a Set[LetterTree] (LetterTree is the case class below), in which from each (c,w) map entry, we make a LetterLevel(c,w), then we then put it in the said Set[LetterTree].
trait LetterTree {
def weight: Int
}
case class LetterLevel(ch: Char, weight: Int) extends LetterTree
object TreeBuild extends App {
def TreeSetBuild(freqs: Map[Char,Int]): Set[LetterTree] = ???```
}
You could use tupled to construct LetterLevel instances from the tuple elements of the Map:
object TreeBuild extends App {
def treeSetBuild(freqs: Map[Char,Int]): Set[LetterTree] =
freqs.map(LetterLevel.tupled).toSet
}
TreeBuild.treeSetBuild(Map('a'->1, 'b'->2, 'c'->3))
// res1: Set[LetterTree] = Set(LetterLevel(a,1), LetterLevel(b,2), LetterLevel(c,3))
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 3 years ago.
Improve this question
what is this grammar? What function was called?
Does this grammar have a name?
val indataRDD = sc.makeRDD(Array("1,jack,15","2,Lily,16","3,mike,16"))
val rdd = indataRDD.map( _.split(',') ).map{
arr => {
val put = new Put(Bytes.toBytes(arr(0)))
put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"),Bytes.toBytes(arr(1)))
put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("age"),Bytes.toBytes(arr(2).toInt))
***(new ImmutableBytesWritable, put)***
}
}
This is syntax sugar to create a tuple of two elements.
The line (new ImmutableBytesWritable, put) is therefore equivalent to new Tuple2(new ImmutableBytesWritable, put)
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
I want to put the print behind
like
"foo" match {
case str => println(str)
}
or
List("foo") foreach print
Are there other best one?
like in Haskell
return "foo" >>= print
so if you just want to have somewhat haskell like semantics you can try the cats or the scalaz libraries. both have IO monads (cats in cats-effects) that would allow you to use the print in a principled manner as in your last example. If you don't care about the purity aspect you could also achieve this with the Id Monad which also both libraries offer.
You can do this:
case class Print(v: String) {
def print = Predef.print(v)
}
object Print {
implicit def stringToString(s: String) = new Print(s)
}
import Print._
object App extends App {
"foo".print
"bar" print
}
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 6 years ago.
Improve this question
what is the type of this object?
class Zad1[A,B](val fst:A, val snd:B) {
override def toString: String = "(" + fst +","+snd+")"
}
object Zad1 {
def main(args: Array[String]): Unit = {
val v = new Zad1[Int, String](1, "2")
println(v)
}
}
I tried to print the class name with :
println(v.getClass) // would print: class $line8.$read$$iw$$iw$Zad1
The type of a singleton object is its singleton type, ergo, the type of Zad1 is Zad1.type.
This is related to how Scala REPL works.
Though you type just:
scala> class Zad1[A,B](val fst:A, val snd:B) {...}
REPL wraps it into a series of other objects($line8.$read.$iw.$iw), so getClass returns class $line8.$read$$iw$$iw$Zad1.
Read about it here:
Trying to understand how classes declared on the REPL are treated internally
If you run the same as a Scala program(not from REPL), getClass will return something much readable, e.g. class com.example.Zad1