mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-10-25 18:54:52 +00:00 
			
		
		
		
	Add relation types
This commit is contained in:
		
							parent
							
								
									6ef83d9c59
								
							
						
					
					
						commit
						78ec06bda3
					
				
					 2 changed files with 6 additions and 1 deletions
				
			
		|  | @ -1,3 +1,5 @@ | ||||||
|  | import { EndoRelation } from "./relation"; | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Count the number of elements that satisfy the predicate |  * Count the number of elements that satisfy the predicate | ||||||
|  */ |  */ | ||||||
|  | @ -61,7 +63,7 @@ export function maximum(xs: number[]): number { | ||||||
|  * Splits an array based on the equivalence relation. |  * Splits an array based on the equivalence relation. | ||||||
|  * The concatenation of the result is equal to the argument. |  * The concatenation of the result is equal to the argument. | ||||||
|  */ |  */ | ||||||
| export function groupBy<T>(f: (x: T, y: T) => boolean, xs: T[]): T[][] { | export function groupBy<T>(f: EndoRelation<T>, xs: T[]): T[][] { | ||||||
| 	const groups = [] as T[][]; | 	const groups = [] as T[][]; | ||||||
| 	for (const x of xs) { | 	for (const x of xs) { | ||||||
| 		if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) { | 		if (groups.length !== 0 && f(groups[groups.length - 1][0], x)) { | ||||||
|  |  | ||||||
							
								
								
									
										3
									
								
								src/prelude/relation.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/prelude/relation.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,3 @@ | ||||||
|  | export type Relation<T, U> = (x: T, y: U) => boolean; | ||||||
|  | 
 | ||||||
|  | export type EndoRelation<T> = Relation<T, T>; | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue