Netflix very quiet on Mac OS
Only volume levels on Macs are the one at the top on the menubar, and the one within the browser, as part of Netflix.
Related videos on Youtube
![Alfredo Gimenez](https://i.stack.imgur.com/Kn6X7.jpg?s=256&g=1)
Alfredo Gimenez
Updated on September 18, 2022Comments
-
Alfredo Gimenez almost 2 years
I have code where a class can provide modified copies of itself, like so:
case class A(i: Int, s: String) { def foo(ii: Int): A = copy(i = ii) def bar(ss: String): A = copy(s = ss) }
I want to create a function that takes some optional arguments and creates these modified copies using these arguments if they are defined:
def subA(a: A, oi: Option[Int] = None, os: Option[String] = None): A = { if (oi.isDefined && os.isDefined) a.foo(oi.get).bar(os.get) else if (oi.isDefined && !os.isDefined) a.foo(oi.get) else if (!oi.isDefined && os.isDefined) a.bar(os.get) else a }
This is clearly not sustainable, as I add new optional arguments, I have to create cases for every combination of arguments...
I also cannot do:
a.foo(oi.getOrElse(a.i)).bar(os.getOrElse(a.s))
Because in my actual code, if
oi
oros
is not provided, I should NOT run their associatedfoo
andbar
functions. In other words, I have no default arguments foroi
andos
, rather their existence defines whether I should run certain functions at all.Current solution, extend the class:
implicit class A_extended(a: A) { def fooOption(oi: Option[Int]): A = if (oi.isDefined) a.foo(oi.get) else a def barOption(os: Option[String]): A = if (os.isDefined) a.bar(os.get) else a } def subA(a: A, oi: Option[Int] = None, os: Option[String] = None): A = { a.fooOption(oi).barOption(os) }
But this problem comes up often and it's a bit tedious to do this constantly, is there something like:
// oi: Option[Int], foo: Int => A oi.ifDefinedThen(a.foo(_), a) // returns a.foo(oi.get) if oi is not None, else just a
Or should I just extend
Option
to provide this functionality?-
Russ Clarke over 12 yearsGood point, I've added that to the bottom of my question.
-
-
Russ Clarke over 12 yearsDisappointing but thanks anyway; I don't have the reputation required to vote you up, sorry.
-
Joseph over 12 yearsNo worries. I would suggest external speakers to help push that level higher.
-
Alfredo Gimenez over 7 yearsThis still requires enumerating out all combinations of
a
andb
, which gets really ugly when I have manyOption
arguments. -
Alfredo Gimenez over 7 yearsYay, I used to wonder why
Option
was iterable, this is yet another great reason for it to be. -
Nagarjuna Pamu over 7 years@spiffman using simple map and flatMap composition . edited the answer
-
Nagarjuna Pamu over 7 years@spiffman oi.map(a.foo).flatMap(a => os.map(a.bar)).getOrElse(a)
-
Alfredo Gimenez over 7 yearsThe
map
/flatMap
solution is incorrect unfortunately, trysubA(new A(5, "five"), None, Some("six"))
. If the first option isNone
then the output doesn't get chained to the second option. -
Alfredo Gimenez over 7 yearsThis has nothing to do with my actual question, but wow, this is cool! My worry with something like
updateInstance
is that it changes the value of an immutableval
, rendering it mutable. -
sarveshseri over 7 yearsI thought that you wanted a generic copier for case classes and were trying to get it done using
Option
s. Aso, If you do not need to update then you can just usecopyInstance
which reflects on the instace to capture its copy method and then uses it to create a copy with your updates fields. -
Alfredo Gimenez over 7 yearsThe copy functions were just to illustrate the problem of using multiple
Option
arguments to guard whether or not some functions are executed. Either way, I appreciate the submission, a lot of the reflection API documentation is dense, I learned more about reflection by reading your code :) -
sarveshseri over 7 yearsYup... your question looks pretty different from what I answered. Hope this answer helps some-one looking for such solution.
-
Alfredo Gimenez over 7 yearsYou should submit it to StackOverflow Documentation, there is currently next to nothing in the Scala Reflection page: stackoverflow.com/documentation/scala/5824/reflection