cleanup
parent
05c5b37316
commit
a36a6c7f0d
@ -0,0 +1,4 @@
|
|||||||
|
#lang racket/base
|
||||||
|
|
||||||
|
(require "coercion/values.rkt" "coercion/contracts.rkt")
|
||||||
|
(provide (all-from-out "coercion/values.rkt" "coercion/contracts.rkt"))
|
@ -0,0 +1,46 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require racket/contract "../define/provide.rkt" "values.rkt")
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax-rule (make-blame-handler try-proc expected-sym)
|
||||||
|
(λ(b)
|
||||||
|
(λ(x)
|
||||||
|
(with-handlers ([exn:fail? (λ(e)
|
||||||
|
(raise-blame-error
|
||||||
|
b x
|
||||||
|
'(expected: "~a" given: "~e")
|
||||||
|
expected-sym x))])
|
||||||
|
(try-proc x)))))
|
||||||
|
|
||||||
|
(define+provide coerce/integer?
|
||||||
|
(make-contract
|
||||||
|
#:name 'coerce/integer?
|
||||||
|
#:projection (make-blame-handler ->int 'can-be-integer?)))
|
||||||
|
|
||||||
|
|
||||||
|
(define+provide coerce/string?
|
||||||
|
(make-contract
|
||||||
|
#:name 'coerce/string?
|
||||||
|
#:projection (make-blame-handler ->string 'can-be-string?)))
|
||||||
|
|
||||||
|
|
||||||
|
(define+provide coerce/symbol?
|
||||||
|
(make-contract
|
||||||
|
#:name 'coerce/symbol?
|
||||||
|
#:projection (make-blame-handler ->symbol 'can-be-symbol?)))
|
||||||
|
|
||||||
|
|
||||||
|
(define+provide coerce/path?
|
||||||
|
(make-contract
|
||||||
|
#:name 'coerce/path?
|
||||||
|
#:projection (make-blame-handler ->path 'can-be-path?)))
|
||||||
|
|
||||||
|
|
||||||
|
(define+provide coerce/boolean?
|
||||||
|
(make-contract
|
||||||
|
#:name 'coerce/boolean?
|
||||||
|
#:projection (make-blame-handler ->boolean 'can-be-boolean?)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,40 +1,5 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax racket/base))
|
|
||||||
(require racket/contract)
|
|
||||||
|
|
||||||
(provide define+provide define+provide/contract define/contract+provide)
|
(require "define/provide.rkt" "define/contract.rkt")
|
||||||
|
|
||||||
;; each define macro recursively converts any form of define
|
(provide (all-from-out "define/provide.rkt" "define/contract.rkt"))
|
||||||
;; into its lambda form (define name body ...) and then operates on that.
|
|
||||||
|
|
||||||
(define-syntax (define+provide stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ (proc arg ... . rest-arg) body ...)
|
|
||||||
#'(define+provide proc
|
|
||||||
(λ(arg ... . rest-arg) body ...))]
|
|
||||||
[(_ name body ...)
|
|
||||||
#'(begin
|
|
||||||
(provide name)
|
|
||||||
(define name body ...))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (define+provide/contract stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ (proc arg ... . rest-arg) contract body ...)
|
|
||||||
#'(define+provide/contract proc contract
|
|
||||||
(λ(arg ... . rest-arg) body ...))]
|
|
||||||
[(_ name contract body ...)
|
|
||||||
#'(begin
|
|
||||||
(provide (contract-out [name contract]))
|
|
||||||
(define name body ...))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (define/contract+provide stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ (proc arg ... . rest-arg) contract body ...)
|
|
||||||
#'(define/contract+provide proc contract
|
|
||||||
(λ(arg ... . rest-arg) body ...))]
|
|
||||||
[(_ name contract body ...)
|
|
||||||
#'(begin
|
|
||||||
(provide name)
|
|
||||||
(define/contract name contract body ...))]))
|
|
@ -0,0 +1,41 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require (for-syntax racket/base))
|
||||||
|
(require racket/contract)
|
||||||
|
|
||||||
|
(provide (all-defined-out) (all-from-out racket/contract))
|
||||||
|
|
||||||
|
;; each define macro recursively converts any form of define
|
||||||
|
;; into its lambda form (define name body ...) and then operates on that.
|
||||||
|
|
||||||
|
(define-syntax (define+provide+safe stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc arg ... . rest-arg) contract body ...)
|
||||||
|
#'(define+provide+safe proc contract
|
||||||
|
(λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ name contract body ...)
|
||||||
|
#'(begin
|
||||||
|
(define name body ...)
|
||||||
|
(provide name)
|
||||||
|
(module+ safe
|
||||||
|
(provide (contract-out [name contract]))))]))
|
||||||
|
|
||||||
|
(define-syntax (define+provide/contract stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc arg ... . rest-arg) contract body ...)
|
||||||
|
#'(define+provide/contract proc contract
|
||||||
|
(λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ name contract body ...)
|
||||||
|
#'(begin
|
||||||
|
(provide (contract-out [name contract]))
|
||||||
|
(define name body ...))]))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (define/contract+provide stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc arg ... . rest-arg) contract body ...)
|
||||||
|
#'(define/contract+provide proc contract
|
||||||
|
(λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ name contract body ...)
|
||||||
|
#'(begin
|
||||||
|
(provide name)
|
||||||
|
(define/contract name contract body ...))]))
|
@ -0,0 +1,14 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require (for-syntax racket/base))
|
||||||
|
|
||||||
|
(provide (all-defined-out))
|
||||||
|
|
||||||
|
(define-syntax (define+provide stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc arg ... . rest-arg) body ...)
|
||||||
|
#'(define+provide proc
|
||||||
|
(λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ name body ...)
|
||||||
|
#'(begin
|
||||||
|
(provide name)
|
||||||
|
(define name body ...))]))
|
@ -1,3 +1,4 @@
|
|||||||
#lang info
|
#lang info
|
||||||
(define collection "sugar")
|
(define collection "sugar")
|
||||||
(define scribblings '(("scribblings/sugar.scrbl" ())))
|
(define scribblings '(("scribblings/sugar.scrbl" ())))
|
||||||
|
(define deps '("describe"))
|
@ -1,23 +1,14 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require racket/contract)
|
(require "define/contract.rkt" "coercion.rkt")
|
||||||
(require "coerce.rkt" "container.rkt" "len.rkt" "exception.rkt")
|
|
||||||
|
|
||||||
(provide starts-with? ends-with? stringish?)
|
|
||||||
|
|
||||||
;; stringish: data type that can be trivially converted to string
|
(define+provide/contract (starts-with? str starter)
|
||||||
;; todo: merge this with pathish
|
(string? string? . -> . coerce/boolean?)
|
||||||
(define/contract (stringish? x)
|
(define starter-pattern (regexp (format "^~a" starter)))
|
||||||
(any/c . -> . boolean?)
|
(regexp-match starter-pattern str))
|
||||||
(try (->boolean (->string x)) (except [exn:fail? (λ(e) #f)])))
|
|
||||||
|
|
||||||
;; python-style string testers
|
|
||||||
(define/contract (starts-with? str starter)
|
|
||||||
(stringish? stringish? . -> . boolean?)
|
|
||||||
(let ([str (->string str)]
|
|
||||||
[starter (->string starter)])
|
|
||||||
(and (<= (len starter) (len str)) (equal? (get str 0 (len starter)) starter))))
|
|
||||||
|
|
||||||
|
(define+provide/contract (ends-with? str ender)
|
||||||
(define/contract (ends-with? str ender)
|
(string? string? . -> . coerce/boolean?)
|
||||||
(string? string? . -> . boolean?)
|
(define ender-pattern (regexp (format "~a$" ender)))
|
||||||
(and (<= (len ender) (len str)) (equal? (get str (- (len str) (len ender)) 'end) ender)))
|
(regexp-match ender-pattern str))
|
@ -1,51 +1,31 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require racket/list racket/contract)
|
(require "define/contract.rkt")
|
||||||
(require "define.rkt")
|
|
||||||
|
|
||||||
(module+ test (require rackunit))
|
(module+ test (require rackunit))
|
||||||
|
|
||||||
;; apply filter proc recursively
|
|
||||||
(define+provide/contract (filter-tree proc tree)
|
(define+provide/contract (filter-tree proc tree)
|
||||||
(procedure? list? . -> . list?)
|
(procedure? list? . -> . list?)
|
||||||
(define (remove-empty x)
|
(define (remove-empty x)
|
||||||
(cond
|
(cond
|
||||||
[(list? x) (filter-not empty? (map remove-empty x))]
|
[(list? x) (filter (compose1 not null?) (map remove-empty x))]
|
||||||
[else x]))
|
[else x]))
|
||||||
|
|
||||||
(define (filter-tree-inner proc x)
|
(define (filter-tree-inner proc x)
|
||||||
(cond
|
(cond
|
||||||
[(list? x) (map (λ(i) (filter-tree-inner proc i)) x)]
|
[(list? x) (map (λ(i) (filter-tree-inner proc i)) x)]
|
||||||
[else (if (proc x) x empty)]))
|
[else (if (proc x) x null)]))
|
||||||
|
|
||||||
(remove-empty (filter-tree-inner proc tree)))
|
(remove-empty (filter-tree-inner proc tree)))
|
||||||
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(check-equal? (filter-tree string? '(p)) empty)
|
|
||||||
(check-equal? (filter-tree string? '(p "foo" "bar")) '("foo" "bar"))
|
|
||||||
(check-equal? (filter-tree string? '(p "foo" (p "bar"))) '("foo" ("bar")))
|
|
||||||
(check-equal? (filter-tree (λ(i) (and (string? i) (equal? i "\n"))) '("\n" (foo "bar") "\n")) '("\n" "\n")))
|
|
||||||
|
|
||||||
;; apply filter-not proc recursively
|
|
||||||
(define+provide/contract (filter-not-tree proc tree)
|
(define+provide/contract (filter-not-tree proc tree)
|
||||||
(procedure? list? . -> . list?)
|
(procedure? list? . -> . list?)
|
||||||
(filter-tree (λ(i) (not (proc i))) tree))
|
(filter-tree (λ(i) (not (proc i))) tree))
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(check-equal? (filter-not-tree string? '(p)) '(p))
|
|
||||||
(check-equal? (filter-not-tree string? '(p "foo" "bar")) '(p))
|
|
||||||
(check-equal? (filter-not-tree string? '(p "foo" (p "bar"))) '(p (p)))
|
|
||||||
;(check-equal? (filter-tree (λ(i) (and (tagged-xexpr? i) (equal? 'em (car i)))) '(p "foo" (em "bar"))) '(p "foo"))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
;; todo: doc this function
|
|
||||||
(define+provide/contract (map-tree proc tree)
|
(define+provide/contract (map-tree proc tree)
|
||||||
(procedure? list? . -> . list?)
|
(procedure? list? . -> . list?)
|
||||||
(cond
|
(cond
|
||||||
[(list? tree) (map (λ(i) (map-tree proc i)) tree)]
|
[(list? tree) (map (λ(i) (map-tree proc i)) tree)]
|
||||||
[else (proc tree)]))
|
[else (proc tree)]))
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(check-equal? (map-tree (λ(i) (if (number? i) (* 2 i) i)) '(p 1 2 3 (em 4 5))) '(p 2 4 6 (em 8 10)))
|
|
||||||
(check-equal? (map-tree (λ(i) (if (symbol? i) 'foo i)) '(p 1 2 3 (em 4 5))) '(foo 1 2 3 (foo 4 5))))
|
|
Loading…
Reference in New Issue