版博士V2.0程序
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

Store.js 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { identity, pipe } from './function';
  2. import { flap as flap_ } from './Functor';
  3. /**
  4. * Reposition the focus at the specified position
  5. *
  6. * @since 2.0.0
  7. */
  8. export function seek(s) {
  9. return function (wa) { return ({ peek: wa.peek, pos: s }); };
  10. }
  11. /**
  12. * Reposition the focus at the specified position, which depends on the current position
  13. *
  14. * @since 2.0.0
  15. */
  16. export function seeks(f) {
  17. return function (wa) { return ({ peek: wa.peek, pos: f(wa.pos) }); };
  18. }
  19. /**
  20. * Extract a value from a position which depends on the current position
  21. *
  22. * @since 2.0.0
  23. */
  24. export function peeks(f) {
  25. return function (wa) { return wa.peek(f(wa.pos)); };
  26. }
  27. export function experiment(F) {
  28. return function (f) { return function (wa) { return F.map(f(wa.pos), function (s) { return wa.peek(s); }); }; };
  29. }
  30. /* istanbul ignore next */
  31. var _map = function (fa, f) { return pipe(fa, map(f)); };
  32. /* istanbul ignore next */
  33. var _extend = function (wa, f) { return pipe(wa, extend(f)); };
  34. /**
  35. * @since 2.0.0
  36. */
  37. export var extend = function (f) { return function (wa) { return ({
  38. peek: function (s) { return f({ peek: wa.peek, pos: s }); },
  39. pos: wa.pos
  40. }); }; };
  41. /**
  42. * @category Extract
  43. * @since 2.6.2
  44. */
  45. export var extract = function (wa) { return wa.peek(wa.pos); };
  46. /**
  47. * @since 2.0.0
  48. */
  49. export var duplicate = /*#__PURE__*/ extend(identity);
  50. /**
  51. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  52. * use the type constructor `F` to represent some computational context.
  53. *
  54. * @category mapping
  55. * @since 2.0.0
  56. */
  57. export var map = function (f) { return function (fa) { return ({
  58. peek: function (s) { return f(fa.peek(s)); },
  59. pos: fa.pos
  60. }); }; };
  61. /**
  62. * @category type lambdas
  63. * @since 2.0.0
  64. */
  65. export var URI = 'Store';
  66. /**
  67. * @category instances
  68. * @since 2.7.0
  69. */
  70. export var Functor = {
  71. URI: URI,
  72. map: _map
  73. };
  74. /**
  75. * @category mapping
  76. * @since 2.10.0
  77. */
  78. export var flap = /*#__PURE__*/ flap_(Functor);
  79. /**
  80. * @category instances
  81. * @since 2.7.0
  82. */
  83. export var Comonad = {
  84. URI: URI,
  85. map: _map,
  86. extend: _extend,
  87. extract: extract
  88. };
  89. // -------------------------------------------------------------------------------------
  90. // deprecated
  91. // -------------------------------------------------------------------------------------
  92. /**
  93. * This instance is deprecated, use small, specific instances instead.
  94. * For example if a function needs a `Comonad` instance, pass `S.Comonad` instead of `S.store`
  95. * (where `S` is from `import S from 'fp-ts/Store'`)
  96. *
  97. * @category zone of death
  98. * @since 2.0.0
  99. * @deprecated
  100. */
  101. export var store = Comonad;