Skip to content

fuseSetters function #117

Description

@mikesol

Hello!

We've been using a function called fuseSetters a fair bit in production to speed up code where several setters are called on complex records (in our case, adding custom OpenAPI tags to a large OpenAPI spec).

Here is the function with the output. If folks find it useful, I can make a PR to include it in the library:

module Main where

import Prelude
import Data.Lens (Setter', _1, _2, over)
import Data.Tuple (Tuple(..), fst, snd)
import Effect (Effect)
import Effect.Class.Console (log)

fuseSetters :: forall s a b c. Setter' a b -> Setter' a c -> Setter' s a -> Setter' s (Tuple (b -> b) (c -> c))
fuseSetters a b c l = over c (over a fa <<< over b fb)
  where
  t = l $ Tuple identity identity

  fa = fst t

  fb = snd t

main :: Effect Unit
main = do
  log
    ( show
        $ over
            (fuseSetters (_1 <<< _1) (_2 <<< _2) (_2 <<< _2 <<< _2))
            (const $ Tuple (const 42) (const 53))
            (Tuple 1 (Tuple 2 (Tuple 3 (Tuple (Tuple 0 1) (Tuple 5 8)))))
    )

yields

07:06 meeshkan-abel@Abel:/tmp/lenz$ spago run
[info] Installation complete.
[info] Build succeeded.
(Tuple 1 (Tuple 2 (Tuple 3 (Tuple (Tuple 42 1) (Tuple 5 53)))))

Thanks and let me know!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions