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
so the final definition is this
def get[S]: State[S, S] = State(s => (s, s))
see if you can reconcile it with the description above
Renan Reis
@renanreismartins

get is a function that returns State[S, S]

and to construct State, we need to pass to its constructor a function that receives S and returns (A, S). Am I correct?

Fabio Labella
@SystemFw
so far yes, but I need you to understand the specific function that I passed to State, because that function defines get specifically

will take the input state, return it as the output, and leave it unmodified for the next state

that's the English description of what it does, you need to see that same description in the code

Luis Miguel Mejía Suárez
@BalmungSan
@renanreismartins yeah no worries, as Fabio said I just wanted to be sure you were not struggling with too many things.
If the book keeps getting hard I may recommend reading Scala with Cats first, which is basically a simplified (and maybe more practical?) version of the same material; I recommend it because that book is extremely clear.
Also, if you find it hard to under things like effects or monads those videos were the ones that made the click for me,
Renan Reis
@renanreismartins
@SystemFw I think I understood so far. You are binding that function to run. The function takes its parameter and return the tuple.
Fabio Labella
@SystemFw
yes, and the tuple leaves the state unmodified, and returns it as the output as well
Renan Reis
@renanreismartins
yes
Fabio Labella
@SystemFw
ok cool, so we have build the first state computation, which we can run
get[Int].run(3)._1 == 3
should be unsurprising
but make sure you understand because we are going to be build the first important transformation next
Rob Norris
@tpolecat
To me State is the key to the kingdom. More than anything else it’s what made FP click for me. So this is fun.
Fabio Labella
@SystemFw
(._1 is a method on tuples that returns the first element, I basically want to get the A out of State[S, A], by passing the initial state and applying all the transformations, in this case the only transform is leaving the state unmodified and returning it, i.e. get)
Renan Reis
@renanreismartins
@SystemFw just a second before we go further...
Fabio Labella
@SystemFw

To me State is the key to the kingdom.

yeah, although State per se isn't really used in real world FP, all the main concepts you need to work with real effects are there really (or most of them)

just a second before we go further...

sure

Rob Norris
@tpolecat
I just used it on Friday!
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
yes, please
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