{"size": 2114, "ext": "scrbl", "max_stars_repo_path": "lens-doc/unstable/lens/if.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/if.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/if.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": 37.75, "max_line_length": 88, "alphanum_fraction": 0.6291390728}
#lang scribble/manual

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

@title{Conditional Lenses}

@defproc[(lens-if [pred (-> target/c any/c)]
                  [lens1 (lens/c target/c view/c)]
                  [lens2 (lens/c target/c view/c)])
         (lens/c target/c view/c)]{
Creates a lens that uses @racket[lens1] when the target satisfies
@racket[pred], and uses @racket[lens2] when the target doesn't satisfy
@racket[pred].
@lens-unstable-examples[
  (define if-lens (lens-if list? first-lens (vector-ref-lens 0)))
  (lens-view if-lens '(1 2 3))
  (lens-view if-lens '#(1 2 3))
  (lens-set if-lens '(1 2 3) 'a)
  (lens-set if-lens '#(1 2 3) 'a)
]}

@defform*[#:literals (else)
          [(lens-cond [pred-expr lens-expr] ... [else else-lens-expr])
           (lens-cond [pred-expr lens-expr] ...)]]{
Like @racket[lens-if], but based on @racket[cond] instead of
@racket[if]. It creates a lens that uses the first lens if the target matches the first
predicate, the second lens if the target matches the second predicate, and so on. If the
target matches none of the predicates, an error is raised.
@lens-unstable-examples[
  (define cond-lens (lens-cond [list? first-lens]
                               [vector? (vector-ref-lens 0)]
                               [string? (string-ref-lens 0)]))
  (lens-view cond-lens '(1 2 3))
  (lens-view cond-lens '#(1 2 3))
  (lens-view cond-lens "123")
  (lens-set cond-lens '(1 2 3) 'a)
  (lens-set cond-lens '#(1 2 3) 'a)
  (lens-set cond-lens "123" #\a)
  (lens-view cond-lens 'none-of-the-above)
]}

@defform[(lens-match [pat lens-expr] ...)]{
Like @racket[lens-if] and @racket[lens-cond], but based on pattern
matching the target against each @racket[pat] with @racket[match].
It creates a lens that uses the first lens if the target matches the
first @racket[pat], the second lens if it matches the second
@racket[pat], and so on.
@lens-unstable-examples[
  (define lens (lens-match [(list a) first-lens]
                           [(list a b) second-lens]))
  (lens-view lens '(1))
  (lens-view lens '(1 2))
  (lens-set lens '(1) 'a)
  (lens-set lens '(1 2) 'a)
]}
