REPL namespaces; lib function for pattern matching
Adds support for REPL namespacing, primarily to avoid `main` collisions. Also adds a library function for an ergonomic pattern matching function that I've been noodling on. I might explore ways to make list syntax less annoying specifically for pattern matching like this.
This commit is contained in:
		
							
								
								
									
										35
									
								
								lib/patterns.tri
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								lib/patterns.tri
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,35 @@ | ||||
| !import "list.tri" !Local | ||||
|  | ||||
| match_ = y (\self value patterns : | ||||
|   triage | ||||
|     t | ||||
|     (\_ : t) | ||||
|     (\pattern rest : | ||||
|       triage | ||||
|         t | ||||
|         (\_ : t) | ||||
|         (\test result : | ||||
|           if (test value) | ||||
|              (result value) | ||||
|              (self value rest)) | ||||
|         pattern) | ||||
|     patterns) | ||||
|  | ||||
| match = (\value patterns : | ||||
|   match_ value (map (\sublist : | ||||
|     pair (head sublist) (head (tail sublist))) | ||||
|     patterns)) | ||||
|  | ||||
| otherwise = const (t t) | ||||
|  | ||||
| -- matchExample = (\x : match x [[(equal? 1)  (\_ : "one")] | ||||
| --                               [(equal? 2)  (\_ : "two")] | ||||
| --                               [(equal? 3)  (\_ : "three")] | ||||
| --                               [(equal? 4)  (\_ : "four")] | ||||
| --                               [(equal? 5)  (\_ : "five")] | ||||
| --                               [(equal? 6)  (\_ : "six")] | ||||
| --                               [(equal? 7)  (\_ : "seven")] | ||||
| --                               [(equal? 8)  (\_ : "eight")] | ||||
| --                               [(equal? 9)  (\_ : "nine")] | ||||
| --                               [(equal? 10) (\_ : "ten")] | ||||
| --                               [ otherwise  (\_ : "I ran out of fingers!")]]) | ||||
		Reference in New Issue
	
	Block a user