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 4 years ago.
Improve this question
so i have a task to make a deck of cards using scala. Im good at object oriented programming and so i made this in a few minutes. Now its time for me to learn functional programming. Oh boy.. Where do i begin with this? How do i even construct these cards? I was thinking maybe i shouls have 3 parallel arrays of information? For specific card id, face and suit? I can use enumerators for values but how do i actually initialize these arrays? Currently im stuck at using arrays. Maybe i should make a list? If so, how would i initialize them as well?
-Thank you!!
A possible implementation in scala:
// algebraic data types are usually used in scala for enum-like concepts
sealed trait Suit
object Suit {
case object Diamond extends Suit
case object Spade extends Suit
case object Club extends Suit
case object Heart extends Suit
val all = List(Diamond, Spade, Club, Heart)
}
sealed trait Card
case class NumberCard (number: Int, suit: Suit) extends Card {
require(number >= 1 && number <= 13, s"Invalid card number: $number")
}
case object Joker extends Card
val deck: List[Card] = Joker :: Joker :: (for {
suit <- Suit.all
number <- 1 to 13
} yield NumberCard(number, suit))
How you store these in collections depends on the operations that you need to perform.
Why not simply have an ID ( 0 to 51 for 52 cards ) and use this :
((ID % 13) + 1) = card value ( 1, 2, 3, ... 13 )
( ID / 13 ) = Suit ( 0,1,2,3 = Hearts, .... )
If you use Jokers, then (ID/13) would equal 4 for the 2 Jokers.
So you keep an array of 52/54 numbers which would be your deck. You keep all these Card ID in this array in any order you want.
Related
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 is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am trying to print some attributes of employee along with method. but it is throwing and error.
But I am not able to understand this issue. can someone help to resolve the issue
Error
Error:(12, 43) not found: value grossSalary
println("Employee gross salary is : "+grossSalary)
Code
case class EmployeeGross (empId: Int,empName: String, deptId: Int, var basicSalary: Double) {
var Hra: Double= basicSalary*(30/100)
var Da: Double = basicSalary*(10/100)
var grossSalary: Double = basicSalary + Hra + Da
def grossSalary(basicSalary: Double) = basicSalary + Hra + Da
println("Employee salary information is :"+ empId,empName,deptId,basicSalary)
}
object EmployeeGross {
def main(args: Array[String]): Unit = {
val Eg = new EmployeeGross(1,"test",10,1200.2)
println("Employee gross salary is : "+ grossSalary)
}
}
You are trying to access grossSalary from the EmployeeGross object, probably thinking that this object knows which employee you have in mind. This is not possible to know, you have to use Eg.grossSalary.
Furthermore, you need to multiply by 0.3 and 0.1 rather than 10/100 and 30/100. This is because 10/100 performs integer division, which will yield 0. You could also fix this by writing 10.0/100 which will do floating point division.
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
}
This question already has an answer here:
Typeclasses and inheritance in scalaz
(1 answer)
Closed 7 years ago.
I was playing with scalaz and thought I could extend Enum type class for something to make myself understand scalaz better. So I wrote this:
sealed abstract trait Counter
case object First extends Counter
case object Second extends Counter
case object Third extends Counter
implicit val enumCounter: Enum[Counter] = new Enum[Counter] {
override def succ(a: Counter): Counter = a match {
case First => Second
case Second => Third
case Third => First
}
override def pred(a: Counter): Counter = a match {
case First => Third
case Second => First
case Third => Second
}
override def order(x: Counter, y: Counter): Ordering = {
val map = Map[Counter, Int](First -> 0, Second -> 1, Third -> 2)
implicitly[Order[Int]].order(map(x), map(y))
}
}
println(First |=> Third)
println(First.succ)
But it turns out that it doesn't work as I hoped it will. First does not have succ nor |=>, because I've created Enum[Counter] but not Enum[First]. If I write First.asInstanceOf[Counter].succ it starts to resolve. Which is now obvious to me. But how can I implement Enum typeclass then in a simple way? I do not want to declare separate implicit values for each of Enum[First], Enum[Second]....
There are two possible solutions I am thinking of:
1) Make scala resolve Enum[First] as Enum[Counter]. But I cannot seem to understand how this can be possible as Enum can be only nonvariant
2) Maybe there is a solution in scalaz?
Otherwise Enum typeclass starts to be quite limited, as it does not supports Enum which sounds very weird.
I am actually not sure how much this question belongs to scalaz, it probably depends on whether the solution is (1) or (2). If the solution is (1) - this question is pure scala.
I rethought the question and rephrased it a lot - and received an answer, please see this: Typeclasses and inheritance in scalaz
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
According to this, Scala case classes automatically generates an equality method. However:
class SomeCaseClass(val string:String) {}
val a = "123"
assertTrue( a.equals( a ) ) // Passes
assertTrue( new SomeCaseClass(a).equals( new SomeCaseClass(a) ) ) // Fails, Scala 2.10
So, what does this automatically generated equals method do?
It's not a case class. That's the case class:
scala> case class SomeCaseClass(string: String)
defined class SomeCaseClass
scala> val a = "123"
a: String = 123
scala> SomeCaseClass(a) == SomeCaseClass(a)
res1: Boolean = true
== is just a syntax sugar for equals
In your example, you've just called equals (you'll get same result with ===) on regular object (this equals is not automatically generated), so it checked just referential equality.
P.S. You may notice that case class doesn't require new as it has automatically generated companion object. It doesn't require val in constructor as it assumes it by default. {} isn't mandatory for both regular and case classess.