Skip to content

A tiny package containing operators missing from the official package

License

Notifications You must be signed in to change notification settings

qwbarch/optics-operators

Repository files navigation

optics-operators

build License: MIT Hackage

A tiny package containing operators missing from the official package.

Why does this package exist?

The optics library is missing convenient operators that the lens library provides.
I've only added the operators I need for now. Feel free to open an issue or pull request to add new ones.

Quick start

This is a literate haskell file. You can run this example via the following:

nix develop --command cabal run --flags="build-readme"

Necessary language extensions and imports for the example:

{-# LANGUAGE DeriveGeneric, OverloadedLabels #-}
import GHC.Generics (Generic)
import Control.Monad.State (State, execState)
import Data.Optics.Operators ((+=), (-=), (*=))

Basic example using state operators:

newtype Person = Person
  { age :: Int
  } deriving (Show, Generic)

addAge :: Int -> State Person ()
addAge age = #age += age

subtractAge :: Int -> State Person ()
subtractAge age = #age -= age

Running the example:

person :: Person
person = Person 50

main :: IO ()
main = print . flip execState person $ do
  addAge 10
  subtractAge 20
  #age *= 2

-- Output: Person {age = 80}

About

A tiny package containing operators missing from the official package

Resources

License

Stars

Watchers

Forks

Packages

No packages published