Skip to content

Conversation

@MichaelSheely
Copy link

Yay

*/
def totalPeopleInRooms(rooms: Map[Int, Option[String]]): Int = {
error("Fix me")
//rooms.keys.map(roomNumToOccupancy(rooms, _: Int)).foldLeft(0)(_ + _)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry I should have deleted this line before I submitted!

* - the value can be:
* -- the amount of people in the room (filled: Some("10"), empty: None)
* -- the room is not available (Some("locked"))
*/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed some >80 char lines. (gotta pass linecheck)

def older(p: Person): Option[String] = {
error("fix me")
p match {
case Person(name, age) if age > 30 => Some(name)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have just used an if statement here, but I had just gotten addicted to case statements from the previous problem.


From reading the scala documentation intro, it became clear that support for
DSLs is a key feature, under the extensibility section. This can be seein in
the ability to customize the syntax for specific domains, the freedom of

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree! This does facilitate the creation of DSLs :)

preferred paradigm upon users. For example, the use of a functional style,
while well supported, is not the only way to write ones code.

This was encouraging, especially comparing it to Haskell, which is very

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never programmed in Haskell, but I have programmed in SML (which I hear is similar) and this is definitely the case. It made SML quite hard to learn at first.

I think this was part of the testing suite rather than a Scala design
decision, but I really liked the way the testing suite reported how the
code result differed from an expected value. For example, we have
`"A person named[:] Jack" vs "A person named[] Jack"`, allowing us

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes -- a good testing suite was so helpful!

def expt(n: Int, e: Int): Int = -1

}
def expt(n: Int, e: Int): Int = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another way to do this, when the exponent is an even #, is to do

if (e == 0) 1
else if (e%2) ((expt(n, e/2))* (expt(n, e/2)))
else n * expt(n, e - 1)

} else if (room_state.get.get == "locked") {
"not available"
} else {
room_state.get.get

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, I didn't think to use room_state.get.get

case str : String => s"A string with length ${str.length()}"
case n : Int => "A positive integer"
case p : Person => s"A person with name: ${p.name}"
case seq @ Seq(_ *) if seq.length > 10 => "Seq with more than 10 elements"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think to use the @ symbol!

*/
def sumOfList(l: List[Int]): Int = {
error("fix me")
l.foldLeft(0)(_ + _)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foldLeft was something I didn't figure out for a while, so good for you for figuring out how to use it :)

* - ... etc
*/
def nthElementInList[T](n: Int, l: List[T]): T = {
error("fix me")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do something similar, but couldn't, so thank you for figuring it out! What I tried to do was to remove all the elements in indices 0 through n-1, and then returning the head of the remaining list.

def elementExists[T](l: List[T], e: T): Boolean = {
error("fix me")
l match {
case List() => false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like how you did this. This is similar to what I tried, based on my limited knowledge of SML.

error("fix me")
l match {
case List() => false
case x :: xs => (x == e) || elementExists(xs, e)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So once it finds the element (and returns true), it stops, correct? Otherwise it keeps looking for the element in the first index of the remainder of the list?

def tails[T](l: List[T]): List[List[T]] = {
error("fix me")
l match {
case List() => List(l)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between saying what you said in the first case and saying the following:

case List() => l

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants