gott.adoc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. = gott
  2. apiote <me@apiote.xyz>
  3. v1.1.2 2020-04-24
  4. :toc:
  5. gott is a Railway Oriented Programming library for Go.
  6. [WARNING]
  7. Version 1 of gott is deprecated, please use version 2 which uses generics
  8. In ROP a program is a chain of functions wrapped in blocks resembling track switches. It’s a simplification of an Either monad.
  9. gott provides N types of blocks:
  10. * Bind, which wraps a function that continues on the happy track or switches to the sad track
  11. >------+-------->
  12. \
  13. \
  14. >---------+----->
  15. * Map, which wraps a function that will alwayc continue on the happy track
  16. >--------------->
  17. >--------------->
  18. * Tee, which wraps a function performing side effects and can switch to the sad track
  19. _
  20. |
  21. >--------++----->
  22. \
  23. \
  24. >------------+-->
  25. * SafeTee, which is to Tee what Map is to Bind
  26. _
  27. |
  28. >--------+------>
  29. >--------------->
  30. * Recover, which wraps a function that tries to return to the happy Path
  31. >--------+------>
  32. /
  33. /
  34. >-----+--------->
  35. * Catch, which switches to the sad track in case of a panic
  36. * Handle, which does different things depending on which track the processing is
  37. == Usage
  38. Provided functions are methods of `Result` and return `Result` so that they can be chained.
  39. Usage can be seen in tests, the simplest being
  40. import (
  41. "apiote.xyz/p/gott"
  42. )
  43. func divide5(by ...interface{}) (interface{}, error) {
  44. if b := by[0].(int); b == 0 {
  45. return Tuple(by), errors.New("divideByZero")
  46. } else {
  47. return 5 / b, nil
  48. }
  49. func main() {
  50. s, e := NewResult(5).Bind(divide5).Finish()
  51. // s == 1; e == nil
  52. }
  53. == Mirrors
  54. The canonical repository for this project is https://git.apiote.xyz/gott.git it’s mirrored at https://notabug.org/apiote/gott
  55. Mirrors exist solely for the sake of the code and any additional functions provided by third-party services (including but not limited to issues and pull requests) will not be used and will be ignored.
  56. == License
  57. ----
  58. This Source Code Form is subject to the terms of the Mozilla Public
  59. License, v. 2.0. If a copy of the MPL was not distributed with this
  60. file, You can obtain one at https://mozilla.org/MPL/2.0/.
  61. ----