Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jul 27 23:42

    SethTisue on 2.13.x

    update dependencies.txt (compare)

  • Jul 27 23:40
    SethTisue milestoned #5606
  • Jul 27 23:40
    SethTisue demilestoned #5606
  • Jul 27 23:40
    SethTisue labeled #5606
  • Jul 27 23:40
    SethTisue assigned #5606
  • Jul 27 22:36

    SethTisue on 2.13.x

    akka-http is green now on JDK 17 (compare)

  • Jul 27 22:33

    SethTisue on 2.13.x

    fix macwire on JDK 17 (#1462) (compare)

  • Jul 27 22:33
    SethTisue closed #1462
  • Jul 27 22:10
    som-snytt commented #5606
  • Jul 27 22:09
    som-snytt synchronize #9712
  • Jul 27 22:03
    som-snytt edited #9712
  • Jul 27 22:00
    scala-jenkins milestoned #9712
  • Jul 27 22:00
    som-snytt opened #9712
  • Jul 27 21:28
    SethTisue edited #2143
  • Jul 27 21:28
    SethTisue synchronize #2143
  • Jul 27 21:18

    SethTisue on main

    style guide: discourage infix (… (compare)

  • Jul 27 21:18
    SethTisue closed #2140
  • Jul 27 21:18
    SethTisue commented #2140
  • Jul 27 21:17
    SethTisue synchronize #2140
  • Jul 27 21:17
    SethTisue synchronize #2140
Martijn Hoekstra
@martijnhoekstra
here is a start for basics in the language: https://docs.scala-lang.org/tour/basics.html
Renan Reis
@renanreismartins

Hello, I'm going through the Functional Programming in Scala and on the chapter about State we have:
case class State[S, +A](run: S => (A, S))

then we have a method
def simulateMachine(inputs: List[Input]): State[Machine, (Int, Int)]

later on:

val m: Machine = Machine(true, 5, 10)
Candy.simulateMachine(inputs).run(m)

What is making me confused is that "m" is of type machine and "run" expects a "S => (A, S)" function... so how could I pass Machine to "run"?

Gavin Bisesi
@Daenyth
In this case simulateMachine's S is Machine
State[Machine, *] so run takes S which is Machine
run provides the "initial" state, and then things go from there
1 reply
Luis Miguel Mejía Suárez
@BalmungSan
@renanreismartins run does not expects a function, run IS A function, you are calling it with the initial state.
(btw, please do not use threads, they are hard to read)
Renan Reis
@renanreismartins
Sorry, I did not know about the thread etiquete.
Rob Norris
@tpolecat
There is no way you would have known.
Renan Reis
@renanreismartins
@BalmungSan when you say run is a function, where is it defined?
Fabio Labella
@SystemFw
State has a field run of type S => (A, S) (a function from S to a tuple of A and S)
when you say .run, you access that field
when you add (), you call Function.apply (under the hood) therefore passing the S to the function
the S type parameter in this case is equal to Machine, which is why you can pass a Machine to the function
Renan Reis
@renanreismartins
@SystemFw thanks for the explanation. Kinda makes sense to me, but where is the definition of run where it takes the S and makes it on a (A, S) ?
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 :/