Meeting 20 - Encapsulating Effects

Author

Bor-Yuh Evan Chang

Published

Thursday, October 31, 2024

\(\newcommand{\TirName}[1]{\text{#1}} \newcommand{\inferrule}[3][]{ \let\and\qquad \begin{array}{@{}l@{}} \TirName{#1} \\ \displaystyle \frac{#2}{#3} \end{array} } \newcommand{\infer}[3][]{\inferrule[#1]{#2}{#3}} \)

What questions does your neighbor have?

Announcements

  • Lab Leaderboards Beta: Nastiness and Robustness competitions for extra credit
  • Review 3-4 today and tomorrow
    • These are practice exams. We created lots of questions with explanations: ask about them in class, in office hours!
    • Sign up on Canvas – needed to get assignment points
    • Scored on the quality of your feedback for your partner, not how you were assessed by your partner
    • Submit on Gradescope as a group — to get each other’s feedback
    • 0 for no-shows.
    • Really sorry if your partner is a no-show in a self-guided session: write privately to CM Lawrence/Instructors on Piazza during your slot and don’t worry from a points standpoint.
  • Exam 3-4 next Tuesday 11/5
    • ?? minutes in class (watch Piazza)
    • accommodations (watch Piazza)

Announcements

  • Come see us to make a study plan
    • e.g., via the redo policy
    • grow before the final exam
  • Lab 1-3 Feedback Form
    • See CM Lawrence today.
  • Lab Tests
    • What an exercise should do is specified in the handout. Any given tests are some assistance.
    • Drop two lab assignments.
  • Lab Submissions
    • Must compile. Comment out temporary debugging things.
    • Remember to submit on Gradescope (to upload from GitHub).
  • I apologize taking away from class time on Tuesday versus having a 1-to-1 conversation.
  • Let me reiterate that the one thing I care about most is your growth as a computer scientist and a software engineer. I strive to meet you where you are —– regardless of whether you are approaching proficiency or exceeding —– and to help you grow from there.
  • Please come see us to help you create an individualized study plan (e.g., through redos or accelerated bonus projects).
  • We have a wealth of resources available to you, but I recognize that it is frustrating if you perhaps feel so far behind that you can’t follow the lectures or know where to even begin reading the textbook.
  • Engage with us sincerely, and we will do our very best to help you grow. It’s not too late.
  • I teach this course with this level of rigor and depth because I believe you are entitled to a world-class education that has a high ceiling to help you reach whatever career aspirations you have — whether it be going on to graduate school or becoming a Principal Software Engineer at a top-tier tech company.
  • I read every piece of feedback from you, and my door is always open for more feedback. I invite you to come meet with me to share your feedback. While it is not feasible for us to show how we address every piece of feedback, know that we continuously reflect and make adjustments. Also know that there are many competing constraints we have to consider, serving a large class and with a staff that is working extremely hard.
  • Again, come see us. We cannot help if we do not see you.

Today

  • Triage Your Questions
    • Static Type Checking?
    • Higher-Order Functions?
    • Review 3 on Semantics or Review 4 on Static Checking?
  • Encapsulating Effects

Questions?

  • Review:
    • What is static type checking and what is the benefit?

Questions?

Error Effects

Exceptions

defined function toDoubleException
res0_1: Double = 1.0
res0_2: Double = 4.2

Option

defined function toDoubleOption
defined function toDoubleNoNaNOption
defined function toDoubleOption
res3_1: Option[Double] = Some(value = 1.0)
res3_2: Option[Double] = Some(value = 4.2)
res3_3: Option[Double] = None
defined function toDoubleNoNaNOption
res4_1: Option[Double] = None
res4_2: Option[Double] = None
res4_3: Option[Double] = None
res4_4: Option[Double] = None
defined function addToDoubleOption
res5_1: Option[Double] = Some(value = 5.2)
res5_2: Option[Double] = None
res5_3: Option[Double] = None

Option as a Collection

defined function toDoubleNoNaNOption
defined function addToDoubleOption
defined function toDoubleNoNaNOption
defined function addToDoubleOption
res9_1: Option[Double] = Some(value = 5.2)
res9_2: Option[Double] = None
res9_3: Option[Double] = None

Map, Filter, FlatMap

Exercise 1 Implement map for Option[A]s:

defined function map

Exercise 2 Implement filter for Option[A]s:

defined function filter

Exercise 3 Implement flatMap for Option[A]s:

defined function flatMap

Comprehensions

defined function addToDoubleOption
defined function addToDoubleOption
res14_1: Option[Double] = Some(value = 5.2)
res14_2: Option[Double] = None
res14_3: Option[Double] = None

Either and Try

defined function toDoubleEither
defined function addToDoubleEither
import scala.util.Try
defined function toDoubleTry
defined function addToDoubleTry
defined function toDoubleEither
import scala.util.Try
defined function toDoubleTry

Mutation Effects

defined function freshVarImperative
defined function freshVar
freshVar: Int => (Int, String) = ammonite.$sess.cmd22$Helper$$Lambda$2244/0x0000000800b87840@3d5721bd
counter: Int = 0
counter_: Int = 1
x0: String = "x0"
counter__: Int = 2
x1: String = "x1"
counter___: Int = 3
x2: String = "x2"
counter: Int = 0
x0: String = "x0"
x1: String = "x1"
x2: String = "x2"

Encapsulating Mutation Effects

defined type DoWith
res25: Int => (Int, String) = ammonite.$sess.cmd22$Helper$$Lambda$2244/0x0000000800b87840@3d5721bd
defined function map
defined function flatMap
counter: Int = 0
defined function map
defined function flatMap
counter: Int = 0
counter___: Int = 3
x0: String = "x0"
x1: String = "x1"
x2: String = "x2"

DoWith

defined class DoWith
defined object DoWith
import DoWith._
defined function freshVar
counter: Int = 0
defined function freshVar
res35_1: (Int, String) = (1, "x0")
defined function freshVar
res36_1: (Int, String) = (1, "x0")
counter: Int = 0
counter___: Int = 3
x0: String = "x0"
x1: String = "x1"
x2: String = "x2"
counter: Int = 0
counter___: Int = 3
x0: String = "x0"
x1: String = "x1"
x2: String = "x2"