版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

384 Zeilen
11 KiB

  1. /**
  2. * Multi-way trees (aka rose trees) and forests, where a forest is
  3. *
  4. * ```ts
  5. * type Forest<A> = Array<Tree<A>>
  6. * ```
  7. *
  8. * @since 2.0.0
  9. */
  10. import { Applicative1 } from './Applicative'
  11. import { Apply1 } from './Apply'
  12. import { Chain1 } from './Chain'
  13. import { Comonad1 } from './Comonad'
  14. import { Eq } from './Eq'
  15. import { Foldable1 } from './Foldable'
  16. import { Functor1 } from './Functor'
  17. import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT'
  18. import { Monad as MonadHKT, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad'
  19. import { Monoid } from './Monoid'
  20. import { Pointed1 } from './Pointed'
  21. import { Predicate } from './Predicate'
  22. import { Show } from './Show'
  23. import { PipeableTraverse1, Traversable1 } from './Traversable'
  24. /**
  25. * @category model
  26. * @since 2.0.0
  27. */
  28. export declare type Forest<A> = Array<Tree<A>>
  29. /**
  30. * @category model
  31. * @since 2.0.0
  32. */
  33. export interface Tree<A> {
  34. readonly value: A
  35. readonly forest: Forest<A>
  36. }
  37. /**
  38. * @category constructors
  39. * @since 2.0.0
  40. */
  41. export declare function make<A>(value: A, forest?: Forest<A>): Tree<A>
  42. /**
  43. * @category instances
  44. * @since 2.0.0
  45. */
  46. export declare function getShow<A>(S: Show<A>): Show<Tree<A>>
  47. /**
  48. * @category instances
  49. * @since 2.0.0
  50. */
  51. export declare function getEq<A>(E: Eq<A>): Eq<Tree<A>>
  52. /**
  53. * Neat 2-dimensional drawing of a forest
  54. *
  55. * @since 2.0.0
  56. */
  57. export declare function drawForest(forest: Forest<string>): string
  58. /**
  59. * Neat 2-dimensional drawing of a tree
  60. *
  61. * @example
  62. * import { make, drawTree } from 'fp-ts/Tree'
  63. *
  64. * const fa = make('a', [
  65. * make('b'),
  66. * make('c'),
  67. * make('d', [make('e'), make('f')])
  68. * ])
  69. *
  70. * assert.strictEqual(drawTree(fa), `a
  71. * ├─ b
  72. * ├─ c
  73. * └─ d
  74. * ├─ e
  75. * └─ f`)
  76. *
  77. *
  78. * @since 2.0.0
  79. */
  80. export declare function drawTree(tree: Tree<string>): string
  81. /**
  82. * Build a (possibly infinite) tree from a seed value in breadth-first order.
  83. *
  84. * @category constructors
  85. * @since 2.0.0
  86. */
  87. export declare function unfoldTree<A, B>(b: B, f: (b: B) => [A, Array<B>]): Tree<A>
  88. /**
  89. * Build a (possibly infinite) forest from a list of seed values in breadth-first order.
  90. *
  91. * @category constructors
  92. * @since 2.0.0
  93. */
  94. export declare function unfoldForest<A, B>(bs: Array<B>, f: (b: B) => [A, Array<B>]): Forest<A>
  95. /**
  96. * Monadic tree builder, in depth-first order
  97. *
  98. * @category constructors
  99. * @since 2.0.0
  100. */
  101. export declare function unfoldTreeM<M extends URIS4>(
  102. M: Monad4<M>
  103. ): <S, R, E, A, B>(b: B, f: (b: B) => Kind4<M, S, R, E, [A, Array<B>]>) => Kind4<M, S, R, E, Tree<A>>
  104. export declare function unfoldTreeM<M extends URIS3>(
  105. M: Monad3<M>
  106. ): <R, E, A, B>(b: B, f: (b: B) => Kind3<M, R, E, [A, Array<B>]>) => Kind3<M, R, E, Tree<A>>
  107. export declare function unfoldTreeM<M extends URIS3, E>(
  108. M: Monad3C<M, E>
  109. ): <R, A, B>(b: B, f: (b: B) => Kind3<M, R, E, [A, Array<B>]>) => Kind3<M, R, E, Tree<A>>
  110. export declare function unfoldTreeM<M extends URIS2>(
  111. M: Monad2<M>
  112. ): <E, A, B>(b: B, f: (b: B) => Kind2<M, E, [A, Array<B>]>) => Kind2<M, E, Tree<A>>
  113. export declare function unfoldTreeM<M extends URIS2, E>(
  114. M: Monad2C<M, E>
  115. ): <A, B>(b: B, f: (b: B) => Kind2<M, E, [A, Array<B>]>) => Kind2<M, E, Tree<A>>
  116. export declare function unfoldTreeM<M extends URIS>(
  117. M: Monad1<M>
  118. ): <A, B>(b: B, f: (b: B) => Kind<M, [A, Array<B>]>) => Kind<M, Tree<A>>
  119. export declare function unfoldTreeM<M>(
  120. M: MonadHKT<M>
  121. ): <A, B>(b: B, f: (b: B) => HKT<M, [A, Array<B>]>) => HKT<M, Tree<A>>
  122. /**
  123. * Monadic forest builder, in depth-first order
  124. *
  125. * @category constructors
  126. * @since 2.0.0
  127. */
  128. export declare function unfoldForestM<M extends URIS4>(
  129. M: Monad4<M>
  130. ): <S, R, E, A, B>(bs: Array<B>, f: (b: B) => Kind4<M, S, R, E, [A, Array<B>]>) => Kind4<M, S, R, E, Forest<A>>
  131. export declare function unfoldForestM<M extends URIS3>(
  132. M: Monad3<M>
  133. ): <R, E, A, B>(bs: Array<B>, f: (b: B) => Kind3<M, R, E, [A, Array<B>]>) => Kind3<M, R, E, Forest<A>>
  134. export declare function unfoldForestM<M extends URIS3, E>(
  135. M: Monad3C<M, E>
  136. ): <R, A, B>(bs: Array<B>, f: (b: B) => Kind3<M, R, E, [A, Array<B>]>) => Kind3<M, R, E, Forest<A>>
  137. export declare function unfoldForestM<M extends URIS2>(
  138. M: Monad2<M>
  139. ): <R, E, B>(bs: Array<B>, f: (b: B) => Kind2<M, R, [E, Array<B>]>) => Kind2<M, R, Forest<E>>
  140. export declare function unfoldForestM<M extends URIS2, E>(
  141. M: Monad2C<M, E>
  142. ): <A, B>(bs: Array<B>, f: (b: B) => Kind2<M, E, [A, Array<B>]>) => Kind2<M, E, Forest<A>>
  143. export declare function unfoldForestM<M extends URIS>(
  144. M: Monad1<M>
  145. ): <A, B>(bs: Array<B>, f: (b: B) => Kind<M, [A, Array<B>]>) => Kind<M, Forest<A>>
  146. export declare function unfoldForestM<M>(
  147. M: MonadHKT<M>
  148. ): <A, B>(bs: Array<B>, f: (b: B) => HKT<M, [A, Array<B>]>) => HKT<M, Forest<A>>
  149. /**
  150. * Fold a tree into a "summary" value in depth-first order.
  151. *
  152. * For each node in the tree, apply `f` to the `value` and the result of applying `f` to each `forest`.
  153. *
  154. * This is also known as the catamorphism on trees.
  155. *
  156. * @example
  157. * import { fold, make } from 'fp-ts/Tree'
  158. * import { concatAll } from 'fp-ts/Monoid'
  159. * import { MonoidSum } from 'fp-ts/number'
  160. *
  161. * const t = make(1, [make(2), make(3)])
  162. *
  163. * const sum = concatAll(MonoidSum)
  164. *
  165. * // Sum the values in a tree:
  166. * assert.deepStrictEqual(fold((a: number, bs: Array<number>) => a + sum(bs))(t), 6)
  167. *
  168. * // Find the maximum value in the tree:
  169. * assert.deepStrictEqual(fold((a: number, bs: Array<number>) => bs.reduce((b, acc) => Math.max(b, acc), a))(t), 3)
  170. *
  171. * // Count the number of leaves in the tree:
  172. * assert.deepStrictEqual(fold((_: number, bs: Array<number>) => (bs.length === 0 ? 1 : sum(bs)))(t), 2)
  173. *
  174. * @category folding
  175. * @since 2.6.0
  176. */
  177. export declare function fold<A, B>(f: (a: A, bs: Array<B>) => B): (tree: Tree<A>) => B
  178. /**
  179. * @since 2.0.0
  180. */
  181. export declare const ap: <A>(fa: Tree<A>) => <B>(fab: Tree<(a: A) => B>) => Tree<B>
  182. /**
  183. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  184. *
  185. * @category Monad
  186. * @since 2.0.0
  187. */
  188. export declare const chain: <A, B>(f: (a: A) => Tree<B>) => (ma: Tree<A>) => Tree<B>
  189. /**
  190. * @since 2.0.0
  191. */
  192. export declare const extend: <A, B>(f: (wa: Tree<A>) => B) => (wa: Tree<A>) => Tree<B>
  193. /**
  194. * @since 2.0.0
  195. */
  196. export declare const duplicate: <A>(wa: Tree<A>) => Tree<Tree<A>>
  197. /**
  198. * @category sequencing
  199. * @since 2.0.0
  200. */
  201. export declare const flatten: <A>(mma: Tree<Tree<A>>) => Tree<A>
  202. /**
  203. * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F<A>) => F<B>` whose argument and return types
  204. * use the type constructor `F` to represent some computational context.
  205. *
  206. * @category mapping
  207. * @since 2.0.0
  208. */
  209. export declare const map: <A, B>(f: (a: A) => B) => (fa: Tree<A>) => Tree<B>
  210. /**
  211. * @category folding
  212. * @since 2.0.0
  213. */
  214. export declare const reduce: <A, B>(b: B, f: (b: B, a: A) => B) => (fa: Tree<A>) => B
  215. /**
  216. * @category folding
  217. * @since 2.0.0
  218. */
  219. export declare const foldMap: <M>(M: Monoid<M>) => <A>(f: (a: A) => M) => (fa: Tree<A>) => M
  220. /**
  221. * @category folding
  222. * @since 2.0.0
  223. */
  224. export declare const reduceRight: <A, B>(b: B, f: (a: A, b: B) => B) => (fa: Tree<A>) => B
  225. /**
  226. * @category Extract
  227. * @since 2.6.2
  228. */
  229. export declare const extract: <A>(wa: Tree<A>) => A
  230. /**
  231. * @category traversing
  232. * @since 2.6.3
  233. */
  234. export declare const traverse: PipeableTraverse1<URI>
  235. /**
  236. * @category traversing
  237. * @since 2.6.3
  238. */
  239. export declare const sequence: Traversable1<URI>['sequence']
  240. /**
  241. * @category constructors
  242. * @since 2.7.0
  243. */
  244. export declare const of: <A>(a: A) => Tree<A>
  245. /**
  246. * @category type lambdas
  247. * @since 2.0.0
  248. */
  249. export declare const URI = 'Tree'
  250. /**
  251. * @category type lambdas
  252. * @since 2.0.0
  253. */
  254. export declare type URI = typeof URI
  255. declare module './HKT' {
  256. interface URItoKind<A> {
  257. readonly [URI]: Tree<A>
  258. }
  259. }
  260. /**
  261. * @category instances
  262. * @since 2.7.0
  263. */
  264. export declare const Functor: Functor1<URI>
  265. /**
  266. * @category mapping
  267. * @since 2.10.0
  268. */
  269. export declare const flap: <A>(a: A) => <B>(fab: Tree<(a: A) => B>) => Tree<B>
  270. /**
  271. * @category instances
  272. * @since 2.10.0
  273. */
  274. export declare const Pointed: Pointed1<URI>
  275. /**
  276. * @category instances
  277. * @since 2.10.0
  278. */
  279. export declare const Apply: Apply1<URI>
  280. /**
  281. * Combine two effectful actions, keeping only the result of the first.
  282. *
  283. * @since 2.0.0
  284. */
  285. export declare const apFirst: <B>(second: Tree<B>) => <A>(first: Tree<A>) => Tree<A>
  286. /**
  287. * Combine two effectful actions, keeping only the result of the second.
  288. *
  289. * @since 2.0.0
  290. */
  291. export declare const apSecond: <B>(second: Tree<B>) => <A>(first: Tree<A>) => Tree<B>
  292. /**
  293. * @category instances
  294. * @since 2.7.0
  295. */
  296. export declare const Applicative: Applicative1<URI>
  297. /**
  298. * @category instances
  299. * @since 2.10.0
  300. */
  301. export declare const Chain: Chain1<URI>
  302. /**
  303. * @category instances
  304. * @since 2.7.0
  305. */
  306. export declare const Monad: Monad1<URI>
  307. /**
  308. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  309. * keeping only the result of the first.
  310. *
  311. * @since 2.0.0
  312. */
  313. export declare const chainFirst: <A, B>(f: (a: A) => Tree<B>) => (first: Tree<A>) => Tree<A>
  314. /**
  315. * @category instances
  316. * @since 2.7.0
  317. */
  318. export declare const Foldable: Foldable1<URI>
  319. /**
  320. * @category instances
  321. * @since 2.7.0
  322. */
  323. export declare const Traversable: Traversable1<URI>
  324. /**
  325. * @category instances
  326. * @since 2.7.0
  327. */
  328. export declare const Comonad: Comonad1<URI>
  329. /**
  330. * @category do notation
  331. * @since 2.9.0
  332. */
  333. export declare const Do: Tree<{}>
  334. /**
  335. * @category do notation
  336. * @since 2.8.0
  337. */
  338. export declare const bindTo: <N extends string>(name: N) => <A>(fa: Tree<A>) => Tree<{ readonly [K in N]: A }>
  339. declare const let_: <N extends string, A, B>(
  340. name: Exclude<N, keyof A>,
  341. f: (a: A) => B
  342. ) => (fa: Tree<A>) => Tree<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  343. export {
  344. /**
  345. * @category do notation
  346. * @since 2.13.0
  347. */
  348. let_ as let
  349. }
  350. /**
  351. * @category do notation
  352. * @since 2.8.0
  353. */
  354. export declare const bind: <N extends string, A, B>(
  355. name: Exclude<N, keyof A>,
  356. f: (a: A) => Tree<B>
  357. ) => (ma: Tree<A>) => Tree<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  358. /**
  359. * @category do notation
  360. * @since 2.8.0
  361. */
  362. export declare const apS: <N extends string, A, B>(
  363. name: Exclude<N, keyof A>,
  364. fb: Tree<B>
  365. ) => (fa: Tree<A>) => Tree<{ readonly [K in N | keyof A]: K extends keyof A ? A[K] : B }>
  366. /**
  367. * @since 2.0.0
  368. */
  369. export declare function elem<A>(E: Eq<A>): (a: A, fa: Tree<A>) => boolean
  370. /**
  371. * @since 2.11.0
  372. */
  373. export declare const exists: <A>(predicate: Predicate<A>) => (ma: Tree<A>) => boolean
  374. /**
  375. * This instance is deprecated, use small, specific instances instead.
  376. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.tree`
  377. * (where `T` is from `import T from 'fp-ts/Tree'`)
  378. *
  379. * @category zone of death
  380. * @since 2.0.0
  381. * @deprecated
  382. */
  383. export declare const tree: Monad1<URI> & Foldable1<URI> & Traversable1<URI> & Comonad1<URI>