-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.rkt
98 lines (77 loc) · 2.3 KB
/
array.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#lang racket/base
(require racket/contract
racket/generic
racket/match
racket/struct
(prefix-in base:: racket/base)
(for-syntax racket/base))
(require "../internal/match.rkt")
(require "./show.rkt"
"./json.rkt"
"./eq.rkt"
"./ord.rkt"
"./functor.rkt"
"./applicative.rkt"
"./monad.rkt")
(provide Array
Array?
Array/c
array
list->array
array->list)
(struct InnerArray [ref]
#:transparent
#:methods gen:custom-write
[(define write-proc
(make-constructor-style-printer
(λ (_) 'Array)
(match-lambda
[(InnerArray xs) xs])))]
#:methods gen:Show
[(define show:show generic-fmt-show)]
#:methods gen:ToJSON
[(define/generic self/->json ->json)
(define/match1 ->json
[(InnerArray xs) (self/->json xs)])]
#:methods gen:Eq
[(define/generic self/= eq:=)
(define/match (eq:= a b)
[((InnerArray xs) (InnerArray ys)) (self/= xs ys)])]
#:methods gen:Ord
[(define/generic self/compare ord:compare)
(define/match (ord:compare a b)
[((InnerArray xs) (InnerArray ys)) (self/compare xs ys)])]
#:methods gen:Functor
[(define/generic self/map functor:map)
(define/match (functor:map f b)
[(_ (InnerArray ys)) (InnerArray (self/map f ys))])]
#:methods gen:Applicative
[(define/generic self/ap applicative:ap)
(define/match (applicative:ap a b)
[((InnerArray xs) (InnerArray ys)) (InnerArray (self/ap xs ys))])]
#:methods gen:Monad
[(define/generic self/bind monad:bind)
(define/match (monad:bind a f)
[((InnerArray xs) _)
(base::foldl (λ (x acc)
(match-define (InnerArray acc-) acc)
(match-define (InnerArray ys) (f x))
(InnerArray (base::append acc- ys)))
(InnerArray (list))
xs)])]
#:property prop:sequence
(match-lambda
[(InnerArray xs) xs]))
(define Array? InnerArray?)
(define (Array/c a)
(struct/c InnerArray (listof a)))
(define-match-expander Array
(λ (stx)
(syntax-case stx ()
[(_ pat ...)
#'(InnerArray (list pat ...))])))
(define list->array InnerArray)
(define (array . xs)
(InnerArray (apply list xs)))
(define/match1 array->list
[(InnerArray xs) xs])