Skip to content

Identity combinator #58

@yaitskov

Description

@yaitskov

Argonaut library has functions to deal with lot of builtin types including Maybe, though I cannot find an option for Identity.

identity combinator, besides making the library more logically complete, has common usecase - a front-end application has a high kinded type to represent partial value for a complex type (structural editor) e.g.

data ValT f = Val (f Number)
data OpT f 
  = Const (f Val) 
  | Prod (f (OpT f)) (f (OpT f)) 
  | Sum (f (OpT f)) (f (OpT f)) 

Form validation deals with type OpM = OpT Maybe, but server expect a complete object, so type Op = Op Identity is going to be serialized to JSON.

valCodec :: CA.JsonCodec Val
valCodec = idCodec CA.number   
import Data.Identity (Identity (..))
import Data.Profunctor (dimap)

unIdentity :: ∀ a. Identity a -> a
unIdentity (Identity a) = a

idCodec :: ∀ a. CA.JsonCodec a -> CA.JsonCodec (Identity a)
idCodec = dimap unIdentity pure

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions