Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jul 19 20:37
    @SethTisue banned @tonyobanon
  • Jan 02 23:58
    @SethTisue banned @fakubishes:nerdsin.space
  • Dec 15 2021 05:01
    som-snytt commented #12516
  • Dec 15 2021 04:38
    SethTisue edited #1312
  • Dec 15 2021 04:38
    SethTisue opened #2273
  • Dec 15 2021 04:31
    jackkoenig opened #12516
  • Dec 15 2021 04:29
    SethTisue edited #1312
  • Dec 15 2021 04:28
    SethTisue edited #1312
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    scala-jenkins milestoned #9831
  • Dec 15 2021 04:27
    SethTisue labeled #9831
  • Dec 15 2021 04:27
    SethTisue opened #9831
  • Dec 15 2021 03:35
    som-snytt commented #11339
  • Dec 15 2021 03:27
    som-snytt labeled #12494
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:07
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 03:05
    SethTisue edited #1312
  • Dec 15 2021 02:58
    SethTisue edited #1312
Fabio Labella
@SystemFw
anyway, sounds like you understood the signature, and understood how to use it as well
Renan Reis
@renanreismartins
I will put that on the companion object instead of the case class,
yes
Fabio Labella
@SystemFw
the problem is that using get in that shape is annoying, and the one in the class is better. Understanding the one in the object is better though yeah
anyway, we just need to implement it now
we will try to follow type driven programming
it's a bit verbose in chat, but I'll try
also if a moderator prefers this convo to be moved to DM, please let me know (Seth basically)
ok so
def map[A, B]: State[S, A] => (A => B) => State[S, B]
we always start from the end, i.e. State[S, B]
how do we create a State[S, B]? at this point the only thing we have that type checks, it's the constructor
Renan Reis
@renanreismartins

im implementing it, one moment.

I think more people can benefit from such explanation.

Fabio Labella
@SystemFw
ah ok I'll let you try
Renan Reis
@renanreismartins
I'm stuck inside a monad lol. how can I get A?
Fabio Labella
@SystemFw
ok let's follow the process I started instead
which is type-driven programming

how do we create a State[S, B]? at this point the only thing we have that type checks, it's the constructor

so that's where we got to

the constructor takes an S => (S, B)
so we have the first step
def map[A, B](fa: State[S, A])(f:  A => B) : State[S, B] = State[S, B] { s: S =>
  // more work to do here
  def result: (S, B) = ???
  result
}
make sense so far?
this method is very useful in general btw
(we are getting at the same point you got to I think, but I think that showing the method will also show how to get unstuck)
Renan Reis
@renanreismartins
we need to take A and apply F
Fabio Labella
@SystemFw
ok, so we have another step
if I was in an editor, I'll type it properly with a helpful layout, but it's not tenable here unfortunately
Renan Reis
@renanreismartins
val a: (A, S) = fa.run(s)
Fabio Labella
@SystemFw
ah ok, you did get it, nice
can you see if you can complete the whole thing now?
Renan Reis
@renanreismartins
def map[S, A, B](fa: State[S, A])(f: A => B): State[S, B] = State[S, B] { s: S =>
    val (a, s2) = fa.run(s)
    val b: B = f(a)
    (b, s2)
  }
Fabio Labella
@SystemFw
cool
so one great thing to point out here
if you look at this example
val a = get[Int].map(_ + 1).map(_.toString)

a.run(3)._1 // "4"
a.run(7)._1 // "8"
even though we haven't looked at State in its entirety yet (pure, flatMap, put, modify etc)
we have already answered your initial question
which was "who builds the run function"
and you can see that there are primitives like get, which build simple functions directly
and combinators like map, which build more complex functions based on previous State computations
so that whole state computations (in this case a), end up having a more complex function which represents the sequence of transformations
and when you are done building that sequence of transformations, you can apply it to various initial states, by passing them to the run functions, and get the result out
does that make sense?
Renan Reis
@renanreismartins
yes, it does
State.map(get[Int])(_ + 1).run(1) in our case, because we do not use infix
Fabio Labella
@SystemFw
yeah
you can follow the same reasoning to understand pure, flatMap and put
the rest happens purely in combinator land
e.g. modify is get.flatMap(s => put(f(s))
Renan Reis
@renanreismartins
damn, I understood it.
Fabio Labella
@SystemFw
:+1: