Skip to content

Commit 41e93da

Browse files
committed
First stab at thinking in terms of composable programs.
1 parent e059fac commit 41e93da

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

algebraic-data-types/src/main/tut/index.html

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,40 @@
210210

211211
---
212212

213+
# Equational reasoning at the program level
214+
215+
<small>
216+
```scala
217+
def create(ca: CreateAccount) = Kleisli { (s: Session) =>
218+
(for {
219+
uid <- ca.owner.fold(
220+
id => id.point[BaseResultTaskET],
221+
cu => EitherT(UserService.create(cu, webUrl).run(s)).map(_._1))
222+
_ = logger.trace(s"createUser uid:$uid")
223+
bid <- ca.billing_address.fold(
224+
bid => bid.point[BaseResultTaskET],
225+
fa => EitherT(AddressService.create(fa).run(s)))
226+
_ = logger.trace(s"createAddress bid:$bid")
227+
cap = ca.parsed(uid, bid, ca.ig.fold(_.some, _ => none, _ => none))
228+
aid <- createAccount(cap).leftMap[BaseResultTask](BadRequest(_))
229+
_ <- ca.ig.fold(
230+
_ => ().point[BaseResultTaskET],
231+
csc => EitherT(SchoolService.create(csc.createSchool(aid.some, bid.left)).run(s)),
232+
csg => EitherT(InstitutionGroupService.create(csg.createIG(aid.some)).run(s)))
233+
_ = logger.trace(s"createAccount aid:$aid")
234+
} yield (aid, "Account created successfully")).run
235+
}
236+
```
237+
238+
Given a `CreateAccount`, we can construct a program that goes from a user
239+
`Session` to a `Program[A] = ConnectionIO[BaseResultTask \/ A]`. Lets name it
240+
```
241+
type SessionResult[A] = Kleisli[ConnectionIO, Session, BaseResultTask \/ A]
242+
```
243+
</small>
244+
245+
---
246+
213247
# References
214248

215249
* [Algebraic Data Types](http://tpolecat.github.io/presentations/algebraic_types.html) by Rob Norris (@tpolecat)
@@ -220,8 +254,11 @@
220254

221255
</textarea>
222256
<script src="http://gnab.github.io/remark/downloads/remark-latest.min.js" type="text/javascript"></script>
223-
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript"></script><script>
224-
var slideshow = remark.create();
257+
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML&delayStartupUntil=configured" type="text/javascript"></script>
258+
<script>
259+
var slideshow = remark.create({
260+
highlightLanguage: 'scala'
261+
});
225262
// Setup MathJax
226263
MathJax.Hub.Config({
227264
tex2jax: {

0 commit comments

Comments
 (0)