J in Racket
Note
This is a work in progress. J support is incomplete at this time. The interfaces here are liable to change substantially.
1 Main
(require j) | package: j |
2 J library
(require j/lib/adverbs) | package: j |
(require j/lib/conjunctions) | package: j |
(require j/lib/nouns) | package: j |
(require j/lib/verbs) | package: j |
3 Numbers
procedure
(string->number/j str) → (or/c number? #f)
str : string?
4 Ranked Apply
4.1 Ranked Values
procedure
(normalized-value? v) → boolean?
v : any/c
an array? with nonzero dimension;
an array? with zero dimension whose element is not a normalized-value?;
an exact nonnegative integer; or
procedure
v : any/c
> (->array '(1 2 3)) (array #[1 2 3])
> (->array (vector 5)) (mutable-array #[5])
> (->array "hello") (array #[#\h #\e #\l #\l #\o])
> (->array 'sym) (array 'sym)
> (->array (array #[#[0 1] #[2 3]])) (array #[#[0 1] #[2 3]])
> (->array (array "atomic string")) (array "atomic string")
procedure
(sequence->array s) → array?
s : sequence?
procedure
v : any/c
procedure
(shape v) → (vectorof exact-nonnegative-integer?)
v : any/c
procedure
(ranked-value atom/c) → flat-contract?
atom/c : flat-contract?
4.2 Items
procedure
v : any/c
> (item-count 3) 1
> (item-count '(3)) 1
> (item-count (index-array #[2 3 4])) 2
> (item-count (vector)) 0
procedure
v : any/c pos : (ranked-value exact-integer?)
procedure
v : any/c
procedure
(take-items v pos) → any/c
v : any/c pos : (ranked-value (or/c exact-integer? infinite?))
If more items are requested than are contained in v, the extra items will be filled with (current-fill).
procedure
(tail-items v) → any/c
v : any/c
procedure
(drop-items v pos) → any/c
v : any/c pos : (ranked-value (or/c exact-integer? infinite?))
procedure
(reshape-items v new-shape) → any/c
v : any/c new-shape : (ranked-value exact-nonnegative-integer?)
> (sequence->list (in-items 3)) '(3)
> (sequence->list (in-items '(3))) '(3)
> (sequence->list (in-items (index-array #[2 3 4])))
(list
(array #[#[0 1 2 3] #[4 5 6 7] #[8 9 10 11]])
(array #[#[12 13 14 15] #[16 17 18 19] #[20 21 22 23]]))
> (sequence->list (in-items (vector))) '()
4.3 Ranked Procedures
A ranked procedure implicitly iterates over its arguments according to its rank.
> (apply/rank + (list '(100 200) (index-array '#[2 3]))) (array #[#[100 101 102] #[203 204 205]])
> (define isnum? (atomic-procedure->ranked-procedure char-numeric?)) > (procedure-rank isnum? 1) '(0)
> (isnum? #\4) #t
> (isnum? "abc123") (array #[#f #f #f #t #t #t])
> (define/rank (sum-rows [y 1]) (if (atom? y) y (apply + (array->list y)))) > (sum-rows (array #[#[1 3 5] #[2 4 6]])) (array #[9 12])
> (sum-rows '(5 10 15)) 30
> (sum-rows 11) 11
> (define/rank (replace-items [x #f] [y -1]) x) > (replace-items '(one two) (array #[1 2 3])) (array #[#['one 'two] #['one 'two] #['one 'two]])
> (define/rank (integers [y 1]) (index-array (array->vector (->array y)))) > (apply/rank integers (list (array #[#[2 2] #[3 3]])) #:fill 9) (array #[#[#[0 1 9] #[2 3 9] #[9 9 9]] #[#[0 1 2] #[3 4 5] #[6 7 8]]])
value
value
procedure
(ranked-procedure? v) → boolean?
v : any/c
parameter
(current-fill) → any/c
(current-fill fill) → void? fill : any/c
= #f
procedure
(apply/rank proc v ... lst [#:fill fill]) → normalized-value?
proc : (or/c procedure? ranked-procedure?) v : any/c lst : list? fill : any/c = (current-fill)
If the results of proc have different shapes, then they will be padded with fill.
procedure
(make-ranked-procedure proc rank-spec) → ranked-procedure?
proc : rankable-procedure/c
rank-spec :
(or/c (listof procedure-rank/c) (-> exact-positive-integer? procedure-rank/c))
If rank-spec is a list, it must contain as many items as proc has arities, with each item being the same length as the number of arguments accepted. Otherwise, rank-spec must be a procedure accepting one argument arity, and must return a list of that length.
procedure
(atomic-procedure->ranked-procedure proc) → ranked-procedure?
proc : rankable-procedure/c
procedure
(procedure-rank proc arity) → procedure-rank/c
proc : (or/c procedure? ranked-procedure?) arity : exact-positive-integer?
syntax
(lambda/rank (arg ...) body ...+)
arg = id | [id rank]
syntax
(case-lambda/rank [(arg ...) body ...+] ...)
arg = id | [id rank]
syntax
(define/rank (id arg ...) body ...+)
value
The prop:rank property value must be a procedure which accepts two arguments. The instance is passed as the first argument, and the second is the arity for which a rank is required. The procedure must return a value accepted by procedure-rank/c and of length equal to the arity.