Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 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
  • Dec 15 2021 02:58
    SethTisue synchronize #1312
Fabio Labella
@SystemFw
but before State you don't really look at it as "transforming the output of a program"
Renan Reis
@renanreismartins
get[Int].map(_ + 1)
Fabio Labella
@SystemFw
exactly, you got it
can you also write the signature of map? (don't worry about making it infix)
get[Int].map(_ + 1).map(_.toString) is describing the sequence of transformations (that I described above) over an Int , that we will pass eventually, in run
Renan Reis
@renanreismartins
not trying to overrun ur explanation, just putting the brain to work. yes. I was thinking on somethinng like that
Fabio Labella
@SystemFw
no that' s ok if you picked up the pace
that's why I ask at which point you are (behind, at, or beyond)
Renan Reis
@renanreismartins

can you also write the signature of map? (don't worry about making it infix)

give me a moment

Fabio Labella
@SystemFw
yeah sure
Renan Reis
@renanreismartins
I did something for the exercises, but I want to revisit
Fabio Labella
@SystemFw
don't worry about the implementation yet, just see if you can work out the signature, given the description above
and let me know if you get stuck
Renan Reis
@renanreismartins

signature is fine, I was thinking of the implementation

def map[B](f: A => B): State[S, B]

Fabio Labella
@SystemFw
I prefer to write the signature as def map[A, B]: State[S, A] => (A => B) => State[S, B]
I.e. show the input state as well
the real one will look like yours because it's inside State to allow to be called infix
but yeah basically they are equivalent
so now we need to implement it
Renan Reis
@renanreismartins
yes, but the infix ones gives me the impression we are working with the obj and then confuses me a bit. Do you mind if i reason about the one you wrote?
I understood it already actually.
Fabio Labella
@SystemFw
yeah that's why I use the non infix one to explain :)
but when I write example I cheat and write get[Int].map.. etc
scala 3 will have infix functions and solve this annoying pedagogical trap
Renan Reis
@renanreismartins
I will put that on the companion object instead of the case class,
Fabio Labella
@SystemFw
anyway, sounds like you understood the signature, and understood how to use it as well
Renan Reis
@renanreismartins
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