Safe Haskell | None |
---|---|

Language | Haskell2010 |

Lens combinators for Shpadoinkle applications.

## Synopsis

- generalize :: forall f m s a. Functor m => Continuous f => Lens' s a -> f m a -> f m s
- onSum :: forall f m s a. Applicative m => Continuous f => Traversal' s a -> f m a -> f m s
- onRecord :: forall f m s a. Functor m => Continuous f => Lens' s a -> f m a -> f m s
- onRecordEndo :: forall f m s a. Functor m => Continuous f => Lens' s a -> (a -> f m a) -> s -> f m s
- mapLens :: forall f m s a. Functor m => Continuous f => (a -> f m a) -> s -> Lens' s a -> f m s
- (%>) :: forall f m s a. Functor m => Continuous f => (a -> f m a) -> s -> Lens' s a -> f m s
- forLens :: forall f m s a. Functor m => Continuous f => s -> Lens' s a -> (a -> f m a) -> f m s
- (<%) :: forall f m s a. Functor m => Continuous f => s -> Lens' s a -> (a -> f m a) -> f m s
- rounding :: forall a s. Integral s => RealFrac a => Iso' s a
- defaulting :: a -> Iso' (Maybe a) a
- fracIntegral :: forall s a. Integral a => RealFrac s => Prism' s a

# Continuous Composition

generalize :: forall f m s a. Functor m => Continuous f => Lens' s a -> f m a -> f m s Source #

Compose multiple Shpadoinkle views onto a product type, most frequently a record.
Let's say we have `Html`

which produces `Int`

s, and we need to use it in a view
with more components. The model for such a view might be `(Int, String)`

. To use
our child `Html`

inside the parent, we can assign produced `Int`

s to the parent
tuple by using the `_1`

lens like so:

child :: Html Int parent :: Html (Int, String) parent = div_ [ button [ onClick (0, "Reset") [ text "Reset!" ] , generalize _1 child ]

onSum :: forall f m s a. Applicative m => Continuous f => Traversal' s a -> f m a -> f m s Source #

Split multiple Shpadoinkle views over a sum type. This is commonly the case when
using a sum to represent pages in a single page application, but it's useful for
any sum. For example, consider that you have a view with a model of `Either Int String`

and a child `Html`

that produces `Int`

s. You can compose this child onto the parent
using `_Left`

traversal like so:

child :: Html Int parent :: Html (Either Int String) parent = div_ [ button [ onClick (Right "Reset") ] [ text "Reset!" ] , onSum _Left child ]

onRecord :: forall f m s a. Functor m => Continuous f => Lens' s a -> f m a -> f m s Source #

Alias for `generalize`

with a name idiomatic to the common case of composing onto a record.

onRecordEndo :: forall f m s a. Functor m => Continuous f => Lens' s a -> (a -> f m a) -> s -> f m s Source #

mapLens :: forall f m s a. Functor m => Continuous f => (a -> f m a) -> s -> Lens' s a -> f m s Source #

Like `forLens`

but with the lambda as the first argument.

(%>) :: forall f m s a. Functor m => Continuous f => (a -> f m a) -> s -> Lens' s a -> f m s infixr 8 Source #

Infix for `mapLens`

forLens :: forall f m s a. Functor m => Continuous f => s -> Lens' s a -> (a -> f m a) -> f m s Source #

A variant of `generalize`

for the case where you might need to map the smaller value.

parent :: Html (Int, String) parent model = div_ [ forLens model _1 $ (i :: Int) -> if i < 10 then text "too low" else button [ onClick (i + 1) ] [ text "Increment" ] ]

(<%) :: forall f m s a. Functor m => Continuous f => s -> Lens' s a -> (a -> f m a) -> f m s infixl 8 Source #

Infix for `forLens`

# Misc Outlaws

defaulting :: a -> Iso' (Maybe a) a Source #

fracIntegral :: forall s a. Integral a => RealFrac s => Prism' s a Source #