Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jul 28 18:07
    julienrf synchronize #2136
  • Jul 28 10:29
    som-snytt commented #12378
  • Jul 28 09:38
    jxnu-liguobin commented #12378
  • Jul 28 09:35
    dwijnand commented #12435
  • Jul 28 09:00
    som-snytt commented #12435
  • Jul 28 08:50
    Jasper-M commented #12435
  • Jul 28 08:46
    dwijnand commented #12435
  • Jul 28 08:46
    dwijnand commented #12435
  • Jul 28 08:43
    dwijnand commented #12435
  • Jul 28 08:30

    julienrf on main

    Inkuire docs Merge pull request #2125 from K… (compare)

  • Jul 28 08:30
    julienrf closed #2125
  • Jul 28 08:12
    KacperFKorban commented #2125
  • Jul 28 08:11
    KacperFKorban synchronize #2125
  • Jul 28 08:11
    KacperFKorban synchronize #2125
  • Jul 28 08:04
    KacperFKorban review_requested #2125
  • Jul 28 08:03
    KacperFKorban synchronize #2125
  • Jul 28 08:01
    KacperFKorban synchronize #2125
  • Jul 28 08:01
    KacperFKorban synchronize #2125
  • Jul 28 08:00
    KacperFKorban synchronize #2125
  • Jul 28 08:00
    KacperFKorban synchronize #2125
Gavin Bisesi
@Daenyth
but that feels weird
@RoelofWobben what type are you calling flatMap on? Success is one of the members and left and right both have that type
Fabio Labella
@SystemFw
why do you find it weird?
Rob Norris
@tpolecat
I think that's like flatMap into pure … consider e.flatMap(a => Right(f(a))) which is e.map(f)
Fabio Labella
@SystemFw
yes
that's it
it's a map, so yield
Avremel Kaminetzky
@avremel
I have values in Future[Try[Option[Double]]] and Future[Option[Double]]. How can I add them? Should I be using Future.sequence?
RoelofWobben
@RoelofWobben
bad luck for me , yield is also not explained
Avremel Kaminetzky
@avremel
Or is there a way to use Future[Try[Option[Double]]] as Future[Option[Double]]?
RoelofWobben
@RoelofWobben
so there is no way I can refractor the this part out :
left.eval flatMap { x => right.eval flatMap(y
Gavin Bisesi
@Daenyth
@RoelofWobben
for {
  x <- left.eval
  y <- right.eval
} yield x + y
RoelofWobben
@RoelofWobben
oke, and I can put that into a function and call it
Fabio Labella
@SystemFw
I think the reason those things are not explained is that they are using the exercises to build the intuition you need to understand for/yield, once they do get there
RoelofWobben
@RoelofWobben
nope, in the other where I use it , it's x - y
so that code is not working
Fabio Labella
@SystemFw
it is
RoelofWobben
@RoelofWobben
@SystemFw for /yield is explained in the next chapter
Fabio Labella
@SystemFw
just pass an f: (Int, Int) => Int
and yield f(x, y)
exactly, so by doing this refactoring you are kinda "cheating"
RoelofWobben
@RoelofWobben
oke, then it let it be
I do not like cheating
Marcin Sokrates
@marcinsokrates_twitter
@avremel you should refactor to get rid of Future[Try[Option[Double]]] because it's redundant, Future.fromTry lets you skip the Try
RoelofWobben
@RoelofWobben
thanks all
time for the chapter about the for/yield and Option
Avremel Kaminetzky
@avremel
@marcinsokrates_twitter interesting. I was using toOption.flatten to remove the Try
  private def calculateShippingPriceRedo(cookie: String, country: Option[String], state: Option[String], shippingMethodId: Option[Int] = None): Future[Option[Double]] = {
    val countryWithDefault = country.getOrElse("US")

    shippingOptionsFull(cookie, country=countryWithDefault, state).map{ opt => opt.map{ rows =>
      shippingMethodId.flatMap{ id =>
          rows.find(_.shippingSpeedType === id ).orElse(rows.headOption).map(_.charge)
        }
      }.toOption.flatten
    }
  }
Derek Wickern
@dwickern
it depends what you want
Avremel Kaminetzky
@avremel
How would I use Future.fromTry?
Derek Wickern
@dwickern
do you want a successful future with None when the Try fails
Rob Norris
@tpolecat
Future[Try is usually wrong because Future already has the bulletproof behavior.
Derek Wickern
@dwickern
or a failed future
Avremel Kaminetzky
@avremel
Ye, it's someone's code, i don't want to refactor his code yet
Rob Norris
@tpolecat
So fto.flatMap(Future.fromTry) should give you Future[Option[Double]]
Avremel Kaminetzky
@avremel
Oh, thanks for that
And OptionT to add Future[Option[Double]], right?
Rob Norris
@tpolecat
You have two of those and want to add the doubles together?
Avremel Kaminetzky
@avremel
Yes
Rob Norris
@tpolecat
Yeah OptionT or Nested if you're using cats.
You don't need a monad transformer there so Nested is a better choice.
@ val a = Future(Option(42)) 
a: Future[Option[Int]] = Future(Success(Some(42)))

@ (Nested(a), Nested(a)).mapN(_ + _).value 
res7: Future[Option[Int]] = Future(Success(Some(84)))
Actually you can use the bare semigroup in this case.
@ val a = Future(Option(42.0)) 
a: Future[Option[Double]] = Future(Success(Some(42.0)))

@ a |+| a 
res13: Future[Option[Double]] = Future(Success(Some(84.0)))
Avremel Kaminetzky
@avremel
cool!
Rob Norris
@tpolecat
Well that one collapses None to 0.0 which may not be what you want.
Avremel Kaminetzky
@avremel
the semigroup?
Rob Norris
@tpolecat
The first one gives you None if either of them is None.
There are a bunch of monoids for Option so you kind of have to be careful.
Avremel Kaminetzky
@avremel
How can I recover with Future.failed if any value is None?