Skip to content

Conversation

@hmc-cs-dzhang
Copy link

No description provided.

Racket as well. Scala also does support pattern matching like Haskell, but it
seemed slightly more cumbersome. This made it very easy to, for example, return
different results based on different inputs, as we did in the `matchOnInputType`
function. It was also very easy to declare variables and use type inference, at
Copy link

Choose a reason for hiding this comment

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

I think it's interesting that you mentioned it's both easy to do functional programming and declare variables/type inference. I agree with the sentiment. However, most functional languages make it more cumbersome to declare variables through let statements. It's interesting to try to classify Scala in terms of imperative/functional because it can do both rather well.

@@ -1,0 +1,51 @@
#Scala Thoughts#

###What's easy to do in Scala? What's not?###
Copy link

Choose a reason for hiding this comment

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

What's not as easy?


There were a few design choices that I very much appreciated. I appreciated
the different ways you could use pattern matching. In match/case statements,
it was possible to include a line like `case x : Int if x > 0`, so that we
Copy link

Choose a reason for hiding this comment

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

That's a great point! For me, it was weird at first because pattern matching with a case statement felt foreign. You're right though that it's a very concise way to express the thought and would be harder or at least less concise in most other languages.

*/
def roomState(rooms: Map[Int, Option[String]], room: Int): String = {
error("Fix me")
if (!rooms.contains(room)) {
Copy link

Choose a reason for hiding this comment

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

I did it differently, using .get and pattern matching all the way through. I tried to look online for which was preferred because I had to use case Some(Some("locked")) which seems less elegant than case Some("locked") but did not use an if statement, but I could not find anything. It would be interesting to look more into that.

*/
def totalPeopleInRooms(rooms: Map[Int, Option[String]]): Int = {
error("Fix me")
rooms.values.foldRight(0)((b,a) => peopleInRoom(b) + a)
Copy link

Choose a reason for hiding this comment

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

Similarly, I'm not sure if foldLeft or foldRight is better. When I looked it up, I read (http://stackoverflow.com/questions/24370549/foldleft-v-foldright-does-it-matter) that the performance of the two is comparable because of how Scala implements their foldLeft and foldRight functions. I haven't seen a language like that before

case i : Int if i > 0 => "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"
case v1::v2::tail => s"first: ${v1}, second: ${v2}, rest: ${tail}"
Copy link

Choose a reason for hiding this comment

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

I used another if statement here, but this is way more idiomatic. I wasn't able to find this Scala idiom. Good job!

error("fix me")
iList match {
case e1::e2::tail => e1 :: oddElements(tail)
case e1::Nil => List(e1)
Copy link

Choose a reason for hiding this comment

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

This again looks a lot cleaner with the :: idiom. I also didn't realize that there was Nil. This definitely looks way better.


List(validYoungNames.toList, validOldNames.toList)
// return the list of just their names
List(sortedYoung.map(p => p.firstName), sortedOld.map(p => p.firstName))
Copy link

Choose a reason for hiding this comment

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

This looks nice. I also like that Scala can do method chaining, so this can be done in fewer lines as well. It really allows for the programmer to make choices based on their personal style.

/**
* Given a string of lowercase letters, ensures that it is a palindrome
*/
def isPalindromeHelper(s: String): Boolean = {
Copy link

Choose a reason for hiding this comment

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

This is a really interesting way of doing it. I just reversed the strings and output whether or not they were equal, but this definitely works as well.

familiar with using `myList[0]` instead of `myList(0)` to access the first
element. I am unsure why the language designers chose this, perhaps because the
bracket operator has a different use. I also thought that the syntax for naming
a variable was unusual. I don't understand why `val x: Int = 5` is better than,
Copy link

Choose a reason for hiding this comment

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

I definitely agree with this. Personally, it's also very confusing that val and var are one letter apart, so I could definitely see typos causing bugs in the code. I agree that const seems better and would be curious to hear the designers' reasoning.

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.

3 participants