{"size": 2502, "ext": "scrbl", "max_stars_repo_path": "lens-doc/unstable/lens/isomorphism.scrbl", "max_stars_repo_name": "AlexKnauth/lens", "max_stars_repo_stars_event_min_datetime": "2015-07-08T12:56:46.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-17T21:21:54.000Z", "max_issues_repo_path": "lens-doc/unstable/lens/isomorphism.scrbl", "max_issues_repo_name": "AlexKnauth/lens", "max_issues_repo_issues_event_min_datetime": "2015-07-07T01:47:59.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-10T18:35:17.000Z", "max_forks_repo_path": "lens-doc/unstable/lens/isomorphism.scrbl", "max_forks_repo_name": "AlexKnauth/lens", "max_forks_repo_forks_event_min_datetime": "2015-07-07T05:01:29.000Z", "max_forks_repo_forks_event_max_datetime": "2019-07-16T00:37:04.000Z"}
{"max_stars_count": 66, "max_issues_count": 199, "max_forks_count": 11, "avg_line_length": 39.7142857143, "max_line_length": 84, "alphanum_fraction": 0.7258193445}
#lang scribble/manual

@(require lens/private/doc-util/main)

@title{Isomorphisms}

@defproc[(make-isomorphism-lens [f (a/c . -> . b/c)] [inv (b/c . -> . a/c)]) lens?]{
Creates a lens for an isomorphism. The @racket[f] argument should be a function
with an inverse, and the @racket[inv] argument should be its inverse.
The @racket[f] function converts targets to views, and the @racket[inv] function
converts views to targets.

So for instance a @racket[symbol->string-lens] could be defined with:
@racketblock[
  (define symbol->string-lens
    (make-isomorphism-lens symbol->string string->symbol))
]
@lens-unstable-examples[
  (lens-view symbol->string-lens 'something)
  (lens-transform symbol->string-lens 'something (λ (s) (string-append "make-" s)))
]}

@defproc[(isomorphism-lens? [v any/c]) boolean?]{
A predicate that returns true when @racket[v] is a lens constructed with
@racket[make-isomorphism-lens], @racket[isomorphism-lens-inverse], or
@racket[make-isomorphism-lenses], and returns false otherwise.
All isomorphism lenses are also lenses according to @racket[lens?].
}

@defproc[(isomorphism-lens-inverse [iso-lens isomorphism-lens?]) isomorphism-lens?]{
Returns the inverse of @racket[iso-lens].
}

@defproc[(make-isomorphism-lenses [f (a/c . -> . b/c)] [inv (b/c . -> . a/c)])
         (values isomorphism-lens? isomorphism-lens?)]{
Returns two values. The first value is the result of
@racket[(make-isomorphism-lens f inv)], and the second value is the inverse of that
lens.

The lenses @racket[symbol->string-lens] and @racket[string->symbol-lens], for
example, are defined like this:
@racketblock[
  (define-values [string->symbol-lens symbol->string-lens]
    (make-isomorphism-lenses string->symbol symbol->string))
]}

@defproc[(isomorphism-compose [lens isomorphism-lens?] ...) isomorphism-lens?]{
Like @racket[lens-compose], but works only on isomorphism lenses, and returns an
isomorphism lens. It is also more efficient than @racket[lens-compose].
}

@defproc[(isomorphism-thrush [lens isomorphism-lens?] ...) isomorphism-lens?]{
Like @racket[lens-thrush], but works only on isomorphism lenses, and returns an
isomorphism lens. It is also more efficient than @racket[lens-thrush].
}

@deflenses[[string->symbol-lens symbol->string-lens
            number->string-lens string->number-lens
            list->vector-lens vector->list-lens
            list->string-lens string->list-lens]]{
Isomorphim lenses for @racket[string->symbol], @racket[number->string], and so on.
}
