Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce stack depth in StateT #1466

Merged
merged 4 commits into from
Dec 31, 2016
Merged

Reduce stack depth in StateT #1466

merged 4 commits into from
Dec 31, 2016

Conversation

johnynek
Copy link
Contributor

I hit some stack overflows using traverse with StateT. I don't think this will always solve the issue, but I tried to minimize stack depth in a number of cases.

@johnynek
Copy link
Contributor Author

looks like a flakey test (Futures), restarting.

@codecov-io
Copy link

codecov-io commented Nov 16, 2016

Current coverage is 92.23% (diff: 100%)

Merging #1466 into master will increase coverage by 0.03%

@@             master      #1466   diff @@
==========================================
  Files           242        242          
  Lines          3619       3646    +27   
  Methods        3549       3577    +28   
  Messages          0          0          
  Branches         70         69     -1   
==========================================
+ Hits           3337       3363    +26   
- Misses          282        283     +1   
  Partials          0          0          

Powered by Codecov. Last update 0fd61d6...d5e371f

@@ -70,9 +77,12 @@ final class StateT[F[_], S, A](val runF: F[S => F[(S, A)]]) extends Serializable
* Like [[map]], but also allows the state (`S`) value to be modified.
*/
def transform[B](f: (S, A) => (S, B))(implicit F: Monad[F]): StateT[F, S, B] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only needs Functor[F] in this formulation.

@@ -12,25 +12,32 @@ import cats.syntax.either._
final class StateT[F[_], S, A](val runF: F[S => F[(S, A)]]) extends Serializable {

def flatMap[B](fas: A => StateT[F, S, B])(implicit F: Monad[F]): StateT[F, S, B] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only needs FlatMap[F] in this formulation, but I didn't want to change binary compatibility for a negligible gain.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense for now. Maybe add a comment to that effect? If we ever want to tighten things up later that will be handy.

fas(a).run(s)
}
})
}
})

def flatMapF[B](faf: A => F[B])(implicit F: Monad[F]): StateT[F, S, B] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only needs FlatMap[F] in the current formulation.

}
)
})

def map[B](f: A => B)(implicit F: Monad[F]): StateT[F, S, B] =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we fix transform to only use Functor[F] then this only needs Functor[F], which is fitting for map.

@non
Copy link
Contributor

non commented Nov 16, 2016

These changes seem positive to me.

I think we should either document all the places where type class bounds could be tightened, or just do the change now. While breaking compatibility is a drag, better now than at 1.0.

👍 whichever way you go.

@johnynek
Copy link
Contributor Author

Okay, I lowered everything about as much as I think you can. Nice to only need Functor[F] in many places, which makes also worthwhile to add a Functor[StateT[F, S, ?]] instance.

@non
Copy link
Contributor

non commented Nov 16, 2016

👍

(EDIT: We had a test timeout so I restarted them.)

@johnynek
Copy link
Contributor Author

@adelbertc what do you think?

@adelbertc
Copy link
Contributor

LGTM 👍

@johnynek johnynek mentioned this pull request Dec 30, 2016
11 tasks
@johnynek johnynek merged commit a6efd9d into master Dec 31, 2016
@johnynek johnynek deleted the oscar/add-statet-funcs branch December 31, 2016 23:49
@stew stew removed the in progress label Dec 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants