## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
• Create your own community
##### 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
ahah nice
Renan Reis
@renanreismartins
the ._1 is fine we accessing the first element of the tuple... no worries here
...
get[Int].run(3)._1 == 3
val a: State[Int, Int] = get[Int]
Fabio Labella
@SystemFw
yeah?
all good there
can we proceed?
Renan Reis
@renanreismartins
get[Int].run(3)._1 == 3
val a: State[Int, Int] = get[Int]
val b: (Int, Int) = a.run(3)
val r1 = b._1
val s1 = b._2
I'm kinda understanding, it is a miracle LOL
Fabio Labella
@SystemFw
nice, can I move on to the first interesting transformation then?
Renan Reis
@renanreismartins
so get just get the State and return the same State, and the "element" resulting from the State is also the State
Fabio Labella
@SystemFw
exactly yeah
Renan Reis
@renanreismartins
Fabio Labella
@SystemFw
ok, so the next thing I want to be able to do, is the ability to transform the output
for example, "get an Int, then apply the transformation that increments that Int, then apply the transformation that transforms that Int to a String"
we're going to look at the shape of it first, and then the implementation
so, recall that in State[S, A], A is the output
Renan Reis
@renanreismartins
right
Fabio Labella
@SystemFw
so "get an Int" will have type get: State[Int, Int]
"increment that Int" will have type State[Int, Int] (the first Int is the state, and stays as it is, the second state is the result, and gets incremented)
"transform that Int to String" will have type State[Int, String] (the state is an Int, the result is now String)
makes sense?
Renan Reis
@renanreismartins
totally
Fabio Labella
@SystemFw
ok so "transforming the output" means taking a State[S, A] and returning a State[S, B], for any two A and B (Int and Int and Int and String, in this case)
Renan Reis
@renanreismartins
right
Fabio Labella
@SystemFw
we also need something that actually does the transformation, i.e. takes an A, it gives a B. This is what functions do: A => B
Renan Reis
@renanreismartins
like a map i guess?
Fabio Labella
@SystemFw
so we can show the shape of this transformation
it is map yeah :)
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
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,