/src/methods.ts

https://github.com/funkia/list · TypeScript · 414 lines · 344 code · 70 blank · 0 comment · 11 complexity · 1e8f872f11e740b7bc3a6b9af5f3bb58 MD5 · raw file

  1. import { List, Comparable, Ordering, Applicative, Of } from "./fantasy-land";
  2. import * as L from "./fantasy-land";
  3. export * from "./index";
  4. declare module "./index" {
  5. interface List<A> {
  6. empty(): List<any>;
  7. of<B>(b: B): List<B>;
  8. append(value: A): List<A>;
  9. nth(index: number): A | undefined;
  10. prepend(value: A): List<A>;
  11. append(value: A): List<A>;
  12. intersperse(separator: A): List<A>;
  13. first(): A | undefined;
  14. head(): A | undefined;
  15. last(): A | undefined;
  16. map<B>(f: (a: A) => B): List<B>;
  17. pluck<K extends keyof A>(key: K): List<A[K]>;
  18. foldl<B>(f: (acc: B, value: A) => B, initial: B): B;
  19. reduce<B>(f: (acc: B, value: A) => B, initial: B): B;
  20. scan<B>(f: (acc: B, value: A) => B, initial: B): List<B>;
  21. foldr<B>(f: (value: A, acc: B) => B, initial: B): B;
  22. reduceRight<B>(f: (value: A, acc: B) => B, initial: B): B;
  23. foldlWhile<B>(
  24. predicate: (acc: B, value: A) => boolean,
  25. f: (value: A, acc: B) => B,
  26. initial: B
  27. ): B;
  28. reduceWhile<B>(
  29. predicate: (acc: B, value: A) => boolean,
  30. f: (value: A, acc: B) => B,
  31. initial: B
  32. ): B;
  33. traverse<A, B>(of: Of, f: (a: A) => Applicative<B>): any;
  34. sequence<A, B>(this: List<Applicative<A>>, of: Of): any;
  35. forEach(callback: (a: A) => void): void;
  36. filter(predicate: (a: A) => boolean): List<A>;
  37. filter<B extends A>(predicate: (a: A) => a is B): List<B>;
  38. reject(predicate: (a: A) => boolean): List<A>;
  39. partition(predicate: (a: A) => boolean): [List<A>, List<A>];
  40. join(separator: string): string;
  41. ap<B>(listF: List<(a: A) => B>): List<B>;
  42. flatten(this: List<List<A>>): List<A>;
  43. flatMap<B>(f: (a: A) => List<B>): List<B>;
  44. chain<B>(f: (a: A) => List<B>): List<B>;
  45. every(predicate: (a: A) => boolean): boolean;
  46. some(predicate: (a: A) => boolean): boolean;
  47. none(predicate: (a: A) => boolean): boolean;
  48. indexOf(element: A): number;
  49. lastIndexOf(element: A): number;
  50. find(predicate: (a: A) => boolean): A | undefined;
  51. findLast(predicate: (a: A) => boolean): A | undefined;
  52. findIndex(predicate: (a: A) => boolean): number;
  53. includes(element: A): boolean;
  54. equals(secondList: List<any>): boolean;
  55. equalsWith(f: (a: A, b: A) => boolean, secondList: List<any>): boolean;
  56. concat(right: List<A>): List<A>;
  57. update(index: number, a: A): List<A>;
  58. adjust(index: number, f: (a: A) => A): List<A>;
  59. slice(from: number, to: number): List<A>;
  60. take(n: number): List<A>;
  61. takeWhile(predicate: (a: A) => boolean): List<A>;
  62. takeLastWhile(predicate: (a: A) => boolean): List<A>;
  63. takeLast(n: number): List<A>;
  64. splitAt(index: number): [List<A>, List<A>];
  65. splitWhen(predicate: (a: A) => boolean): [List<A>, List<A>];
  66. splitEvery(size: number): List<List<A>>;
  67. remove(from: number, amount: number): List<A>;
  68. drop(n: number): List<A>;
  69. dropWhile(predicate: (a: A) => boolean): List<A>;
  70. dropRepeats(): List<A>;
  71. dropRepeatsWith(predicate: (a: A, b: A) => boolean): List<A>;
  72. dropLast(n: number): List<A>;
  73. pop(): List<A>;
  74. tail(): List<A>;
  75. toArray(): A[];
  76. insert(index: number, element: A): List<A>;
  77. insertAll(index: number, elements: List<A>): List<A>;
  78. reverse(): List<A>;
  79. backwards(): Iterable<A>;
  80. zipWith<B, C>(f: (a: A, b: B) => C, bs: List<B>): List<C>;
  81. zip<B>(bs: List<B>): List<[A, B]>;
  82. sort<A extends Comparable>(this: List<A>, l: List<A>): List<A>;
  83. sortBy<B extends Comparable>(f: (a: A) => B): List<A>;
  84. sortWith(comparator: (a: A, b: A) => Ordering): List<A>;
  85. group(): List<List<A>>;
  86. groupWith<A>(f: (a: A, b: A) => boolean): List<List<A>>;
  87. isEmpty(): boolean;
  88. }
  89. }
  90. List.prototype.append = function<A>(value: A): List<A> {
  91. return L.append(value, this);
  92. };
  93. List.prototype.intersperse = function<A>(separator: A): List<A> {
  94. return L.intersperse(separator, this);
  95. };
  96. List.prototype.nth = function<A>(index: number): A | undefined {
  97. return L.nth(index, this);
  98. };
  99. List.prototype.empty = function(): List<any> {
  100. return L.empty();
  101. };
  102. List.prototype.of = function<B>(b: B): List<B> {
  103. return L.of(b);
  104. };
  105. List.prototype.prepend = function<A>(value: A): List<A> {
  106. return L.prepend(value, this);
  107. };
  108. List.prototype.append = function<A>(value: A): List<A> {
  109. return L.append(value, this);
  110. };
  111. List.prototype.first = function<A>(): A | undefined {
  112. return L.first(this);
  113. };
  114. List.prototype.head = List.prototype.first;
  115. List.prototype.last = function<A>(): A | undefined {
  116. return L.last(this);
  117. };
  118. List.prototype.map = function<A, B>(f: (a: A) => B): List<B> {
  119. return L.map(f, this);
  120. };
  121. List.prototype.pluck = function<A, K extends keyof A>(
  122. this: List<A>,
  123. key: K
  124. ): List<A[K]> {
  125. return L.pluck(key, this);
  126. } as any;
  127. List.prototype.foldl = function foldl<A, B>(
  128. f: (acc: B, value: A) => B,
  129. initial: B
  130. ): B {
  131. return L.foldl(f, initial, this);
  132. };
  133. List.prototype.reduce = List.prototype.foldl;
  134. List.prototype.scan = function scan<A, B>(
  135. f: (acc: B, value: A) => B,
  136. initial: B
  137. ): List<B> {
  138. return L.scan(f, initial, this);
  139. };
  140. List.prototype.foldr = function<A, B>(
  141. f: (value: A, acc: B) => B,
  142. initial: B
  143. ): B {
  144. return L.foldr(f, initial, this);
  145. };
  146. List.prototype.reduceRight = List.prototype.foldr;
  147. List.prototype.foldlWhile = function foldlWhile<A, B>(
  148. predicate: (acc: B, value: A) => boolean,
  149. f: (acc: B, value: A) => B,
  150. initial: B
  151. ): B {
  152. return L.foldlWhile(predicate, f, initial, this);
  153. };
  154. List.prototype.reduceWhile = List.prototype.foldlWhile;
  155. List.prototype.traverse = function<A, B>(
  156. of: Of,
  157. f: (a: A) => Applicative<B>
  158. ): any {
  159. return L.traverse(of, f, this);
  160. };
  161. List.prototype.sequence = function<A>(this: List<Applicative<A>>, of: Of): any {
  162. return L.sequence(of, this);
  163. };
  164. List.prototype.forEach = function<A>(callback: (a: A) => void): void {
  165. return L.forEach(callback, this);
  166. };
  167. List.prototype.filter = function<A>(predicate: (a: A) => boolean): List<A> {
  168. return L.filter(predicate, this);
  169. };
  170. List.prototype.reject = function<A>(predicate: (a: A) => boolean): List<A> {
  171. return L.reject(predicate, this);
  172. };
  173. List.prototype.partition = function<A>(
  174. predicate: (a: A) => boolean
  175. ): [List<A>, List<A>] {
  176. return L.partition(predicate, this);
  177. };
  178. List.prototype.join = function(separator: string): string {
  179. return L.join(separator, this);
  180. };
  181. List.prototype.ap = function<A, B>(listF: List<(a: A) => B>): List<B> {
  182. return L.ap(listF, this);
  183. };
  184. List.prototype.flatten = function<A>(this: List<List<A>>): List<A> {
  185. return L.flatten(this);
  186. };
  187. List.prototype.flatMap = function<A, B>(f: (a: A) => List<B>): List<B> {
  188. return L.flatMap(f, this);
  189. };
  190. List.prototype.chain = List.prototype.flatMap;
  191. List.prototype.every = function<A>(predicate: (a: A) => boolean): boolean {
  192. return L.every(predicate, this);
  193. };
  194. List.prototype.some = function<A>(predicate: (a: A) => boolean): boolean {
  195. return L.some(predicate, this);
  196. };
  197. List.prototype.none = function<A>(predicate: (a: A) => boolean): boolean {
  198. return L.none(predicate, this);
  199. };
  200. List.prototype.indexOf = function<A>(element: A): number {
  201. return L.indexOf(element, this);
  202. };
  203. List.prototype.lastIndexOf = function<A>(element: A): number {
  204. return L.lastIndexOf(element, this);
  205. };
  206. List.prototype.find = function find<A>(
  207. predicate: (a: A) => boolean
  208. ): A | undefined {
  209. return L.find(predicate, this);
  210. };
  211. List.prototype.findLast = function findLast<A>(
  212. predicate: (a: A) => boolean
  213. ): A | undefined {
  214. return L.findLast(predicate, this);
  215. };
  216. List.prototype.findIndex = function<A>(predicate: (a: A) => boolean): number {
  217. return L.findIndex(predicate, this);
  218. };
  219. List.prototype.includes = function<A>(element: A): boolean {
  220. return L.includes(element, this);
  221. };
  222. List.prototype.equals = function<A>(secondList: List<A>): boolean {
  223. return L.equals(this, secondList);
  224. };
  225. List.prototype.equalsWith = function<A>(
  226. f: (a: A, b: A) => boolean,
  227. secondList: List<A>
  228. ): boolean {
  229. return L.equalsWith(f, this, secondList);
  230. };
  231. List.prototype.concat = function<A>(right: List<A>): List<A> {
  232. return L.concat(this, right);
  233. };
  234. List.prototype.update = function<A>(index: number, a: A): List<A> {
  235. return L.update(index, a, this);
  236. };
  237. List.prototype.adjust = function<A>(index: number, f: (a: A) => A): List<A> {
  238. return L.adjust(index, f, this);
  239. };
  240. List.prototype.slice = function<A>(from: number, to: number): List<A> {
  241. return L.slice(from, to, this);
  242. };
  243. List.prototype.take = function<A>(n: number): List<A> {
  244. return L.take(n, this);
  245. };
  246. List.prototype.takeWhile = function<A>(predicate: (a: A) => boolean): List<A> {
  247. return L.takeWhile(predicate, this);
  248. };
  249. List.prototype.takeLast = function<A>(n: number): List<A> {
  250. return L.takeLast(n, this);
  251. };
  252. List.prototype.takeLastWhile = function<A>(
  253. predicate: (a: A) => boolean
  254. ): List<A> {
  255. return L.takeLastWhile(predicate, this);
  256. };
  257. List.prototype.splitAt = function<A>(index: number): [List<A>, List<A>] {
  258. return L.splitAt(index, this);
  259. };
  260. List.prototype.splitWhen = function<A>(
  261. predicate: (a: A) => boolean
  262. ): [List<A>, List<A>] {
  263. return L.splitWhen(predicate, this);
  264. };
  265. List.prototype.splitEvery = function<A>(size: number): List<List<A>> {
  266. return L.splitEvery(size, this);
  267. };
  268. List.prototype.remove = function<A>(from: number, amount: number): List<A> {
  269. return L.remove(from, amount, this);
  270. };
  271. List.prototype.drop = function<A>(n: number): List<A> {
  272. return L.drop(n, this);
  273. };
  274. List.prototype.dropWhile = function<A>(predicate: (a: A) => boolean): List<A> {
  275. return L.dropWhile(predicate, this);
  276. };
  277. List.prototype.dropRepeats = function<A>(): List<A> {
  278. return L.dropRepeats(this);
  279. };
  280. List.prototype.dropRepeatsWith = function<A>(
  281. predicate: (a: A, b: A) => boolean
  282. ): List<A> {
  283. return L.dropRepeatsWith(predicate, this);
  284. };
  285. List.prototype.dropLast = function<A>(n: number): List<A> {
  286. return L.dropLast(n, this);
  287. };
  288. List.prototype.pop = function<A>(): List<A> {
  289. return L.pop(this);
  290. };
  291. List.prototype.tail = function<A>(): List<A> {
  292. return L.tail(this);
  293. };
  294. List.prototype.toArray = function<A>(): A[] {
  295. return L.toArray(this);
  296. };
  297. List.prototype.insert = function<A>(index: number, element: A): List<A> {
  298. return L.insert(index, element, this);
  299. };
  300. List.prototype.insertAll = function<A>(
  301. index: number,
  302. elements: List<A>
  303. ): List<A> {
  304. return L.insertAll(index, elements, this);
  305. };
  306. List.prototype.reverse = function<A>(): List<A> {
  307. return L.reverse(this);
  308. };
  309. List.prototype.backwards = function<A>(): Iterable<A> {
  310. return L.backwards(this);
  311. };
  312. List.prototype.zipWith = function<A, B, C>(
  313. f: (a: A, b: B) => C,
  314. bs: List<B>
  315. ): List<C> {
  316. return L.zipWith(f, this, bs);
  317. };
  318. List.prototype.zip = function<A, B>(bs: List<B>): List<[A, B]> {
  319. return L.zip(this, bs);
  320. };
  321. List.prototype.sort = function<A extends Comparable>(): List<A> {
  322. return L.sort(this);
  323. };
  324. List.prototype.sortWith = function<A>(
  325. comparator: (a: A, b: A) => Ordering
  326. ): List<A> {
  327. return L.sortWith(comparator, this);
  328. };
  329. List.prototype.sortBy = function<A, B extends Comparable>(
  330. f: (a: A) => B
  331. ): List<A> {
  332. return L.sortBy(f, this);
  333. };
  334. List.prototype.group = function<A>(): List<List<A>> {
  335. return L.group(this);
  336. };
  337. List.prototype.groupWith = function<A>(
  338. f: (a: A, b: A) => boolean
  339. ): List<List<A>> {
  340. return L.groupWith(f, this);
  341. };
  342. List.prototype.isEmpty = function(): boolean {
  343. return L.isEmpty(this);
  344. };