Compare commits
1 Commits
master
...
dev-debug-
Author | SHA1 | Date |
---|---|---|
Matthew Butterick | 90baf7868d | 10 years ago |
@ -1,43 +0,0 @@
|
|||||||
name: CI
|
|
||||||
|
|
||||||
on: [push, pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
run:
|
|
||||||
name: "Build using Racket '${{ matrix.racket-version }}' (${{ matrix.racket-variant }})"
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
racket-version: ["6.6", "6.7", "6.8", "6.9", "6.10.1", "6.11", "6.12", "7.0", "7.1", "7.2", "7.3", "7.4", "7.5", "7.6", "7.7", "7.8", "7.9", "current"]
|
|
||||||
racket-variant: ["BC", "CS"]
|
|
||||||
# CS builds are only provided for versions 7.4 and up so avoid
|
|
||||||
# running the job for prior versions.
|
|
||||||
exclude:
|
|
||||||
- {racket-version: "6.6", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.7", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.8", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.9", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.10.1", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.11", racket-variant: "CS"}
|
|
||||||
- {racket-version: "6.12", racket-variant: "CS"}
|
|
||||||
- {racket-version: "7.0", racket-variant: "CS"}
|
|
||||||
- {racket-version: "7.1", racket-variant: "CS"}
|
|
||||||
- {racket-version: "7.2", racket-variant: "CS"}
|
|
||||||
- {racket-version: "7.3", racket-variant: "CS"}
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@master
|
|
||||||
|
|
||||||
- uses: Bogdanp/setup-racket@v0.11
|
|
||||||
with:
|
|
||||||
distribution: 'full'
|
|
||||||
version: ${{ matrix.racket-version }}
|
|
||||||
variant: ${{ matrix.racket-variant }}
|
|
||||||
|
|
||||||
- name: Install Sugar and its dependencies
|
|
||||||
run: raco pkg install --auto --batch
|
|
||||||
|
|
||||||
- name: Run the tests
|
|
||||||
run: xvfb-run raco test -j 4 -p sugar
|
|
@ -0,0 +1,33 @@
|
|||||||
|
# adapted from
|
||||||
|
# https://github.com/greghendershott/travis-racket/blob/master/.travis.yml
|
||||||
|
# Thanks Greg!
|
||||||
|
|
||||||
|
language: c
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- RACKET_DIR=~/racket
|
||||||
|
matrix:
|
||||||
|
- RACKET_VERSION=6.0
|
||||||
|
- RACKET_VERSION=6.1
|
||||||
|
- RACKET_VERSION=6.2
|
||||||
|
- RACKET_VERSION=HEAD
|
||||||
|
|
||||||
|
#branches:
|
||||||
|
# only:
|
||||||
|
# - master
|
||||||
|
# - typed
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
fast_finish: true
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- git clone https://github.com/mbutterick/travis-racket.git
|
||||||
|
- cat travis-racket/install-racket.sh | bash # pipe to bash not sh!
|
||||||
|
- export PATH="${RACKET_DIR}/bin:${PATH}" #install-racket.sh can't set for us
|
||||||
|
|
||||||
|
script:
|
||||||
|
- cd .. # Travis did a cd into the dir. Back up, for the next:
|
||||||
|
- raco pkg install --deps search-auto --link sugar
|
||||||
|
- raco test -p sugar
|
@ -0,0 +1,165 @@
|
|||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
|
||||||
|
This version of the GNU Lesser General Public License incorporates
|
||||||
|
the terms and conditions of version 3 of the GNU General Public
|
||||||
|
License, supplemented by the additional permissions listed below.
|
||||||
|
|
||||||
|
0. Additional Definitions.
|
||||||
|
|
||||||
|
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||||
|
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||||
|
General Public License.
|
||||||
|
|
||||||
|
"The Library" refers to a covered work governed by this License,
|
||||||
|
other than an Application or a Combined Work as defined below.
|
||||||
|
|
||||||
|
An "Application" is any work that makes use of an interface provided
|
||||||
|
by the Library, but which is not otherwise based on the Library.
|
||||||
|
Defining a subclass of a class defined by the Library is deemed a mode
|
||||||
|
of using an interface provided by the Library.
|
||||||
|
|
||||||
|
A "Combined Work" is a work produced by combining or linking an
|
||||||
|
Application with the Library. The particular version of the Library
|
||||||
|
with which the Combined Work was made is also called the "Linked
|
||||||
|
Version".
|
||||||
|
|
||||||
|
The "Minimal Corresponding Source" for a Combined Work means the
|
||||||
|
Corresponding Source for the Combined Work, excluding any source code
|
||||||
|
for portions of the Combined Work that, considered in isolation, are
|
||||||
|
based on the Application, and not on the Linked Version.
|
||||||
|
|
||||||
|
The "Corresponding Application Code" for a Combined Work means the
|
||||||
|
object code and/or source code for the Application, including any data
|
||||||
|
and utility programs needed for reproducing the Combined Work from the
|
||||||
|
Application, but excluding the System Libraries of the Combined Work.
|
||||||
|
|
||||||
|
1. Exception to Section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
You may convey a covered work under sections 3 and 4 of this License
|
||||||
|
without being bound by section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
2. Conveying Modified Versions.
|
||||||
|
|
||||||
|
If you modify a copy of the Library, and, in your modifications, a
|
||||||
|
facility refers to a function or data to be supplied by an Application
|
||||||
|
that uses the facility (other than as an argument passed when the
|
||||||
|
facility is invoked), then you may convey a copy of the modified
|
||||||
|
version:
|
||||||
|
|
||||||
|
a) under this License, provided that you make a good faith effort to
|
||||||
|
ensure that, in the event an Application does not supply the
|
||||||
|
function or data, the facility still operates, and performs
|
||||||
|
whatever part of its purpose remains meaningful, or
|
||||||
|
|
||||||
|
b) under the GNU GPL, with none of the additional permissions of
|
||||||
|
this License applicable to that copy.
|
||||||
|
|
||||||
|
3. Object Code Incorporating Material from Library Header Files.
|
||||||
|
|
||||||
|
The object code form of an Application may incorporate material from
|
||||||
|
a header file that is part of the Library. You may convey such object
|
||||||
|
code under terms of your choice, provided that, if the incorporated
|
||||||
|
material is not limited to numerical parameters, data structure
|
||||||
|
layouts and accessors, or small macros, inline functions and templates
|
||||||
|
(ten or fewer lines in length), you do both of the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the object code that the
|
||||||
|
Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
4. Combined Works.
|
||||||
|
|
||||||
|
You may convey a Combined Work under terms of your choice that,
|
||||||
|
taken together, effectively do not restrict modification of the
|
||||||
|
portions of the Library contained in the Combined Work and reverse
|
||||||
|
engineering for debugging such modifications, if you also do each of
|
||||||
|
the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the Combined Work that
|
||||||
|
the Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
c) For a Combined Work that displays copyright notices during
|
||||||
|
execution, include the copyright notice for the Library among
|
||||||
|
these notices, as well as a reference directing the user to the
|
||||||
|
copies of the GNU GPL and this license document.
|
||||||
|
|
||||||
|
d) Do one of the following:
|
||||||
|
|
||||||
|
0) Convey the Minimal Corresponding Source under the terms of this
|
||||||
|
License, and the Corresponding Application Code in a form
|
||||||
|
suitable for, and under terms that permit, the user to
|
||||||
|
recombine or relink the Application with a modified version of
|
||||||
|
the Linked Version to produce a modified Combined Work, in the
|
||||||
|
manner specified by section 6 of the GNU GPL for conveying
|
||||||
|
Corresponding Source.
|
||||||
|
|
||||||
|
1) Use a suitable shared library mechanism for linking with the
|
||||||
|
Library. A suitable mechanism is one that (a) uses at run time
|
||||||
|
a copy of the Library already present on the user's computer
|
||||||
|
system, and (b) will operate properly with a modified version
|
||||||
|
of the Library that is interface-compatible with the Linked
|
||||||
|
Version.
|
||||||
|
|
||||||
|
e) Provide Installation Information, but only if you would otherwise
|
||||||
|
be required to provide such information under section 6 of the
|
||||||
|
GNU GPL, and only to the extent that such information is
|
||||||
|
necessary to install and execute a modified version of the
|
||||||
|
Combined Work produced by recombining or relinking the
|
||||||
|
Application with a modified version of the Linked Version. (If
|
||||||
|
you use option 4d0, the Installation Information must accompany
|
||||||
|
the Minimal Corresponding Source and Corresponding Application
|
||||||
|
Code. If you use option 4d1, you must provide the Installation
|
||||||
|
Information in the manner specified by section 6 of the GNU GPL
|
||||||
|
for conveying Corresponding Source.)
|
||||||
|
|
||||||
|
5. Combined Libraries.
|
||||||
|
|
||||||
|
You may place library facilities that are a work based on the
|
||||||
|
Library side by side in a single library together with other library
|
||||||
|
facilities that are not Applications and are not covered by this
|
||||||
|
License, and convey such a combined library under terms of your
|
||||||
|
choice, if you do both of the following:
|
||||||
|
|
||||||
|
a) Accompany the combined library with a copy of the same work based
|
||||||
|
on the Library, uncombined with any other library facilities,
|
||||||
|
conveyed under the terms of this License.
|
||||||
|
|
||||||
|
b) Give prominent notice with the combined library that part of it
|
||||||
|
is a work based on the Library, and explaining where to find the
|
||||||
|
accompanying uncombined form of the same work.
|
||||||
|
|
||||||
|
6. Revised Versions of the GNU Lesser General Public License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the GNU Lesser General Public License from time to time. Such new
|
||||||
|
versions will be similar in spirit to the present version, but may
|
||||||
|
differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Library as you received it specifies that a certain numbered version
|
||||||
|
of the GNU Lesser General Public License "or any later version"
|
||||||
|
applies to it, you have the option of following the terms and
|
||||||
|
conditions either of that published version or of any later version
|
||||||
|
published by the Free Software Foundation. If the Library as you
|
||||||
|
received it does not specify a version number of the GNU Lesser
|
||||||
|
General Public License, you may choose any version of the GNU Lesser
|
||||||
|
General Public License ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Library as you received it specifies that a proxy can decide
|
||||||
|
whether future versions of the GNU Lesser General Public License shall
|
||||||
|
apply, that proxy's public statement of acceptance of any version is
|
||||||
|
permanent authorization for you to choose that version for the
|
||||||
|
Library.
|
@ -1,9 +0,0 @@
|
|||||||
MIT License for `sugar`
|
|
||||||
|
|
||||||
© 2014-2019 Matthew Butterick
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,5 +1,7 @@
|
|||||||
#lang info
|
#lang info
|
||||||
(define collection 'multi)
|
(define collection 'multi)
|
||||||
(define version "0.3")
|
(define deps '("base"
|
||||||
(define deps '("base"))
|
"typed-racket-lib"
|
||||||
(define build-deps '("scribble-lib" "racket-doc" "rackunit-lib"))
|
"typed-racket-more"
|
||||||
|
"rackunit-lib"))
|
||||||
|
(define build-deps '("scribble-lib" "racket-doc" "typed-racket-doc"))
|
||||||
|
@ -1,17 +1,6 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax
|
(require sugar/define)
|
||||||
racket/base
|
(require-via-wormhole "../typed/sugar/cache.rkt")
|
||||||
"private/syntax-utils.rkt")
|
|
||||||
"define.rkt")
|
|
||||||
|
|
||||||
(define+provide+safe (make-caching-proc base-proc)
|
(provide+safe [make-caching-proc (procedure? . -> . procedure?)]
|
||||||
(procedure? . -> . procedure?)
|
define/caching)
|
||||||
(let ([cache (make-hash)])
|
|
||||||
(make-keyword-procedure
|
|
||||||
(λ (kws kw-args . args)
|
|
||||||
(hash-ref! cache (list* kws kw-args args) (λ () (keyword-apply base-proc kws kw-args args)))))))
|
|
||||||
|
|
||||||
(provide+safe define/caching)
|
|
||||||
(define-syntax (define/caching stx)
|
|
||||||
(with-syntax ([(ID LAMBDA-EXPR) (lambdafy stx)])
|
|
||||||
#'(define ID (make-caching-proc LAMBDA-EXPR))))
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
#lang racket/base
|
||||||
|
;; isolate typed requires in a helper file so the untyped versions can be substituted on the untyped side
|
||||||
|
(require net/url)
|
@ -1,4 +1,75 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require "private/syntax-utils.rkt")
|
(require (for-syntax racket/base racket/syntax) sugar/define net/url)
|
||||||
|
(require-via-wormhole "../typed/sugar/coerce.rkt")
|
||||||
|
|
||||||
(require+provide/safe "coerce/base.rkt" "coerce/contract.rkt")
|
(provide+safe [->int (any/c . -> . integer?)]
|
||||||
|
[->string (any/c . -> . string?)]
|
||||||
|
[->symbol (any/c . -> . symbol?)]
|
||||||
|
[->path (any/c . -> . path?)]
|
||||||
|
[->complete-path (any/c . -> . complete-path?)]
|
||||||
|
[->url (any/c . -> . url?)]
|
||||||
|
[->list (any/c . -> . list?)]
|
||||||
|
[->vector (any/c . -> . vector?)]
|
||||||
|
[->boolean (any/c . -> . boolean?)])
|
||||||
|
|
||||||
|
;; coercion contracts & *ish predicates
|
||||||
|
;; only make sense in untyped code
|
||||||
|
;; thus they are here.
|
||||||
|
(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)))))
|
||||||
|
|
||||||
|
(provide+safe make-coercion-contract)
|
||||||
|
(define-syntax (make-coercion-contract stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ stem coerce-proc)
|
||||||
|
(with-syntax ([coerce/stem? (format-id stx "coerce/~a?" #'stem)]
|
||||||
|
[can-be-stem? (format-id stx "can-be-~a?" #'stem)])
|
||||||
|
#'(make-contract
|
||||||
|
#:name 'coerce/stem?
|
||||||
|
#:projection (make-blame-handler coerce-proc 'can-be-stem?)))]
|
||||||
|
[(_ stem)
|
||||||
|
(with-syntax ([->stem (format-id stx "->~a" #'stem)])
|
||||||
|
#'(make-coercion-contract stem ->stem))]))
|
||||||
|
|
||||||
|
(define-syntax (define+provide-coercion-contract stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ stem)
|
||||||
|
(with-syntax ([coerce/stem? (format-id stx "coerce/~a?" #'stem)])
|
||||||
|
#'(begin
|
||||||
|
(provide+safe coerce/stem?)
|
||||||
|
(define coerce/stem? (make-coercion-contract stem))))]))
|
||||||
|
|
||||||
|
(define+provide-coercion-contract int)
|
||||||
|
(define+provide-coercion-contract string)
|
||||||
|
(define+provide-coercion-contract symbol)
|
||||||
|
(define+provide-coercion-contract path)
|
||||||
|
(define+provide-coercion-contract boolean)
|
||||||
|
(define+provide-coercion-contract list)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (make-*ish-predicate stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ stem)
|
||||||
|
(with-syntax ([stemish? (format-id stx "~aish?" #'stem)]
|
||||||
|
[->stem (format-id stx "->~a" #'stem)])
|
||||||
|
#`(begin
|
||||||
|
(provide+safe stemish?)
|
||||||
|
(define (stemish? x)
|
||||||
|
(with-handlers ([exn:fail? (λ(e) #f)]) (and (->stem x) #t)))))]))
|
||||||
|
|
||||||
|
(make-*ish-predicate int)
|
||||||
|
(make-*ish-predicate string)
|
||||||
|
(make-*ish-predicate symbol)
|
||||||
|
(make-*ish-predicate url)
|
||||||
|
(make-*ish-predicate complete-path)
|
||||||
|
(make-*ish-predicate path)
|
||||||
|
(make-*ish-predicate list)
|
||||||
|
(make-*ish-predicate vector)
|
@ -1,123 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax
|
|
||||||
racket/base
|
|
||||||
racket/syntax)
|
|
||||||
racket/stream
|
|
||||||
racket/generic
|
|
||||||
net/url
|
|
||||||
racket/sequence
|
|
||||||
"../unstable/len.rkt"
|
|
||||||
"../define.rkt")
|
|
||||||
|
|
||||||
(module+ safe (require racket/contract))
|
|
||||||
|
|
||||||
(define-syntax-rule (make-coercion-error-handler func funcish val)
|
|
||||||
(λ (exn) (raise-argument-error 'func (symbol->string 'funcish) val)))
|
|
||||||
|
|
||||||
(define (disjoin . preds) (λ (x) (ormap (λ (pred) (pred x)) preds)))
|
|
||||||
(define (conjoin . preds) (λ (x) (andmap (λ (pred) (pred x)) preds)))
|
|
||||||
|
|
||||||
(define-generics+provide+safe stringish
|
|
||||||
(any/c . -> . string?)
|
|
||||||
(->string stringish)
|
|
||||||
#:fast-defaults
|
|
||||||
([string? (define ->string values)]
|
|
||||||
[(disjoin null? void?) (define (->string x) "")]
|
|
||||||
[symbol? (define ->string symbol->string)]
|
|
||||||
[number? (define ->string number->string)]
|
|
||||||
[path? (define ->string path->string)]
|
|
||||||
[(disjoin char? bytes?) (define (->string x) (format "~a" x))]
|
|
||||||
[url? (define ->string url->string)]))
|
|
||||||
|
|
||||||
|
|
||||||
(define (real->int x) (inexact->exact (floor x)))
|
|
||||||
(define (string->int x) (let ([strnum (string->number x)])
|
|
||||||
(unless (real? strnum)
|
|
||||||
(raise-argument-error '->int "eligible string" x))
|
|
||||||
(real->int strnum)))
|
|
||||||
|
|
||||||
(define-generics+provide+safe intish
|
|
||||||
(any/c . -> . integer?)
|
|
||||||
(->int intish)
|
|
||||||
#:fast-defaults
|
|
||||||
([(disjoin integer? real?) (define ->int real->int)]
|
|
||||||
[complex? (define ->int (compose1 real->int real-part))]
|
|
||||||
[string? (define ->int string->int)]
|
|
||||||
[(disjoin symbol? path? bytes?) (define ->int (compose1 string->int ->string))]
|
|
||||||
[char? (define ->int char->integer)]
|
|
||||||
[lengthable? (define (->int x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->int intish? x)])
|
|
||||||
(len x)))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-generics+provide+safe symbolish
|
|
||||||
(any/c . -> . symbol?)
|
|
||||||
(->symbol symbolish)
|
|
||||||
#:fast-defaults
|
|
||||||
([symbol? (define ->symbol values)]
|
|
||||||
[stringish? (define (->symbol x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->symbol symbolish? x)])
|
|
||||||
(string->symbol (->string x))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-generics+provide+safe pathish
|
|
||||||
(any/c . -> . path?)
|
|
||||||
(->path pathish)
|
|
||||||
#:fast-defaults
|
|
||||||
([path? (define ->path values)]
|
|
||||||
[stringish? (define (->path x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->path pathish? x)])
|
|
||||||
(if (url? x)
|
|
||||||
(url->path x)
|
|
||||||
(string->path (->string x)))))]))
|
|
||||||
|
|
||||||
(define-generics+provide+safe urlish
|
|
||||||
(any/c . -> . url?)
|
|
||||||
(->url urlish)
|
|
||||||
#:fast-defaults
|
|
||||||
([url? (define ->url values)]
|
|
||||||
[stringish? (define (->url x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->url urlish? x)])
|
|
||||||
(string->url (->string x))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-generics+provide+safe complete-pathish
|
|
||||||
(any/c . -> . complete-path?)
|
|
||||||
(->complete-path complete-pathish)
|
|
||||||
#:fast-defaults
|
|
||||||
([(conjoin path? complete-path?)
|
|
||||||
;; caution: plain `complete-path?` returns #t for path strings,
|
|
||||||
;; so also check `path?`
|
|
||||||
(define ->complete-path values)]
|
|
||||||
[stringish? (define (->complete-path x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->complete-path complete-pathish? x)])
|
|
||||||
(path->complete-path (->path x))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-generics+provide+safe listish
|
|
||||||
(any/c . -> . list?)
|
|
||||||
(->list listish)
|
|
||||||
#:fast-defaults
|
|
||||||
([list? (define ->list values)]
|
|
||||||
[string? (define ->list list)]
|
|
||||||
[vector? (define ->list vector->list)]
|
|
||||||
[hash? (define ->list hash->list)]
|
|
||||||
[integer? (define ->list list)]
|
|
||||||
[sequence? (define ->list sequence->list)]
|
|
||||||
[stream? (define ->list stream->list)]
|
|
||||||
[(λ (x) #t) (define ->list list)]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-generics+provide+safe vectorish
|
|
||||||
(any/c . -> . vector?)
|
|
||||||
(->vector vectorish)
|
|
||||||
#:fast-defaults
|
|
||||||
([vector? (define ->vector values)]
|
|
||||||
[listish? (define (->vector x)
|
|
||||||
(with-handlers ([exn:fail? (make-coercion-error-handler ->vector vectorish? x)])
|
|
||||||
(list->vector (->list x))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (->boolean x)
|
|
||||||
(any/c . -> . boolean?)
|
|
||||||
(and x #t))
|
|
@ -1,46 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax racket/base racket/syntax) racket/contract "../define.rkt" "base.rkt")
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax-rule (make-blame-handler PROC EXPECTED)
|
|
||||||
(λ (b)
|
|
||||||
(λ (x) (with-handlers ([exn:fail? (λ (exn)
|
|
||||||
(raise-blame-error b x
|
|
||||||
'(expected: "~a" given: "~e")
|
|
||||||
EXPECTED x))])
|
|
||||||
(PROC x)))))
|
|
||||||
|
|
||||||
|
|
||||||
(provide+safe make-coercion-contract)
|
|
||||||
(define-syntax (make-coercion-contract stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ STEM COERCE-PROC)
|
|
||||||
(with-syntax ([COERCE/STEM? (format-id stx "coerce/~a?" #'STEM)]
|
|
||||||
[STEMISH? (format-id stx "~aish?" #'STEM)])
|
|
||||||
#'(make-contract
|
|
||||||
#:name 'COERCE/STEM?
|
|
||||||
#:projection (make-blame-handler COERCE-PROC 'STEMISH?)))]
|
|
||||||
[(MACRO-NAME STEM)
|
|
||||||
(with-syntax ([->STEM (format-id stx "->~a" #'STEM)])
|
|
||||||
#'(MACRO-NAME STEM ->STEM))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (define+provide-coercion-contract stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ STEM)
|
|
||||||
(with-syntax ([COERCE/STEM? (format-id stx "coerce/~a?" #'STEM)])
|
|
||||||
#'(begin
|
|
||||||
(provide+safe COERCE/STEM?)
|
|
||||||
(define COERCE/STEM? (make-coercion-contract STEM))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax-rule (define+provide-coercion-contracts STEM ...)
|
|
||||||
(begin (define+provide-coercion-contract STEM) ...))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide-coercion-contracts int
|
|
||||||
string
|
|
||||||
symbol
|
|
||||||
path
|
|
||||||
boolean
|
|
||||||
list)
|
|
@ -1,100 +1,14 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require racket/list
|
(require sugar/define racket/set sugar/coerce)
|
||||||
racket/match
|
(require-via-wormhole "../typed/sugar/file.rkt")
|
||||||
(except-in racket/path filename-extension)
|
|
||||||
"define.rkt"
|
(provide+safe
|
||||||
"coerce/base.rkt")
|
[get-enclosing-dir (coerce/path? . -> . path?)]
|
||||||
|
[has-ext? (coerce/path? coerce/string? . -> . coerce/boolean?)]
|
||||||
;; this is identical to `filename-extension` in `racket/path`
|
[get-ext (coerce/path? . -> . (or/c #f string?))]
|
||||||
;; but will not treat hidden files as an extension (which is a bug)
|
binary-extensions
|
||||||
(define (filename-extension name)
|
[has-binary-ext? (coerce/path? . -> . coerce/boolean?)]
|
||||||
(match (file-name-from-path name)
|
[add-ext (coerce/string? coerce/string? . -> . coerce/path?)]
|
||||||
[(? path-for-some-system? filename)
|
[remove-ext (coerce/path? . -> . path?)]
|
||||||
(=> resume)
|
[remove-ext* (coerce/path? . -> . path?)])
|
||||||
(match (regexp-match #rx#".[.]([^.]+)$" (path->bytes filename))
|
|
||||||
[(list _ second) second]
|
|
||||||
[_ (resume)])]
|
|
||||||
[_ #false]))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(require (prefix-in rp: racket/path))
|
|
||||||
(check-equal? (rp:filename-extension (string->path ".foo")) #"foo") ; bad behavior
|
|
||||||
(check-false (filename-extension (string->path ".foo")))) ; good behavior
|
|
||||||
|
|
||||||
;; does path have a certain extension, case-insensitively
|
|
||||||
(define+provide+safe (has-ext? x ext)
|
|
||||||
(pathish? stringish? . -> . boolean?)
|
|
||||||
(unless (pathish? x)
|
|
||||||
(raise-argument-error 'has-ext? "pathish?" x))
|
|
||||||
(unless (stringish? ext)
|
|
||||||
(raise-argument-error 'has-ext? "stringish?" ext))
|
|
||||||
(define ext-of-path (filename-extension (->path x)))
|
|
||||||
(and ext-of-path (string=? (string-downcase (bytes->string/utf-8 ext-of-path))
|
|
||||||
(string-downcase (->string ext)))))
|
|
||||||
|
|
||||||
;; get file extension as a string, or return #f
|
|
||||||
;; (consistent with filename-extension behavior)
|
|
||||||
(define+provide+safe (get-ext x)
|
|
||||||
(pathish? . -> . (or/c #f string?))
|
|
||||||
(unless (pathish? x)
|
|
||||||
(raise-argument-error 'get-ext "pathish?" x))
|
|
||||||
(cond
|
|
||||||
[(filename-extension (->path x)) => bytes->string/utf-8]
|
|
||||||
[else #false]))
|
|
||||||
|
|
||||||
;; todo: add extensions
|
|
||||||
(provide+safe binary-extensions)
|
|
||||||
(define binary-extensions
|
|
||||||
(map symbol->string '(gif jpg jpeg mp3 png zip pdf ico tar ai eps exe)))
|
|
||||||
|
|
||||||
(define+provide+safe (has-binary-ext? x)
|
|
||||||
(pathish? . -> . boolean?)
|
|
||||||
(unless (pathish? x)
|
|
||||||
(raise-argument-error 'has-binary-ext? "pathish?" x))
|
|
||||||
(for/or ([ext (in-list binary-extensions)]
|
|
||||||
#:when (has-ext? (->path x) ext))
|
|
||||||
#true))
|
|
||||||
|
|
||||||
;; put extension on path
|
|
||||||
;; use local contract here because this function is used within module
|
|
||||||
(define+provide+safe (add-ext x ext)
|
|
||||||
(stringish? stringish? . -> . pathish?)
|
|
||||||
(unless (stringish? x)
|
|
||||||
(raise-argument-error 'add-ext "stringish?" x))
|
|
||||||
(unless (stringish? ext)
|
|
||||||
(raise-argument-error 'add-ext "stringish?" ext))
|
|
||||||
(->path (string-append (->string x) "." (->string ext))))
|
|
||||||
|
|
||||||
(define (starts-with? str starter)
|
|
||||||
(define pat (regexp (format "^~a" (regexp-quote starter))))
|
|
||||||
(and (regexp-match pat str) #true))
|
|
||||||
|
|
||||||
(define (path-hidden? path)
|
|
||||||
((->string (file-name-from-path path)) . starts-with? . "."))
|
|
||||||
|
|
||||||
(define (change-hide-state new-hide-state path)
|
|
||||||
(define reversed-path-elements (reverse (explode-path path)))
|
|
||||||
(apply build-path (append (reverse (cdr reversed-path-elements))
|
|
||||||
(list (if (eq? new-hide-state 'hide)
|
|
||||||
(format ".~a" (->string (car reversed-path-elements)))
|
|
||||||
(regexp-replace #rx"^." (->string (car reversed-path-elements)) ""))))))
|
|
||||||
|
|
||||||
;; take one extension off path
|
|
||||||
(define+provide+safe (remove-ext x)
|
|
||||||
(pathish? . -> . path?)
|
|
||||||
;; `path-replace-suffix` incorrectly thinks any leading dot counts as a file extension
|
|
||||||
;; when it might be a hidden path.
|
|
||||||
;; so handle hidden paths specially.
|
|
||||||
;; this is fixed in later Racket versions with `path-replace-extension`
|
|
||||||
(match (->path x)
|
|
||||||
[(? path-hidden? path) (change-hide-state 'hide (path-replace-suffix (change-hide-state 'unhide path) ""))]
|
|
||||||
[path (path-replace-suffix path "")]))
|
|
||||||
|
|
||||||
;; take all extensions off path
|
|
||||||
(define+provide+safe (remove-ext* x)
|
|
||||||
(pathish? . -> . path?)
|
|
||||||
(let loop ([path (->path x)])
|
|
||||||
(match (remove-ext path)
|
|
||||||
[(== path) path]
|
|
||||||
[path-reduced (loop path-reduced)])))
|
|
@ -1,10 +1,11 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
|
|
||||||
(require (for-syntax racket/base
|
(require (for-syntax racket/base
|
||||||
syntax/path-spec
|
syntax/path-spec
|
||||||
racket/private/increader
|
racket/private/increader
|
||||||
compiler/cm-accomplice
|
compiler/cm-accomplice
|
||||||
racket/match racket/function)
|
racket/match racket/function)
|
||||||
"../define.rkt")
|
sugar/define)
|
||||||
|
|
||||||
(provide+safe include-without-lang-line)
|
(provide+safe include-without-lang-line)
|
||||||
|
|
@ -1,3 +1,4 @@
|
|||||||
#lang info
|
#lang info
|
||||||
(define scribblings '(("scribblings/sugar.scrbl" ())))
|
(define scribblings '(("scribblings/sugar.scrbl" ())))
|
||||||
(define compile-omit-paths '("test"))
|
|
||||||
|
(define compile-omit-paths '("test"))
|
@ -0,0 +1,5 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require sugar/define racket/set)
|
||||||
|
(require-via-wormhole "../typed/sugar/len.rkt")
|
||||||
|
|
||||||
|
(provide+safe [len ((or/c list? vector? set? sequence? string? symbol? path? hash?) . -> . integer?)])
|
@ -0,0 +1,3 @@
|
|||||||
|
#lang racket/base
|
||||||
|
;; isolate typed requires in a helper file so the untyped versions can be substituted on the untyped side
|
||||||
|
(require (only-in racket/list dropf dropf-right))
|
@ -1,217 +1,40 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax
|
(require (for-syntax racket/base)
|
||||||
racket/base)
|
racket/list racket/set racket/function sugar/define)
|
||||||
racket/list
|
(require "len.rkt" "coerce.rkt")
|
||||||
racket/match
|
|
||||||
racket/function
|
(require-via-wormhole "../typed/sugar/list.rkt")
|
||||||
"define.rkt")
|
|
||||||
|
(define (list-of-lists? xs) (and (list? xs) (andmap list? xs)))
|
||||||
(define (increasing-nonnegative-list? x)
|
(define (index? x) (and (integer? x) (not (negative? x))))
|
||||||
(and (list? x) (or (empty? x) (apply < -1 x))))
|
|
||||||
|
(define increasing-nonnegative? (λ(xs) (apply < -1 xs)))
|
||||||
(define+provide+safe (trimf xs test-proc)
|
(define increasing-nonnegative-list? (and/c list? increasing-nonnegative?))
|
||||||
(list? procedure? . -> . list?)
|
|
||||||
(unless (list? xs)
|
(define (integers? x) (and (list? x) (andmap integer? x)))
|
||||||
(raise-argument-error 'trimf "list?" xs))
|
|
||||||
(dropf-right (dropf xs test-proc) test-proc))
|
(provide+safe [trimf (list? procedure? . -> . list?)]
|
||||||
|
[slicef (list? procedure? . -> . list-of-lists?)]
|
||||||
(define (slicef-and-filter-split-helper xs pred [separate-negated? #f])
|
[slicef-at ((list? procedure?) (boolean?) . ->* . list-of-lists?)]
|
||||||
(let loop ([xs xs][negating? #f][acc empty][negated-acc empty])
|
[slicef-after (list? procedure? . -> . list-of-lists?)]
|
||||||
(match xs
|
[slice-at ((list? (and/c integer? positive?)) (boolean?) . ->* . list-of-lists?)]
|
||||||
[(? empty?) (if separate-negated?
|
[filter-split (list? predicate/c . -> . list-of-lists?)]
|
||||||
(values (reverse acc) (reverse negated-acc))
|
[frequency-hash (list? . -> . hash?)]
|
||||||
(reverse acc))]
|
[members-unique? ((or/c list? vector? string?) . -> . boolean?)]
|
||||||
[(list* (? (if negating? (negate pred) pred) pred-xs) ... other-xs)
|
[members-unique?/error ((or/c list? vector? string?) . -> . boolean?)]
|
||||||
(cond
|
when/splice
|
||||||
[(and negating? separate-negated?)
|
values->list
|
||||||
(loop other-xs
|
[sublist (list? index? index? . -> . list?)]
|
||||||
(not negating?)
|
[break-at (list? (and/c coerce/list? (or/c empty? increasing-nonnegative-list?)) . -> . list-of-lists?)]
|
||||||
acc
|
[shift ((list? integer?) (any/c boolean?) . ->* . list?)]
|
||||||
(match pred-xs
|
[shifts ((list? integers?) (any/c boolean?) . ->* . (listof list?))]
|
||||||
[(? empty?) negated-acc]
|
[shift/values ((list? (or/c integers? integer?)) (any/c boolean?) . ->* . any)])
|
||||||
[_ (cons pred-xs negated-acc)]))]
|
|
||||||
[else
|
|
||||||
(loop other-xs
|
;; todo: can this work in typed context? couldn't figure out how to polymorphically `apply values`
|
||||||
(not negating?)
|
;; macro doesn't work either
|
||||||
(match pred-xs
|
(define (shift/values xs shift-amount-or-amounts [fill-item #f] [cycle #f])
|
||||||
[(? empty?) acc]
|
|
||||||
[_ (cons pred-xs acc)])
|
|
||||||
negated-acc)])])))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (slicef xs pred)
|
|
||||||
(list? procedure? . -> . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'slicef "list?" xs))
|
|
||||||
(slicef-and-filter-split-helper xs pred))
|
|
||||||
|
|
||||||
(define+provide+safe (slicef-at xs pred [force? #f])
|
|
||||||
((list? procedure?) (boolean?) . ->* . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'slicef-at "list?" xs))
|
|
||||||
(unless (procedure? pred)
|
|
||||||
(raise-argument-error 'slicef-at "procedure?" pred))
|
|
||||||
(let loop ([xs xs][acc empty])
|
|
||||||
(match xs
|
|
||||||
[(== empty) (reverse acc)]
|
|
||||||
[(list* (? pred pred-x) (? (negate pred) not-pred-xs) ... tail)
|
|
||||||
(loop tail (cons (cons pred-x not-pred-xs) acc))]
|
|
||||||
[(list* (? (negate pred) not-pred-xs) ... tail)
|
|
||||||
(loop tail (if force? acc (cons not-pred-xs acc)))])))
|
|
||||||
|
|
||||||
(define+provide+safe (slicef-after xs pred [force? #f])
|
|
||||||
((list? procedure?) (boolean?) . ->* . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'slicef-after "list?" xs))
|
|
||||||
(unless (procedure? pred)
|
|
||||||
(raise-argument-error 'slicef-after "procedure?" pred))
|
|
||||||
(let loop ([xs xs][acc empty])
|
|
||||||
(match xs
|
|
||||||
[(== empty) (reverse acc)]
|
|
||||||
[(list* (? (negate pred) not-pred-xs) ... (? pred pred-x) tail)
|
|
||||||
(loop tail (cons (append not-pred-xs (list pred-x)) acc))]
|
|
||||||
[tail (loop empty (if force? acc (cons tail acc)))])))
|
|
||||||
|
|
||||||
(define+provide+safe (slice-at xs len [force? #f])
|
|
||||||
((list? exact-nonnegative-integer?) (boolean?) . ->* . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'slice-at "list?" xs))
|
|
||||||
(unless (and (integer? len) (positive? len))
|
|
||||||
(raise-argument-error 'slice-at "positive integer for sublist length" len))
|
|
||||||
(let loop ([xs xs][slices empty])
|
|
||||||
(if (< (length xs) len)
|
|
||||||
(reverse (if (or force? (empty? xs))
|
|
||||||
slices
|
|
||||||
(cons xs slices)))
|
|
||||||
(match/values (split-at xs len)
|
|
||||||
[(subxs rest) (loop rest (cons subxs slices))]))))
|
|
||||||
|
|
||||||
(define+provide+safe (partition* pred xs)
|
|
||||||
(predicate/c list? . -> . (values list? list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'partition* "list?" xs))
|
|
||||||
(slicef-and-filter-split-helper xs pred 'drop-negated))
|
|
||||||
|
|
||||||
(define+provide+safe (filter-split xs pred)
|
|
||||||
(list? predicate/c . -> . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'filter-split "list?" xs))
|
|
||||||
;; same idea as slicef, but the negated items are dropped
|
|
||||||
(define-values (negated-pred-xs _) (partition* (negate pred) xs))
|
|
||||||
negated-pred-xs)
|
|
||||||
|
|
||||||
(define+provide+safe (frequency-hash xs)
|
|
||||||
(list? . -> . hash?)
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'frequency-hash "list?" xs))
|
|
||||||
(define counter (make-hash))
|
|
||||||
(for ([item (in-list xs)])
|
|
||||||
(hash-update! counter item add1 0))
|
|
||||||
counter)
|
|
||||||
|
|
||||||
(define (->list x)
|
|
||||||
(match x
|
|
||||||
[(? list? x) x]
|
|
||||||
[(? vector?) (vector->list x)]
|
|
||||||
[(? string?) (string->list x)]
|
|
||||||
[else (raise-argument-error '->list "item that can be converted to list" x)]))
|
|
||||||
|
|
||||||
(define+provide+safe (members-unique? x)
|
|
||||||
((or/c list? vector? string?) . -> . boolean?)
|
|
||||||
(match (->list x)
|
|
||||||
[(? list? x) (= (length (remove-duplicates x)) (length x))]
|
|
||||||
[_ (raise-argument-error 'members-unique? "list, vector, or string" x)]))
|
|
||||||
|
|
||||||
(define+provide+safe (members-unique?/error x)
|
|
||||||
((or/c list? vector? string?) . -> . boolean?)
|
|
||||||
(match (members-unique? x)
|
|
||||||
[(== #false)
|
|
||||||
(define duplicate-keys (filter values (hash-map (frequency-hash (->list x))
|
|
||||||
(λ (element freq) (and (> freq 1) element)))))
|
|
||||||
(error (string-append "members-unique? failed because " (if (= (length duplicate-keys) 1)
|
|
||||||
"item isn't"
|
|
||||||
"items aren't") " unique:") duplicate-keys)]
|
|
||||||
[result result]))
|
|
||||||
|
|
||||||
(provide+safe values->list)
|
|
||||||
(define-syntax (values->list stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ VALUES-EXPR) #'(call-with-values (λ () VALUES-EXPR) list)]))
|
|
||||||
|
|
||||||
(define+provide+safe (sublist xs i j)
|
|
||||||
(list? exact-nonnegative-integer? exact-nonnegative-integer? . -> . list?)
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'sublist "list?" xs))
|
|
||||||
(cond
|
|
||||||
[(> j (length xs)) (error 'sublist (format "ending index ~a exceeds length of list" j))]
|
|
||||||
[(>= j i) (for/list ([(x idx) (in-indexed xs)]
|
|
||||||
#:when (<= i idx (sub1 j)))
|
|
||||||
x)]
|
|
||||||
[else (raise-argument-error 'sublist (format "starting index larger than ending index" (list i j)))]))
|
|
||||||
|
|
||||||
(define+provide+safe (break-at xs bps-in)
|
|
||||||
(list? any/c . -> . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'break-at "list" xs))
|
|
||||||
(define bps ((if (list? bps-in) values list) bps-in))
|
|
||||||
(when (ormap (λ (bp) (<= (length xs) bp)) bps)
|
|
||||||
(raise-argument-error 'break-at
|
|
||||||
(format "breakpoints not greater than or equal to input list length = ~a" (length xs)) bps))
|
|
||||||
(unless (increasing-nonnegative-list? bps)
|
|
||||||
(raise-argument-error 'break-at "increasing-nonnegative-list" bps))
|
|
||||||
;; easier to do back to front, because then the list index for each item won't change during the recursion
|
|
||||||
;; cons a zero onto bps (which may already start with zero) and then use that as the terminating condition
|
|
||||||
;; because breaking at zero means we've reached the start of the list
|
|
||||||
(let loop ([xs xs][bps (reverse (cons 0 bps))][acc empty])
|
|
||||||
(match bps
|
|
||||||
[(cons (? zero?) _) (cons xs acc)] ; return whatever's left, because no more splits are possible
|
|
||||||
[_ (match/values (split-at xs (car bps))
|
|
||||||
[(head tail) (loop head (cdr bps) (cons tail acc))])])))
|
|
||||||
|
|
||||||
(define (shift-base xs how-far fill-item cycle caller)
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error caller "list?" xs))
|
|
||||||
(define abs-how-far (if cycle
|
|
||||||
(modulo (abs how-far) (length xs))
|
|
||||||
(abs how-far)))
|
|
||||||
(define (make-fill thing) (if cycle thing (make-list abs-how-far fill-item)))
|
|
||||||
(cond
|
|
||||||
[(> abs-how-far (length xs))
|
|
||||||
(raise-argument-error caller
|
|
||||||
(format "index not larger than list length ~a" (length xs))
|
|
||||||
(* (if (eq? caller 'shift-left) -1 1) how-far))]
|
|
||||||
[(zero? how-far) xs]
|
|
||||||
[(positive? how-far)
|
|
||||||
(match/values (split-at-right xs abs-how-far)
|
|
||||||
[(head tail) (append (make-fill tail) head)])]
|
|
||||||
[else ; how-far is negative
|
|
||||||
(match/values (split-at xs abs-how-far)
|
|
||||||
[(head tail) (append tail (make-fill head))])]))
|
|
||||||
|
|
||||||
(define+provide+safe (shift xs how-far [fill-item #f] [cycle #f])
|
|
||||||
((list? integer?) (any/c boolean?) . ->* . list?)
|
|
||||||
(shift-base xs how-far fill-item cycle 'shift))
|
|
||||||
|
|
||||||
(define+provide+safe (shift-left xs how-far [fill-item #f] [cycle #f])
|
|
||||||
((list? integer?) (any/c boolean?) . ->* . list?)
|
|
||||||
(shift-base xs (- how-far) fill-item cycle 'shift-left))
|
|
||||||
|
|
||||||
(define+provide+safe (shift-cycle xs how-far)
|
|
||||||
(list? integer? . -> . list?)
|
|
||||||
(shift-base xs how-far #false #true 'shift-cycle))
|
|
||||||
|
|
||||||
(define+provide+safe (shift-left-cycle xs how-far)
|
|
||||||
(list? integer? . -> . list?)
|
|
||||||
(shift-base xs (- how-far) #false #true 'shift-left-cycle))
|
|
||||||
|
|
||||||
(define+provide+safe (shifts xs how-fars [fill-item #f] [cycle #f])
|
|
||||||
((list? (listof integer?)) (any/c boolean?) . ->* . (listof list?))
|
|
||||||
(unless (list? xs)
|
|
||||||
(raise-argument-error 'shifts "list?" xs))
|
|
||||||
(map (λ (how-far) (shift xs how-far fill-item cycle)) how-fars))
|
|
||||||
|
|
||||||
(define+provide+safe (shift/values xs shift-amount-or-amounts [fill-item #f] [cycle #f])
|
|
||||||
((list? (or/c (listof integer?) integer?)) (any/c boolean?) . ->* . any)
|
|
||||||
(apply values ((if (list? shift-amount-or-amounts)
|
(apply values ((if (list? shift-amount-or-amounts)
|
||||||
shifts
|
shifts
|
||||||
shift) xs shift-amount-or-amounts fill-item cycle)))
|
shift) xs shift-amount-or-amounts fill-item cycle)))
|
||||||
|
|
||||||
|
@ -1,11 +1,23 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require "private/syntax-utils.rkt")
|
|
||||||
|
|
||||||
(require+provide/safe "cache.rkt"
|
(define-syntax-rule (r+p modname)
|
||||||
"coerce.rkt"
|
(begin
|
||||||
"debug.rkt"
|
(require modname)
|
||||||
"define.rkt"
|
(provide (all-from-out modname))
|
||||||
"file.rkt"
|
(module+ safe
|
||||||
"list.rkt"
|
(require (submod modname safe))
|
||||||
"test.rkt"
|
(provide (all-from-out (submod modname safe))))))
|
||||||
"xml.rkt")
|
|
||||||
|
(r+p "cache.rkt")
|
||||||
|
(r+p "coerce.rkt")
|
||||||
|
(r+p "container.rkt")
|
||||||
|
(r+p "debug.rkt")
|
||||||
|
(r+p "define.rkt")
|
||||||
|
(r+p "file.rkt")
|
||||||
|
(r+p "include.rkt")
|
||||||
|
(r+p "len.rkt")
|
||||||
|
(r+p "list.rkt")
|
||||||
|
(r+p "misc.rkt")
|
||||||
|
(r+p "string.rkt")
|
||||||
|
(r+p "test.rkt")
|
||||||
|
(r+p "xml.rkt")
|
@ -0,0 +1,5 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require sugar/define racket/set sugar/coerce)
|
||||||
|
(require-via-wormhole "../typed/sugar/misc.rkt")
|
||||||
|
|
||||||
|
(provide+safe [bytecount->string (integer? . -> . string?)])
|
@ -1,42 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax racket/base)
|
|
||||||
syntax/define)
|
|
||||||
(provide (except-out (all-defined-out) values->list))
|
|
||||||
|
|
||||||
(define-syntax-rule (require+provide/safe MODNAME ...)
|
|
||||||
(begin
|
|
||||||
(begin
|
|
||||||
(require MODNAME)
|
|
||||||
(provide (all-from-out MODNAME))
|
|
||||||
(module+ safe
|
|
||||||
(require (submod MODNAME safe))
|
|
||||||
(provide (all-from-out (submod MODNAME safe))))) ...))
|
|
||||||
|
|
||||||
(define-syntax (values->list stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ values-expr) #'(call-with-values (λ () values-expr) list)]))
|
|
||||||
|
|
||||||
;; convert calling pattern to form (id contract body-exp)
|
|
||||||
;; hoist contract out of lambda-exp entirely
|
|
||||||
(define (lambdafy-with-contract stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID-EXP CONTRACT LAMBDA-EXP) ; matches exactly three args after `define`
|
|
||||||
;; `normalize-definition` can't handle the acceptable `define/contract` pattern of id, contract, lambda exp after the `define`.
|
|
||||||
;; so extract the contract, and then put id & lambda-exp back together, and let `normalize-definition` destructure as usual.
|
|
||||||
(with-syntax ([(NEW-ID NEW-LAMBDA-EXP)
|
|
||||||
(values->list (normalize-definition #'(_ ID-EXP LAMBDA-EXP) (datum->syntax stx 'λ) #t #t))])
|
|
||||||
#'(NEW-ID CONTRACT NEW-LAMBDA-EXP))]
|
|
||||||
;; matches two or more args (three-arg case handled above)
|
|
||||||
[(_ ID-EXP . BODY)
|
|
||||||
(with-syntax ([(NEW-ID (LAMBDA ARGS CONTRACT . NEW-BODY))
|
|
||||||
(values->list (normalize-definition stx (datum->syntax stx 'λ) #t #t))])
|
|
||||||
;; because the macro provides the `lambda` below, it takes the local srcloc by default
|
|
||||||
;; so `syntax/loc` applies the original srcloc (associated with args and body-exp)
|
|
||||||
#`(NEW-ID CONTRACT #,(syntax/loc #'ID-EXP (LAMBDA ARGS . NEW-BODY))))]
|
|
||||||
;; matches zero or one arguments
|
|
||||||
[_ (raise-syntax-error 'define-macro "not enough arguments")]))
|
|
||||||
|
|
||||||
(define (lambdafy stx)
|
|
||||||
(with-syntax ([(ID LAMBDA-EXP)
|
|
||||||
(values->list (normalize-definition stx (datum->syntax stx 'λ) #true #true))])
|
|
||||||
#'(ID LAMBDA-EXP)))
|
|
@ -0,0 +1,7 @@
|
|||||||
|
#lang racket/base
|
||||||
|
(require sugar/define racket/set sugar/coerce)
|
||||||
|
(require-via-wormhole "../typed/sugar/string.rkt")
|
||||||
|
|
||||||
|
(provide+safe [starts-with? (coerce/string? coerce/string? . -> . coerce/boolean?)]
|
||||||
|
[ends-with? (coerce/string? coerce/string? . -> . coerce/boolean?)]
|
||||||
|
[capitalized? (coerce/string? . -> . coerce/boolean?)])
|
@ -1,44 +1,39 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require (for-syntax
|
(require (for-syntax racket/base racket/syntax))
|
||||||
racket/base
|
(require sugar/define)
|
||||||
racket/syntax
|
(provide+safe module-test-external module-test-internal module-test-internal+external)
|
||||||
syntax/strip-context)
|
|
||||||
"define.rkt")
|
|
||||||
|
|
||||||
(provide+safe module-test-external
|
|
||||||
module-test-internal
|
|
||||||
module-test-internal+external)
|
|
||||||
|
|
||||||
;; tests using module-boundary contracts
|
;; tests using module-boundary contracts
|
||||||
(define-syntax (module-test-external stx)
|
(define-syntax (module-test-external stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
[(_ EXPR ...)
|
[(_ expr ...)
|
||||||
(replace-context
|
(with-syntax ([mod-name (generate-temporary)])
|
||||||
stx
|
#'(begin
|
||||||
(with-syntax ([MOD-NAME (syntax-e (generate-temporary))])
|
(module* mod-name racket/base
|
||||||
#'(begin
|
(require (submod ".."))
|
||||||
(module* MOD-NAME racket/base
|
(require rackunit)
|
||||||
(require (submod ".."))
|
expr ...)
|
||||||
(require rackunit)
|
(module+ test
|
||||||
EXPR ...)
|
(require (submod ".." mod-name)))))]))
|
||||||
(module+ test
|
|
||||||
(require (submod ".." MOD-NAME))))))]))
|
|
||||||
|
|
||||||
(define-syntax (module-test-internal stx)
|
(define-syntax (module-test-internal stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
[(_ EXPR ...)
|
[(_ expr ...)
|
||||||
(replace-context
|
(let ([exprs (syntax->datum #'(expr ...))])
|
||||||
stx
|
(datum->syntax stx `(begin
|
||||||
#'(begin
|
(module+ test
|
||||||
(module+ test
|
(require rackunit)
|
||||||
(require rackunit)
|
,@exprs))
|
||||||
EXPR ...)))]))
|
;; pass original stx for srcloc
|
||||||
|
;; which is not precisely accurate but
|
||||||
|
;; OK for now
|
||||||
|
stx))]))
|
||||||
|
|
||||||
(define-syntax (module-test-internal+external stx)
|
(define-syntax (module-test-internal+external stx)
|
||||||
(syntax-case stx ()
|
(syntax-case stx ()
|
||||||
[(_ EXPR ...)
|
[(_ expr ...)
|
||||||
(replace-context
|
(let ([exprs (syntax->datum #'(expr ...))])
|
||||||
stx
|
(datum->syntax stx `(begin
|
||||||
#'(begin
|
(module-test-internal ,@exprs)
|
||||||
(module-test-internal EXPR ...)
|
(module-test-external ,@exprs)) stx))]))
|
||||||
(module-test-external EXPR ...)))]))
|
|
@ -1,15 +1,10 @@
|
|||||||
#lang sugar/debug racket
|
#lang sugar/debug racket
|
||||||
(require rackunit)
|
(require sugar/debug rackunit)
|
||||||
(let ([out (open-output-string)]
|
(let ([out (open-output-string)])
|
||||||
[let "something else"]
|
|
||||||
[local-require "something else entirely"]
|
|
||||||
[only-in "completely unexpected!"]
|
|
||||||
[report "well, not really"])
|
|
||||||
(parameterize ([current-error-port out])
|
(parameterize ([current-error-port out])
|
||||||
#R5)
|
#^5)
|
||||||
(check-equal? (get-output-string out) "5 = 5\n"))
|
(check-equal? (get-output-string out) "5 = 5\n"))
|
||||||
(let ([out (open-output-string)]
|
(let ([out (open-output-string)])
|
||||||
[report/line "outta the blue!"])
|
|
||||||
(parameterize ([current-error-port out])
|
(parameterize ([current-error-port out])
|
||||||
#RR5)
|
#^^5)
|
||||||
(check-equal? (get-output-string out) "5 = 5 on line 14\n"))
|
(check-equal? (get-output-string out) "5 = 5 on line 9\n"))
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
#lang typed/racket
|
||||||
|
|
||||||
|
(define included-symbol 'bar)
|
@ -1,14 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax racket/base racket/syntax))
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
;; like case but strictly uses `eq?` comparison (as opposed to `equal?`)
|
|
||||||
(define-syntax caseq (make-rename-transformer #'case))
|
|
||||||
(define-syntax casev (make-rename-transformer #'case))
|
|
||||||
|
|
||||||
|
|
||||||
(require sugar/debug)
|
|
||||||
(define-syntax (cond-report stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ [COND . BODY] ... [else . ELSE-BODY]) #'(cond [(report COND) (report (let () (void) . BODY))] ... [else . ELSE-BODY])]
|
|
||||||
[(_ [COND . BODY] ... ) #'(cond-report [COND . BODY] ... [else (void)])]))
|
|
@ -1,95 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax racket/base racket/syntax) racket/class)
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
(define string%
|
|
||||||
(class* object% (writable<%>)
|
|
||||||
(super-new)
|
|
||||||
(init-field [data #f])
|
|
||||||
(define (get-string)
|
|
||||||
(with-handlers ([exn:fail:object? (λ (exn) data)])
|
|
||||||
(send this toString)))
|
|
||||||
(define/public (custom-write port) (write (get-string) port))
|
|
||||||
(define/public (custom-display port) (display (get-string) port))))
|
|
||||||
|
|
||||||
(define mixin-tester%
|
|
||||||
(class object%
|
|
||||||
(super-new)
|
|
||||||
(define/public (addContent val) (make-object string% val))))
|
|
||||||
|
|
||||||
(define-syntax (as-method stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID) (with-syntax ([PRIVATE-ID (generate-temporary #'ID)])
|
|
||||||
#'(begin
|
|
||||||
(public [PRIVATE-ID ID])
|
|
||||||
(define (PRIVATE-ID . args) (apply ID this args))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax-rule (as-methods ID ...)
|
|
||||||
(begin (as-method ID) ...))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (define-instance stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID (MAKER BASE-CLASS . ARGS))
|
|
||||||
(with-syntax ([ID-CLASS (format-id stx "~a:~a" (syntax->datum #'BASE-CLASS) (syntax->datum #'ID))])
|
|
||||||
#'(define ID (let ([ID-CLASS (class BASE-CLASS (super-new))])
|
|
||||||
(MAKER ID-CLASS . ARGS))))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (define-class-predicates stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID)
|
|
||||||
(with-syntax ([+ID (format-id #'ID "+~a" (syntax->datum #'ID))]
|
|
||||||
[ID? (format-id #'ID "~a?" (syntax->datum #'ID))])
|
|
||||||
#'(begin (define (ID? x) (is-a? x ID))
|
|
||||||
(define (+ID . args) (apply make-object ID args))))]))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-subclass*/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) . BODY)
|
|
||||||
(begin
|
|
||||||
(define ID (class* SUPERCLASS INTERFACES (init-field . INIT-ARGS) . BODY))
|
|
||||||
(define-class-predicates ID)))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-subclass/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) . BODY)
|
|
||||||
(define-subclass*/interfaces SUPERCLASS INTERFACES (ID . INIT-ARGS) (super-new) . BODY))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-subclass* SUPERCLASS (ID . INIT-ARGS) . BODY)
|
|
||||||
(define-subclass*/interfaces SUPERCLASS () (ID . INIT-ARGS) . BODY))
|
|
||||||
|
|
||||||
(define-syntax-rule (define-subclass SUPERCLASS (ID . INIT-ARGS) . BODY)
|
|
||||||
(define-subclass* SUPERCLASS (ID . INIT-ARGS) (super-new) . BODY))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax-rule (push-field! FIELD O EXPR)
|
|
||||||
(set-field! FIELD O (cons EXPR (get-field FIELD O))))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax-rule (push-end-field! FIELD O EXPR)
|
|
||||||
(set-field! FIELD O (append (get-field FIELD O) (list EXPR))))
|
|
||||||
|
|
||||||
(define-syntax-rule (pop-field! FIELD O)
|
|
||||||
(let ([xs (get-field FIELD O)])
|
|
||||||
(set-field! FIELD O (cdr xs))
|
|
||||||
(car xs)))
|
|
||||||
|
|
||||||
(define-syntax (increment-field! stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ FIELD O) #'(increment-field! FIELD O 1)]
|
|
||||||
[(_ FIELD O EXPR)
|
|
||||||
#'(begin (set-field! FIELD O (+ (get-field FIELD O) EXPR)) (get-field FIELD O))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (getter-field/override stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ [ID . EXPRS])
|
|
||||||
(syntax-property #'(getter-field [ID . EXPRS]) 'override #t)]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (getter-field stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ [ID . EXPRS])
|
|
||||||
(with-syntax ([_ID (format-id #'ID "_~a" (syntax->datum #'ID))])
|
|
||||||
#`(begin
|
|
||||||
(field [(ID _ID) . EXPRS])
|
|
||||||
(public (_ID ID))
|
|
||||||
(#,(if (syntax-property stx 'override) #'define/override #'define) (_ID) ID)))]))
|
|
@ -1,86 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require "../define.rkt" "../coerce.rkt" "len.rkt" racket/list racket/set racket/sequence racket/stream racket/dict)
|
|
||||||
|
|
||||||
(define (sliceable-container? x)
|
|
||||||
(ormap (λ(proc) (proc x)) (list list? string? symbol? vector? path? (λ(i) (and (not (dict? i)) (sequence? i))))))
|
|
||||||
|
|
||||||
(define (gettable-container? x)
|
|
||||||
(ormap (λ(proc) (proc x)) (list sliceable-container? dict?)))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (get container start [end #f])
|
|
||||||
((gettable-container? any/c) ((or/c (and/c integer? positive?) #f)) . ->* . any)
|
|
||||||
|
|
||||||
(define result
|
|
||||||
;; use handler to capture error & print localized error message
|
|
||||||
(with-handlers ([exn:fail? (λ(exn) (error (format "get: couldn't retrieve ~a from ~a" (if end (format "items ~a through ~a" start end) (format "item ~a" start)) container)))])
|
|
||||||
(let ([end (if (and (equal? end #f) (sliceable-container? container)) (add1 start) end)])
|
|
||||||
(cond
|
|
||||||
[(list? container) (for/list ([i (in-range start end)]) (list-ref container i))]
|
|
||||||
[(vector? container) (for/vector ([i (in-range start end)]) (vector-ref container i))]
|
|
||||||
[(string? container) (substring container start end)]
|
|
||||||
[(symbol? container) (->symbol (get (->string container) start end))]
|
|
||||||
[(path? container) (get (explode-path container) start end)]
|
|
||||||
[(dict? container) (dict-ref container start)]
|
|
||||||
[(sequence? container) (get (->list container) start end)]
|
|
||||||
[else (error)]))))
|
|
||||||
|
|
||||||
;; don't return single-item results inside a list
|
|
||||||
;; check for integer because integers don't have length
|
|
||||||
(if (and (not (integer? result)) (= (len result) 1) (sliceable-container? container))
|
|
||||||
(car (->list result))
|
|
||||||
result))
|
|
||||||
|
|
||||||
(define (listlike-container? container)
|
|
||||||
(ormap (λ(pred) (pred container)) (list vector? set? sequence?)))
|
|
||||||
|
|
||||||
(define+provide+safe (in? item container)
|
|
||||||
(any/c any/c . -> . boolean?)
|
|
||||||
(->boolean (cond
|
|
||||||
[(list? container) (member item container)]
|
|
||||||
[(dict? container) (dict-has-key? container item)]
|
|
||||||
[(path? container) (in? (->path item) (explode-path container))]
|
|
||||||
[(stringish? container) (regexp-match (->string item) (->string container))]
|
|
||||||
;; location relevant because dicts and strings are also listlike (= sequences)
|
|
||||||
[(listlike-container? container) (in? item (->list container))]
|
|
||||||
[else #f])))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(check-equal? (get '(0 1 2 3 4 5) 2) 2)
|
|
||||||
(check-exn exn:fail? (λ() (get '(0 1 2 3 4 5) 100))) ; index too big
|
|
||||||
(check-equal? (get `(0 1 ,(list 2) 3 4 5) 2) (list 2))
|
|
||||||
(check-equal? (get '(0 1 2 3 4 5) 0 2) '(0 1))
|
|
||||||
(check-equal? (get (list->vector '(0 1 2 3 4 5)) 2) 2)
|
|
||||||
(check-equal? (get (list->vector'(0 1 2 3 4 5)) 0 2) (list->vector '(0 1)))
|
|
||||||
(check-equal? (get "purple" 2) "r")
|
|
||||||
(check-equal? (get "purple" 0 2) "pu")
|
|
||||||
(check-equal? (get 'purple 2) 'r)
|
|
||||||
(check-equal? (get 'purple 0 2) 'pu)
|
|
||||||
(check-equal? (get (string->path "/root/foo/bar/file.txt") 2) (string->path "foo"))
|
|
||||||
(check-equal? (get (string->path "/root/foo/bar/file.txt") 0 2) (list (string->path "/") (string->path "root")))
|
|
||||||
(check-equal? (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'a) (list 1))
|
|
||||||
(check-exn exn:fail? (λ() (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'z))) ; nonexistent key
|
|
||||||
|
|
||||||
(check-equal? (get (string->path "/root/foo/bar/file.txt") 1) (string->path "root"))
|
|
||||||
(check-equal? (get (string->path "/root/foo/bar/file.txt") 0 3)
|
|
||||||
(map string->path '("/" "root" "foo")))
|
|
||||||
|
|
||||||
(check-equal? (get (make-hash '((a . 1) (b . 2) (c . 3))) 'b) 2)
|
|
||||||
|
|
||||||
(check-true (2 . in? . '(1 2 3)))
|
|
||||||
(check-false (4 . in? . '(1 2 3)))
|
|
||||||
(check-true (2 . in? . (list->vector '(1 2 3))))
|
|
||||||
(check-false (4 . in? . (list->vector '(1 2 3))))
|
|
||||||
(check-true ('a . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
|
|
||||||
(check-false ('x . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
|
|
||||||
(check-true ("o" . in? . "foobar"))
|
|
||||||
(check-false ("z" . in? . "foobar"))
|
|
||||||
(check-true ('o . in? . 'foobar))
|
|
||||||
(check-false ('z . in? . 'foobar))
|
|
||||||
(check-true ("F" . in? . #\F))
|
|
||||||
|
|
||||||
(check-true (in? "foo" (string->path "/root/foo/bar/file.txt")))
|
|
||||||
(check-false (in? "zam" (string->path "/root/foo/bar/file.txt"))))
|
|
@ -1,25 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require racket/contract racket/class)
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
(define (option/c x) (or/c #f x))
|
|
||||||
|
|
||||||
(module+ main
|
|
||||||
|
|
||||||
(define-syntax-rule (define/public/contract (ID . ARGS) CONTRACT . BODY)
|
|
||||||
(define/public (ID . ARGS)
|
|
||||||
(define/contract (ID . ARGS)
|
|
||||||
CONTRACT . BODY)
|
|
||||||
(ID . ARGS)))
|
|
||||||
|
|
||||||
(define c% (class object%
|
|
||||||
(super-new)
|
|
||||||
|
|
||||||
(define/public/contract (add x y)
|
|
||||||
(integer? integer? . -> . integer?)
|
|
||||||
(+ x y))))
|
|
||||||
|
|
||||||
|
|
||||||
(define c (make-object c%))
|
|
||||||
|
|
||||||
(send c add 12 21))
|
|
@ -1,22 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require sugar/list)
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
(define (assoc? x) (and (pair? x) (not (list? x))))
|
|
||||||
(define (assocs? xs) (and (list? xs) (andmap assoc? xs)))
|
|
||||||
|
|
||||||
(define (listify kvs)
|
|
||||||
(for/list ([slice (in-list (slice-at kvs 2))])
|
|
||||||
(cons (car slice) (cadr slice))))
|
|
||||||
(define-syntax-rule (define-hashifier id hasher) (define (id . kvs) (hasher (listify kvs))))
|
|
||||||
|
|
||||||
;; like indefinite-arity `hash` but mutable
|
|
||||||
(define-hashifier mhash make-hash)
|
|
||||||
(define-hashifier mhasheq make-hasheq)
|
|
||||||
(define-hashifier mhasheqv make-hasheqv)
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(check-equal? (mhash 'k "v") (make-hash (list (cons 'k "v")))))
|
|
||||||
|
|
||||||
(define (dictify . xs) (listify xs))
|
|
@ -1,95 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require racket/class (for-syntax racket/base racket/syntax) racket/dict)
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
|
|
||||||
;; js-style `push`, which appends to end of list
|
|
||||||
(define-syntax-rule (push-end! ID THING)
|
|
||||||
(set! ID (append ID (list THING))))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (increment! stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID) #'(increment! ID 1)]
|
|
||||||
[(_ ID EXPR)
|
|
||||||
#'(begin (set! ID (+ ID EXPR)) ID)]))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(define xs '(1 2 3))
|
|
||||||
(push-end! xs 4)
|
|
||||||
(check-equal? xs '(1 2 3 4)))
|
|
||||||
|
|
||||||
(define-syntax-rule (+= ID THING) (begin (set! ID (+ ID THING)) ID))
|
|
||||||
(define-syntax-rule (++ ID) (+= ID 1))
|
|
||||||
(define-syntax-rule (-- ID) (+= ID -1))
|
|
||||||
(define-syntax-rule (-= ID THING) (+= ID (- THING)))
|
|
||||||
|
|
||||||
|
|
||||||
;; fancy number->string. bounds are checked, inexact integers are coerced.
|
|
||||||
(define (number x #:round [round? #true])
|
|
||||||
(unless (and (number? x) (< -1e21 x 1e21))
|
|
||||||
(raise-argument-error 'number "valid number" x))
|
|
||||||
(let ([x (if round? (/ (round (* x 1e6)) 1e6) x)])
|
|
||||||
(number->string (if (integer? x)
|
|
||||||
(inexact->exact x)
|
|
||||||
x))))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(check-equal? (number 4.5) "4.5")
|
|
||||||
(check-equal? (number 4.0) "4")
|
|
||||||
(check-equal? (number 4) "4")
|
|
||||||
(check-equal? (number -4) "-4"))
|
|
||||||
|
|
||||||
|
|
||||||
(define ·-helper
|
|
||||||
(procedure-rename
|
|
||||||
(λ (x . refs)
|
|
||||||
(for/fold ([x x])
|
|
||||||
([ref (in-list refs)]
|
|
||||||
#:break (not x))
|
|
||||||
(cond
|
|
||||||
;; give `send` precedence (presence of method => wants runtime resolution of value)
|
|
||||||
[(and (object? x)
|
|
||||||
(memq ref (interface->method-names (object-interface x)))) (dynamic-send x ref)]
|
|
||||||
;; dict first, to catch objects that implement gen:dict
|
|
||||||
[(dict? x) (dict-ref x ref #f)]
|
|
||||||
[(object? x) (cond
|
|
||||||
[(memq ref (field-names x)) (dynamic-get-field ref x)]
|
|
||||||
[else #f])]
|
|
||||||
[else (raise-argument-error '· "object or dict" (cons x refs))]))) '·))
|
|
||||||
|
|
||||||
(define-syntax-rule (· X REF ...) (·-helper X 'REF ...))
|
|
||||||
|
|
||||||
#;(module+ test
|
|
||||||
(define c (class object%
|
|
||||||
(super-new)
|
|
||||||
(field [a 42])
|
|
||||||
(define/public (res) (hash 'res (hash 'b 43)))))
|
|
||||||
(define co (make-object c))
|
|
||||||
(define h2 (hash 'a 42 'res co))
|
|
||||||
(check-equal? (· h2 a) 42)
|
|
||||||
(check-equal? (· h2 b) 43)
|
|
||||||
(check-equal? (· co a) 42)
|
|
||||||
(check-equal? (· co b) 43))
|
|
||||||
|
|
||||||
(define-syntax-rule (·map REF XS)
|
|
||||||
(for/list ([x (in-list XS)]) (· x REF)))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(define C
|
|
||||||
(class object%
|
|
||||||
(super-new)
|
|
||||||
(field [foo 'field])
|
|
||||||
(define/public (bar) 'method)
|
|
||||||
(define/public (zam) (hasheq 'zoom 'hash))))
|
|
||||||
(define h (hasheq 'bam (new C) 'foo 'hashlet))
|
|
||||||
(define o (new C))
|
|
||||||
(check-equal? (· o foo) 'field)
|
|
||||||
(check-equal? (· o bar) 'method)
|
|
||||||
(check-equal? (· o zam zoom) 'hash)
|
|
||||||
(check-equal? (· h bam foo) 'field)
|
|
||||||
(check-equal? (· h bam bar) 'method)
|
|
||||||
(check-equal? (· h bam zam zoom) 'hash)
|
|
||||||
(check-equal? (·map foo (list o h)) '(field hashlet)))
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax
|
|
||||||
racket/base
|
|
||||||
racket/syntax)
|
|
||||||
"../define.rkt"
|
|
||||||
racket/sequence
|
|
||||||
racket/generic)
|
|
||||||
|
|
||||||
|
|
||||||
(provide define-generics+provide+safe)
|
|
||||||
(define-syntax (define-generics+provide+safe stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ TYPE ID-CONTRACT (ID . ID-ARGS) . ARGS)
|
|
||||||
(with-syntax ([TYPE? (format-id stx "~a?" #'TYPE)])
|
|
||||||
#'(begin
|
|
||||||
(provide TYPE? ID)
|
|
||||||
(module+ safe
|
|
||||||
(require racket/contract)
|
|
||||||
(provide TYPE? (contract-out [ID ID-CONTRACT])))
|
|
||||||
(define-generics TYPE (ID . ID-ARGS) . ARGS)))]))
|
|
||||||
|
|
||||||
(provide len lengthable?)
|
|
||||||
(define-generics lengthable
|
|
||||||
(len lengthable)
|
|
||||||
#:fast-defaults
|
|
||||||
([list? (define len length)]
|
|
||||||
[string? (define len string-length)]
|
|
||||||
[symbol? (define len (compose1 string-length symbol->string))]
|
|
||||||
[path? (define len (compose1 string-length path->string))]
|
|
||||||
[vector? (define len vector-length)]
|
|
||||||
[hash? (define (len x) (length (hash-keys x)))]
|
|
||||||
[(λ (x) (and (sequence? x) (not (integer? x)))) (define len (compose1 length sequence->list))]))
|
|
||||||
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit racket/set)
|
|
||||||
(check-equal? (len '(1 2 3)) 3)
|
|
||||||
(check-not-equal? (len '(1 2)) 3) ; len 2
|
|
||||||
(check-equal? (len "foo") 3)
|
|
||||||
(check-not-equal? (len "fo") 3) ; len 2
|
|
||||||
(check-equal? (len 'foo) 3)
|
|
||||||
(check-not-equal? (len 'fo) 3) ; len 2
|
|
||||||
(check-equal? (len (list->vector '(1 2 3))) 3)
|
|
||||||
(check-not-equal? (len (list->vector '(1 2))) 3) ; len 2
|
|
||||||
(check-equal? (len (set 1 2 3)) 3)
|
|
||||||
(check-not-equal? (len (set 1 2)) 3) ; len 2
|
|
||||||
(check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3)
|
|
||||||
(check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3)) ; len 2
|
|
@ -1,27 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require racket/port)
|
|
||||||
(provide (all-defined-out) (all-from-out racket/port))
|
|
||||||
|
|
||||||
(define (port-position ip [where #f])
|
|
||||||
(cond
|
|
||||||
[where (file-position ip where)
|
|
||||||
ip]
|
|
||||||
[else (file-position ip)]))
|
|
||||||
|
|
||||||
(define (set-port-position! ip where)
|
|
||||||
(file-position ip where))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(define ip (open-input-bytes (bytes 1 2 3 4)))
|
|
||||||
(port-count-lines! ip)
|
|
||||||
(check-equal? (port-position ip) 0)
|
|
||||||
(check-equal? (read-byte ip) 1)
|
|
||||||
(check-equal? (port-position ip) 1)
|
|
||||||
(check-equal? (read-byte ip) 2)
|
|
||||||
(set-port-position! ip 4)
|
|
||||||
(check-equal? (port-position ip) 4)
|
|
||||||
(check-equal? (read-byte ip) eof)
|
|
||||||
(set-port-position! ip 0)
|
|
||||||
(check-equal? (port-position ip) 0)
|
|
||||||
(check-equal? (read-byte ip) 1))
|
|
@ -1,3 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
|
|
||||||
(define included-symbol 'bar)
|
|
@ -1,37 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require "../define.rkt" "../coerce.rkt")
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (starts-with? str starter)
|
|
||||||
(string? string? . -> . coerce/boolean?)
|
|
||||||
(define pat (regexp (format "^~a" (regexp-quote starter))))
|
|
||||||
(and (regexp-match pat (->string str)) #t))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (ends-with? str ender)
|
|
||||||
(string? string? . -> . coerce/boolean?)
|
|
||||||
(define pat (regexp (format "~a$" (regexp-quote ender))))
|
|
||||||
(and (regexp-match pat (->string str)) #t))
|
|
||||||
|
|
||||||
|
|
||||||
(define+provide+safe (capitalized? str-in)
|
|
||||||
(string? . -> . coerce/boolean?)
|
|
||||||
(define str (->string str-in))
|
|
||||||
(and (positive? (string-length str))
|
|
||||||
(char-upper-case? (car (string->list (car (regexp-match "." str)))))))
|
|
||||||
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(check-true (starts-with? "foobar" "foo"))
|
|
||||||
(check-true (starts-with? "foobar" "foobar"))
|
|
||||||
(check-false (starts-with? "foobar" "zam"))
|
|
||||||
(check-false (starts-with? "foobar" "foobars"))
|
|
||||||
(check-false (starts-with? "foo" "."))
|
|
||||||
(check-true (ends-with? "foobar" "bar"))
|
|
||||||
(check-false (ends-with? "foobar" "zam"))
|
|
||||||
(check-true (ends-with? "foobar" "foobar"))
|
|
||||||
(check-false (ends-with? "foobar" "foobars"))
|
|
||||||
(check-true (capitalized? "Brennan"))
|
|
||||||
(check-false (capitalized? "foobar")))
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
|||||||
#lang racket/base
|
|
||||||
(require (for-syntax racket/base racket/syntax))
|
|
||||||
(provide (all-defined-out))
|
|
||||||
|
|
||||||
(begin-for-syntax
|
|
||||||
(require racket/string racket/format)
|
|
||||||
(define (make-prefix caller-stx)
|
|
||||||
(string-join (map ~a (list (syntax-source caller-stx) (syntax-line caller-stx))) ":" #:after-last ":")))
|
|
||||||
|
|
||||||
(define-syntax (define-stub-stop stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID)
|
|
||||||
(with-syntax ([ERROR-ID (format-id stx "~a~a:not-implemented" (make-prefix stx) (syntax->datum #'ID))])
|
|
||||||
#'(define (ID . args)
|
|
||||||
(error 'ERROR-ID)))]))
|
|
||||||
|
|
||||||
(provide (rename-out [define-stub-stop define-stub]))
|
|
||||||
|
|
||||||
(define-syntax (define-stub-go stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ ID)
|
|
||||||
(with-syntax ([ERROR-ID (format-id stx "~a~a:not-implemented" (make-prefix stx) (syntax->datum #'ID))])
|
|
||||||
#'(define (ID . args)
|
|
||||||
(displayln 'ERROR-ID)))]))
|
|
||||||
|
|
||||||
(define-syntax (define-unfinished stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_ (ID . ARGS) . BODY)
|
|
||||||
(with-syntax ([ID-UNFINISHED (format-id stx "~a~a:unfinished" (make-prefix stx) (syntax->datum #'ID))])
|
|
||||||
#'(define (ID . ARGS)
|
|
||||||
(begin . BODY)
|
|
||||||
(error 'ID-UNFINISHED)))]))
|
|
||||||
|
|
||||||
|
|
||||||
(define-syntax (unfinished stx)
|
|
||||||
(syntax-case stx ()
|
|
||||||
[(_)
|
|
||||||
(with-syntax ([ID-UNFINISHED (format-id stx "~a:~a:~a" (path->string (syntax-source stx)) (syntax-line stx) (syntax->datum #'unfinished))])
|
|
||||||
#'(error 'ID-UNFINISHED))]))
|
|
@ -1,29 +1,12 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require xml
|
(require xml racket/port racket/contract "define.rkt")
|
||||||
racket/port
|
|
||||||
racket/contract
|
|
||||||
"define.rkt")
|
|
||||||
(provide (all-defined-out))
|
(provide (all-defined-out))
|
||||||
|
|
||||||
(define+provide+safe (xml-string->xexprs str)
|
(define+provide+safe (xml-string->xexprs str)
|
||||||
(string? . -> . (values xexpr? xexpr?))
|
(string? . -> . (values xexpr? xexpr?))
|
||||||
(parameterize ([current-input-port (open-input-string str)]
|
(define xml-doc (with-input-from-string str (λ _ (permissive-xexprs #t) (read-xml))))
|
||||||
[permissive-xexprs #true])
|
(values (xml->xexpr (document-prolog xml-doc)) (xml->xexpr (document-element xml-doc))))
|
||||||
(define xml-doc (read-xml))
|
|
||||||
(values (xml->xexpr (document-prolog xml-doc)) (xml->xexpr (document-element xml-doc)))))
|
|
||||||
|
|
||||||
(define+provide+safe (xexprs->xml-string prolog-xexpr root-xexpr)
|
(define+provide+safe (xexprs->xml-string prolog-xexpr root-xexpr)
|
||||||
(xexpr? xexpr? . -> . string?)
|
(xexpr? xexpr? . -> . string?)
|
||||||
(with-output-to-string
|
(with-output-to-string (λ _ (write-xml (document (xexpr->xml prolog-xexpr) (xexpr->xml root-xexpr) null)))))
|
||||||
(λ ()
|
|
||||||
(parameterize ([permissive-xexprs #true])
|
|
||||||
(write-xml (document (xexpr->xml prolog-xexpr) (xexpr->xml root-xexpr) null))))))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(require xml)
|
|
||||||
(define str "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>hello world</root>")
|
|
||||||
(define-values (str-prolog str-doc) (xml-string->xexprs str))
|
|
||||||
(check-equal? str-prolog (prolog (list (p-i (location 1 0 1) (location 1 38 39) 'xml "version=\"1.0\" encoding=\"utf-8\"")) #f null))
|
|
||||||
(check-equal? str-doc '(root () "hello world"))
|
|
||||||
(check-equal? (xexprs->xml-string str-prolog str-doc) str))
|
|
@ -0,0 +1,17 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
|
||||||
|
(define-syntax-rule (r/p name)
|
||||||
|
(begin
|
||||||
|
(require name)
|
||||||
|
(provide (all-from-out name))))
|
||||||
|
|
||||||
|
(r/p "sugar/cache.rkt")
|
||||||
|
(r/p "sugar/coerce.rkt")
|
||||||
|
(r/p "sugar/debug.rkt")
|
||||||
|
(r/p "sugar/define.rkt")
|
||||||
|
(r/p "sugar/file.rkt")
|
||||||
|
(r/p "sugar/len.rkt")
|
||||||
|
(r/p "sugar/list.rkt")
|
||||||
|
(r/p "sugar/misc.rkt")
|
||||||
|
(r/p "sugar/string.rkt")
|
||||||
|
(r/p "sugar/test.rkt")
|
@ -0,0 +1,16 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
(require (for-syntax typed/racket/base) typed/sugar/define)
|
||||||
|
|
||||||
|
(define/typed+provide (make-caching-proc base-proc)
|
||||||
|
(All (A B) (A * -> B) -> (A * -> B))
|
||||||
|
(let ([cache ((inst make-hash (Listof A) B))])
|
||||||
|
(λ args
|
||||||
|
(hash-ref! cache args (λ () (apply base-proc args))))))
|
||||||
|
|
||||||
|
(provide define/caching)
|
||||||
|
(define-syntax (define/caching stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (name arg ... . rest-arg) body ...)
|
||||||
|
#'(define/caching name (λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ name body ...)
|
||||||
|
#'(define name (make-caching-proc body ...))]))
|
@ -0,0 +1,3 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
;; isolate typed requires in a helper file so the untyped versions can be substituted on the untyped side
|
||||||
|
(require typed/net/url)
|
@ -0,0 +1,88 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
(require (for-syntax racket/base racket/syntax))
|
||||||
|
(provide (all-defined-out))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (report stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ expr) #'(report expr expr)]
|
||||||
|
[(_ expr name)
|
||||||
|
#'(let ([expr-result expr])
|
||||||
|
(eprintf "~a = ~v\n" 'name expr-result)
|
||||||
|
expr-result)]))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (report/line stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ expr) #'(report/line expr expr)]
|
||||||
|
[(_ expr name)
|
||||||
|
(with-syntax ([line (syntax-line #'expr)])
|
||||||
|
#'(let ([expr-result expr])
|
||||||
|
(eprintf "~a = ~v on line ~v\n" 'name expr-result line)
|
||||||
|
expr-result))]))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (report/file stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ expr) #'(report/file expr expr)]
|
||||||
|
[(_ expr name)
|
||||||
|
(with-syntax ([file (syntax-source #'expr)]
|
||||||
|
[line (syntax-line #'expr)])
|
||||||
|
#'(let ([expr-result expr])
|
||||||
|
(eprintf "~a = ~v on line ~v in \"~a\"\n" 'name expr-result line file)
|
||||||
|
expr-result))]))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax-rule (define-multi-version multi-name name)
|
||||||
|
(define-syntax-rule (multi-name x (... ...))
|
||||||
|
(begin (name x) (... ...))))
|
||||||
|
|
||||||
|
(define-multi-version report* report)
|
||||||
|
(define-multi-version report*/line report/line)
|
||||||
|
(define-multi-version report*/file report/file)
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax report-apply
|
||||||
|
(syntax-rules ()
|
||||||
|
[(report-apply proc expr)
|
||||||
|
(let ([lst expr])
|
||||||
|
(report (apply proc lst) (apply proc expr))
|
||||||
|
lst)]
|
||||||
|
[(report-apply proc expr #:line)
|
||||||
|
(let ([lst expr])
|
||||||
|
(report (apply proc lst) (apply proc expr) #:line)
|
||||||
|
lst)]))
|
||||||
|
|
||||||
|
#|
|
||||||
|
(define-syntax (verbalize stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ proc args ...)
|
||||||
|
(with-syntax ([proc-input (format-id stx "args to ~a" #'proc)])
|
||||||
|
#'(begin
|
||||||
|
(let () (report (list args ...) proc-input) (void))
|
||||||
|
(report (proc args ...))))]))
|
||||||
|
|#
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax-rule (repeat num expr ...)
|
||||||
|
(for/last ([i (in-range num)])
|
||||||
|
expr ...))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax-rule (time-repeat num expr ...)
|
||||||
|
(time (repeat num expr ...)))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (time-repeat* stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ num expr ...)
|
||||||
|
#'(let ([n num])
|
||||||
|
(values (time-repeat n expr) ...))]))
|
||||||
|
|
||||||
|
|
||||||
|
(define-syntax (compare stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ expr id id-alt ...)
|
||||||
|
#'(values expr (let ([id id-alt]) expr) ...)]))
|
@ -0,0 +1,39 @@
|
|||||||
|
#lang typed/racket/base/no-check
|
||||||
|
;; use of no-check is deliberate here.
|
||||||
|
;; these helper macros don't do any type checking, just rearranging
|
||||||
|
;; they can't be combined with the untyped define macros, however
|
||||||
|
;; because the -> symbol is defined differently here
|
||||||
|
(require (for-syntax typed/racket/base racket/syntax))
|
||||||
|
(provide (all-defined-out))
|
||||||
|
|
||||||
|
(define-syntax (define/typed stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc-name arg ... . rest-arg) type-expr body ...)
|
||||||
|
#'(define/typed proc-name type-expr
|
||||||
|
(λ(arg ... . rest-arg) body ...))]
|
||||||
|
[(_ proc-name type-expr body ...)
|
||||||
|
#'(begin
|
||||||
|
(: proc-name type-expr)
|
||||||
|
(define proc-name body ...))]))
|
||||||
|
|
||||||
|
(define-syntax (define/typed+provide stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ (proc-name arg ... . rest-arg) type-expr body ...)
|
||||||
|
#'(begin
|
||||||
|
(provide proc-name)
|
||||||
|
(define/typed proc-name type-expr
|
||||||
|
(λ(arg ... . rest-arg) body ...)))]
|
||||||
|
[(_ proc-name type-expr body ...)
|
||||||
|
#'(begin
|
||||||
|
(provide proc-name)
|
||||||
|
(begin
|
||||||
|
(: proc-name : type-expr)
|
||||||
|
(define proc-name body ...)))]))
|
||||||
|
|
||||||
|
(define-syntax (define-type+predicate stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ id basetype)
|
||||||
|
(with-syntax ([id? (format-id stx "~a?" #'id)])
|
||||||
|
#'(begin
|
||||||
|
(define-type id basetype)
|
||||||
|
(define-predicate id? id)))]))
|
@ -0,0 +1,59 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
(require typed/sugar/define "coerce.rkt" "string.rkt" racket/path)
|
||||||
|
|
||||||
|
(define/typed+provide (get-enclosing-dir p)
|
||||||
|
(Pathish -> Path)
|
||||||
|
(simplify-path (build-path (->path p) 'up)))
|
||||||
|
|
||||||
|
;; does path have a certain extension
|
||||||
|
(define/typed+provide (has-ext? x ext)
|
||||||
|
(Pathish Stringish -> Boolean)
|
||||||
|
(define ext-of-path (filename-extension (->path x)))
|
||||||
|
(->boolean (and ext-of-path (equal? (string-downcase (bytes->string/utf-8 ext-of-path)) (string-downcase (->string ext))))))
|
||||||
|
|
||||||
|
;; get file extension as a string, or return #f
|
||||||
|
;; (consistent with filename-extension behavior)
|
||||||
|
(define/typed+provide (get-ext x)
|
||||||
|
(Pathish -> (Option String))
|
||||||
|
(let ([fe-result (filename-extension (->path x))])
|
||||||
|
(and fe-result (bytes->string/utf-8 fe-result))))
|
||||||
|
|
||||||
|
|
||||||
|
;; todo: add extensions
|
||||||
|
(define/typed+provide binary-extensions
|
||||||
|
(Listof String)
|
||||||
|
(map symbol->string '(gif jpg jpeg mp3 png zip pdf ico tar ai eps exe)))
|
||||||
|
|
||||||
|
(define/typed+provide (has-binary-ext? x)
|
||||||
|
(Pathish -> Boolean)
|
||||||
|
(let ([x (->path x)])
|
||||||
|
(ormap (λ:([ext : String]) (has-ext? x ext)) binary-extensions)))
|
||||||
|
|
||||||
|
;; put extension on path
|
||||||
|
;; use local contract here because this function is used within module
|
||||||
|
(define/typed+provide (add-ext x ext)
|
||||||
|
(Stringish Stringish -> Path)
|
||||||
|
(->path (string-append (->string x) "." (->string ext))))
|
||||||
|
|
||||||
|
;; take one extension off path
|
||||||
|
(define/typed+provide (remove-ext x)
|
||||||
|
(Pathish -> Path)
|
||||||
|
;; pass through hidden files (those starting with a dot)
|
||||||
|
(let ([x (->path x)])
|
||||||
|
(if (x . starts-with? . ".")
|
||||||
|
x
|
||||||
|
(path-replace-suffix x ""))))
|
||||||
|
|
||||||
|
|
||||||
|
;; take all extensions off path
|
||||||
|
(define/typed+provide (remove-ext* x)
|
||||||
|
(Pathish -> Path)
|
||||||
|
;; pass through hidden files (those starting with a dot)
|
||||||
|
(let ([x (->path x)])
|
||||||
|
(if (x . starts-with? . ".")
|
||||||
|
x
|
||||||
|
(let ([path-with-removed-ext (remove-ext x)])
|
||||||
|
(if (equal? x path-with-removed-ext)
|
||||||
|
x
|
||||||
|
(remove-ext* path-with-removed-ext))))))
|
||||||
|
|
@ -0,0 +1,4 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
;; isolate typed requires in a helper file so the untyped versions can be substituted on the untyped side
|
||||||
|
(require/typed racket/list [dropf (All (A) (Listof A) (A -> Boolean) -> (Listof A))]
|
||||||
|
[dropf-right (All (A) (Listof A) (A -> Boolean) -> (Listof A))])
|
@ -0,0 +1,21 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
(require typed/sugar/define "coerce.rkt")
|
||||||
|
|
||||||
|
(define/typed+provide (starts-with? str starter)
|
||||||
|
(Stringish Stringish -> Boolean)
|
||||||
|
(let ([str (->string str)]
|
||||||
|
[starter (->string starter)])
|
||||||
|
(and (<= (string-length starter) (string-length str))
|
||||||
|
(equal? (substring str 0 (string-length starter)) starter))))
|
||||||
|
|
||||||
|
(define/typed+provide (ends-with? str ender)
|
||||||
|
(Stringish Stringish -> Boolean)
|
||||||
|
(let ([str (->string str)]
|
||||||
|
[ender (->string ender)])
|
||||||
|
(and (<= (string-length ender) (string-length str))
|
||||||
|
(equal? (substring str (- (string-length str) (string-length ender)) (string-length str)) ender))))
|
||||||
|
|
||||||
|
(define/typed+provide (capitalized? str)
|
||||||
|
(Stringish -> Boolean)
|
||||||
|
(let ([str (->string str)])
|
||||||
|
(char-upper-case? (car (string->list str)))))
|
@ -0,0 +1,22 @@
|
|||||||
|
#lang typed/racket/base/no-check
|
||||||
|
(require (for-syntax typed/racket/base) typed/rackunit)
|
||||||
|
|
||||||
|
(provide check-typing-fails check-typing)
|
||||||
|
|
||||||
|
(define-syntax (check-typing-base stx)
|
||||||
|
(syntax-case stx ()
|
||||||
|
[(_ wants-to-fail? expr)
|
||||||
|
(let* ([wants-to-fail? (syntax->datum #'wants-to-fail?)]
|
||||||
|
[λ-arg 'v]
|
||||||
|
[eval-string (if wants-to-fail? `(cons '#%top-interaction ,λ-arg) λ-arg)]
|
||||||
|
[check-string (if wants-to-fail? '(curry check-exn exn:fail:syntax?) 'check-not-exn)])
|
||||||
|
#`(begin
|
||||||
|
(define-namespace-anchor ns)
|
||||||
|
(let ([E (λ(#,λ-arg) (eval #,eval-string (namespace-anchor->namespace ns)))])
|
||||||
|
(apply #,check-string (list (λ _ (call-with-values (λ _ (E 'expr)) (λ vals (car vals)))))))))]))
|
||||||
|
|
||||||
|
(define-syntax-rule (check-typing-fails expr)
|
||||||
|
(check-typing-base #t expr))
|
||||||
|
|
||||||
|
(define-syntax-rule (check-typing expr)
|
||||||
|
(check-typing-base #f expr))
|
@ -0,0 +1,45 @@
|
|||||||
|
#lang typed/racket/base
|
||||||
|
(require racket/port)
|
||||||
|
#;(provide (all-defined-out))
|
||||||
|
#;(require/typed xml [permissive-xexprs (Parameterof Boolean)]
|
||||||
|
[#:struct prolog ([misc : (Listof Misc)][dtd : (Option DTD)][misc2 : (Listof Misc)])]
|
||||||
|
[#:struct document ([prolog : Prolog][element : Element][misc : (Listof Misc)])])
|
||||||
|
|
||||||
|
#|
|
||||||
|
The following grammar describes expressions that create X-expressions:
|
||||||
|
|
||||||
|
xexpr = string
|
||||||
|
| (list symbol (list (list symbol string) ...) xexpr ...)
|
||||||
|
| (cons symbol (list xexpr ...))
|
||||||
|
| symbol
|
||||||
|
| valid-char?
|
||||||
|
| cdata
|
||||||
|
| misc
|
||||||
|
|#
|
||||||
|
|
||||||
|
(define-type Cdata String) ;; could be tighter
|
||||||
|
|
||||||
|
;; valid-char could be tighter
|
||||||
|
#|
|
||||||
|
Returns true if x is an exact-nonnegative-integer whose character interpretation under UTF-8 is from the set ([#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]),
|
||||||
|
|#
|
||||||
|
(define-type Valid-Char Char)
|
||||||
|
(define-type Xexpr (Rec X (U String
|
||||||
|
(List* Symbol (Listof (List Symbol String)) (Listof X))
|
||||||
|
(Pairof Symbol (Listof X))
|
||||||
|
Symbol
|
||||||
|
Valid-Char
|
||||||
|
Cdata)))
|
||||||
|
(define-predicate Xexpr? Xexpr)
|
||||||
|
|
||||||
|
#|
|
||||||
|
(: xml-string->xexprs (String . -> . (values Xexpr Xexpr)))
|
||||||
|
(define (xml-string->xexprs str)
|
||||||
|
(define xml-doc (with-input-from-string str (λ _ (permissive-xexprs #t) (read-xml))))
|
||||||
|
(values (xml->xexpr (document-prolog xml-doc)) (xml->xexpr (document-element xml-doc))))
|
||||||
|
|
||||||
|
|
||||||
|
(define (xexprs->xml-string prolog-xexpr root-xexpr)
|
||||||
|
(xexpr? xexpr? . -> . string?)
|
||||||
|
(with-output-to-string (λ _ (write-xml (document (xexpr->xml prolog-xexpr) (xexpr->xml root-xexpr) null)))))
|
||||||
|
|#
|
Loading…
Reference in New Issue