Tango-br
  • Introduction
  • Começando
    • Índice
    • Recado do desenvolvedor
    • O que é a Tango?
    • Por onde começar?
  • Instalação
    • Instalação via NuGet
    • Instalação manual
  • Conceitos
    • Introdução
    • Utilizando comparação de padrões (Pattern Matching)
    • Valores Opcionais
    • Valores "Ou um ou outro" (Either)
    • Saindo do void para o Unit
    • Delegates Func e Action
    • Utilizando operações encadeadas com processos contínuos
    • Currying e Aplicação Parcial
  • Funcional
    • Introdução
    • Currying
    • Aplicação Parcial
    • Extensões
    • Cast Rápido para Delegates
  • Operações
    • Introdução
    • Operações com Booleans
    • Operações com Inteiros
    • Operações com Decimais
    • Operações com Doubles
    • Operações com Strings
  • Tipos
    • Introdução
    • Unit
    • Option<T>
    • Either<TLeft, TRight>
    • Continuation<TFail, TSuccess>
  • Módulos
    • Introdução
    • Option
      • Apply
      • AsEnumerable
      • Bind
      • Count
      • Exists
      • Filter
      • Fold
      • FoldBack
      • Iterate
      • Map
      • OfNullable
      • ToArray
      • ToList
      • ToNullable
    • Either
      • Exists
      • Iterate
      • Fold
      • FoldBack
      • Map
      • Swap
      • ToTuple
    • Continuation
      • AsContinuation
      • Resolve
      • Reject
      • All
    • Collection
      • Append
      • Choose
      • ChunkBySize
      • Collect
      • CompareWith
      • CountBy
      • Concat
      • Distinct
      • Empty
      • Exists
      • Exists2
      • Filter
      • FindIndex
      • Fold
      • Fold2
      • FoldBack
      • FoldBack2
      • ForAll
      • ForAll2
      • ForAll3
      • Head
      • HeadAndTailEnd
      • Range
      • Generate
      • Initialize
      • Iterate
      • Iterate2
      • IterateIndexed
      • IterateIndexed2
      • Map
      • Map2
      • Map3
      • MapIndexed
      • MapIndexed2
      • MapIndexed3
      • Partition
      • Permute
      • Pick
      • Reduce
      • ReduceBack
      • Replicate
      • Scan
      • Scan2
      • ScanBack
      • ScanBack2
      • Tail
      • TryFind
      • TryPick
      • Unzip
      • Unzip3
      • Zip
      • Zip3
  • Extensões
    • Introdução
    • Extensões para Enum
    • Construtor de EqualityComparer
    • Módulos como extensão
Powered by GitBook
On this page

Was this helpful?

  1. Módulos
  2. Either

Map

Cria um novo valor Either<TLeft, TRight>, onde o valor encapsulado é o resultado da função mapping sobre o valor either, de acordo com seu estado.

Parâmetros

Retorno

Func<TRight, TRightResult> mappingWhenRight

Func<TLeft, TLeftResult> mappingWhenLeft

Either<TLeft, TRight> either

Either<TLeftResult, TRightResult>

Como usar

Esta função é comumente utilizada para alterar um valor Either através de uma função, semelhante ao Map dos valores opcionais.

Esta função utiliza o Match para extrair o valor do tipo Either<TLeft, TRight>, aplica a função mappingWhenRight ou mappingWhenLeft de acordo com o estado do valor.

Quando o valor Either está no estado IsRight e os tipos são transformados

Either<string, int> either = 10;
Either<int, bool> eitherResult = 
    either.Map(
        right => right % 2 == 0,
        left => Convert.ToInt32(left));

//eitherResult.IsRight = true
//eitherResult.Right = true

Quando o valor Either está no estado IsLeft e os tipos são transformados

Either<string, int> either = "25";
Either<int, bool> eitherResult = 
    either.Map(
        right => right % 2 == 0,
        left => Convert.ToInt32(left));

//eitherResult.IsLeft = true
//eitherResult.Left = 25

Abordagens para apenas um dos lados

Você pode utilizar os métodos MapLeft e MapRight para obter o mesmo resultado, mas desta vez aplicando a função somente em um dos valores.

Sempre que estas funções forem aplicados para valores Either que não estão do mesmo tipo que a função de avaliação será retornado o próprio valor Either.

MapRight Quando o valor Either está no estado IsRight

Either<string, int> either = 10;
Either<string, int> eitherResult = 
    either.MapRight(right => right * 2);

//eitherResult.IsRight = true
//eitherResult.Right = 20

MapRight Quando o valor Either está no estado IsLeft

Either<string, int> either = "Hello World";
Either<string, int> eitherResult = either.MapRight(
right => right * 2);

//eitherResult.IsLeft = true
//eitherResult.Left = "Hello World"
PreviousFoldBackNextSwap

Last updated 5 years ago

Was this helpful?