Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Sep 25 20:02
    joroKr21 commented #12463
  • Sep 25 09:30
    gaopinghuang0 opened #2190
  • Sep 24 18:28
    pjfanning commented #12327
  • Sep 24 18:28
    robert-blankenship commented #12327
  • Sep 24 18:27
    robert-blankenship commented #12327
  • Sep 24 18:25
    robert-blankenship commented #12327
  • Sep 24 17:34
    som-snytt commented #11631
  • Sep 24 15:28

    SethTisue on 2.13.x

    advance http4s to make it green… (compare)

  • Sep 24 14:11
    SethTisue commented on cf1b35b
  • Sep 24 14:10

    SethTisue on 2.13.x

    avro4s went green on JDK 17 (compare)

  • Sep 24 08:29
    scala-jenkins milestoned #9768
  • Sep 24 08:06
    NthPortal edited #9768
  • Sep 24 08:04
    NthPortal labeled #9768
  • Sep 24 08:04
    NthPortal opened #9768
  • Sep 24 08:04
    NthPortal labeled #9768
  • Sep 24 07:46
    nwk37011 synchronize #9752
  • Sep 24 07:12
    xuwei-k commented on cf1b35b
  • Sep 24 06:17
    som-snytt commented #12464
  • Sep 24 05:59
    NthPortal commented #9275
  • Sep 24 05:45
    NthPortal commented #9388
Luis Miguel Mejía Suárez
@BalmungSan
@renanreismartins yeah no worries about the thread, sorry if I sounded rude.
Fabio already explained it pretty well but maybe this helps, the code is expanded like this:
Candy.simulateMachine(inputs).run.apply(m) // we are calling the apply method on the run value which is an instance of the function class
Martijn Hoekstra
@martijnhoekstra
if you have a val x: Int => Int it's a value that returns a function
Luis Miguel Mejía Suárez
@BalmungSan
Here: final case class State[S, +A](run: S => (A, S)) when you create an State you have to pass a Function.
Basically the State data type is just a convenient wrapper over a S => (A, S) Function.
Fabio Labella
@SystemFw
@renanreismartins actually, that's the key conceptual point about State

when you create an State you have to pass a Function.

I think this view is confusing

but where is the definition of run where it takes the S and

so you have various State computations, right?

which you can combine together with map and flatMap and pure etc
Renan Reis
@renanreismartins
yes
Fabio Labella
@SystemFw
so, those computations are building the function
so what's the starting point?
the initial mini functions that get combined together
those are the primitive State computation, i.e get and put

I think this view is confusing

so actually I take this back

you can build a State computation by passing the function
and that's how get and put are built
Renan Reis
@renanreismartins
@SystemFw yes, the example from the book is quite complex, at least for me, the way he creates that function, so Im trying to decompose
Fabio Labella
@SystemFw
or, you can build State computations based on other State computations
ok, so what State represents is a sequence of transformations over an S
Renan Reis
@renanreismartins

Do you guys have a simple example?

The way the book creates the computation:

def simulateMachine(inputs: List[Input]): State[Machine, (Int, Int)] = for {
    _ <- sequence(inputs map (modify[Machine] _ compose update))
    s <- get
  } yield (s.coins, s.candies)
Fabio Labella
@SystemFw
which will result in an A
that's what State[S, A] means, "a sequence of transformation over an S, which will result in an A"
does this bit make sense?
I will get to simple examples soon, once the main concepts are clarified :)
Renan Reis
@renanreismartins

that's what State[S, A] means, "a sequence of transformation over an S, which will result in an A"

yes

Fabio Labella
@SystemFw
ok, so once you have built this sequence of transformations, you pass the initial state, and you get back the result A (and the final new state, which is also of type S)
does this also make sense?
this is what you do when you write mySequenceOfTransformationsWhichIsStateSA.run(initialState)
Renan Reis
@renanreismartins
Also does, looks like very much with Random number Generator, previously on the chapter. Till here i can follow you

this is what you do when you write mySequenceOfTransformationsWhichIsStateSA.run(initialState)

perfect :)

Luis Miguel Mejía Suárez
@BalmungSan
@renanreismartins btw, even if I know Fabio will be able to make it clear (because he always does). May I ask, what was your experience with Scala before that book?
It is quite complex and some people recommend learning the language + fp with it which IMHO it is really an overkill.
Fabio Labella
@SystemFw
right, so we can start by building the simplest State program, the program whose final result is the state itself
the name of this program is get, and its type is State[S, S], i.e. the sequence of transformations on S which will return S itself (which is what "getting the state" is)
fair enough?
Renan Reis
@renanreismartins

@BalmungSan I read:
Programming Scala - martin

did the coursera and...

https://alvinalexander.com/scala/functional-programming-simplified-book/

https://www.amazon.com/Functional-Programming-Java-functional-techniques/dp/1617292737 - this is very similar to the scala book, but in java and not sooooo deep, more into data structures than category theory / monads.

I had no problem in the Functional Scala till this chapter :/

Fabio Labella
@SystemFw
it's ok, this chapter is the first "real" one
don't get discouraged
Luis point is valid in general but I don't think it applies to you
it applies to people that still struggle heavily with the syntax and semantics of basics Scala, whereas in this case I feel like you are confused by the concepts and so started getting confused by scala as well (in the run question)
that being said, it's totally ok to do just part 1 of the Red Book, then go write code/read other books, then go back to Part 2 (and then Part 3)
Renan Reis
@renanreismartins
@BalmungSan thanks for checking man... let me know if you have any other content that you find relevant.
Fabio Labella
@SystemFw

he name of this program is get, and its type is State[S, S], i.e. the sequence of transformations on S which will return S itself (which is what "getting the state" is)

I can try and explain it all though, if you want. Does the above statement make sense?

Renan Reis
@renanreismartins
@SystemFw I'm getting there dont give up on me. Yes that makes sense too.
Fabio Labella
@SystemFw
ok cool so let's resume
Renan Reis
@renanreismartins
I dont know how to compose the transformations tho
Fabio Labella
@SystemFw
we need to build def get[S]: State[S, S], and the only way we can build State computations at this point is through the constructor, which case class State[S, A](run: S => (A, S)). In this case A == S, since we want our program get to be State[S, S]
we'll get there, but if you don't understand the primitives, it's harder to see the mechanics
Renan Reis
@renanreismartins

we need to build def get[S]: State[S, S], and the only way we can build State computations at this point is through the constructor, which case class State[S, A](run: S => (A, S)). In this case A == S, since we want our program get to be State[S, S]

im with you here too

Fabio Labella
@SystemFw
so to build get, we need to pass a function of type S => (S, S) to the constructor
which, when run, will take the input state, return it as the output, and leave it unmodified for the next state