|
210 | 210 |
|
211 | 211 | ---
|
212 | 212 |
|
| 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 | + |
213 | 247 | # References
|
214 | 248 |
|
215 | 249 | * [Algebraic Data Types](http://tpolecat.github.io/presentations/algebraic_types.html) by Rob Norris (@tpolecat)
|
|
220 | 254 |
|
221 | 255 | </textarea>
|
222 | 256 | <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 | + }); |
225 | 262 | // Setup MathJax
|
226 | 263 | MathJax.Hub.Config({
|
227 | 264 | tex2jax: {
|
|
0 commit comments