• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Elm
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functional abstractions to manipulate complex records in Elm - Iso, Prism, Lens, Optional, Traversal.

Build Status

elm-monocle

A Monocle-inspired library providing purely functional abstractions to manipulate complex records in the elm language.

Published as arturopala/elm-monocle library.

Long Example

import Monocle.Optional exposing (Optional)
import Monocle.Lens exposing (Lens)


type StreetType
    = Street
    | Avenue


type Country
    = US
    | UK
    | FI
    | PL
    | DE


type alias Address =
    { streetName : String
    , streetType : StreetType
    , floor : Maybe Int
    , town : String
    , region : Maybe String
    , postcode : String
    , country : Country
    }


type alias Place =
    { name : String
    , description : Maybe String
    , address : Maybe Address
    }


addressOfPlace : Optional Place Address
addressOfPlace =
    Optional .address (\b a -> { a | address = Just b })


regionOfAddress : Optional Address String
regionOfAddress =
    Optional .region (\b a -> { a | region = Just b })


streetNameOfAddress : Lens Address String
streetNameOfAddress =
    Lens .streetName (\b a -> { a | streetName = b })


regionOfPlace : Optional Place String
regionOfPlace =
    addressOfPlace |> Monocle.Compose.optionalWithOptional regionOfAddress


streetNameOfPlace : Optional Place String
streetNameOfPlace =
    addressOfPlace |> Monocle.Compose.optionalWithLens streetNameOfAddress


place : Place
place =
    { name = "MyPlace"
    , description = Nothing
    , address =
        Just
            { streetName = "Union Road"
            , streetType = Street
            , floor = Nothing
            , town = "Daisytown"
            , region = Nothing
            , postcode = "00100"
            , country = US
            }
    }


updatedPlace : Place
updatedPlace =
    place
        |> regionOfPlace.set "NorthEast"
        |> streetNameOfPlace.set "Union Avenue"

Abstractions

Iso

An Iso is a tool which converts elements of type A into elements of type B and back without loss.

    type alias Iso a b =
        { get : a -> b
        , reverseGet : b -> a
        }
Example
    string2CharListIso : Iso String (List Char)
    string2CharListIso =
        Iso String.toList String.fromList

    (string2CharListIso.get "ABcdE") == ['A','B','c','d','E']
    (string2CharListIso.reverseGet ['A','B','c','d','E']) == "ABcdE"

Prism

A Prism is a tool which optionally converts elements of type A into elements of type B and back.

    type alias Prism a b =
        { getOption : a -> Maybe b
        , reverseGet : b -> a
        }
Example
    string2IntPrism : Prism String Int
    string2IntPrism =
        Prism String.toInt String.fromInt

    string2IntPrism.getOption "17896" == Just 17896
    string2IntPrism.getOption "1a896" == Nothing
    string2IntPrism.reverseGet 1626767 = "1626767"

Lens

A Lens is a functional concept which solves a very common problem: how to easily update a complex immutable structure, for this purpose Lens acts as a zoom into a record.

    type alias Lens a b =
        { get : a -> b
        , set : b -> a -> a
        }
Example
    type alias AddressΒ = 
        { streetName: String
        , postcode: String
        , town: String
        }

    type alias Place =
        { name: String
        , address: Address
        }

    addressStreetNameLens : Lens Address String
    addressStreetNameLens =
        Lens .streetName (\b a -> { a | streetName = b })

    placeAddressLens : Lens Place Address
    placeAddressLens =
        Lens .address (\b a -> { a | address = b })

    placeStreetName: Lens Place String
    placeStreetName =
        placeAddressLens |> Monocle.Compose.lensWithLens addressStreetNameLens

    myPlace = Place "my" (Address "Elm" "00001" "Daisytown")
    placeStreetName.get myPlace == "Elm"
    
    myNewPlace = placeStreetName.set "Oak" myPlace

    placeStreetName.get myNewPlace == "Oak"
    myNewPlace == Place "my" (Address "Oak" "00001" "Daisytown")

Optional

A Optional is a weaker Lens and a weaker Prism.

    type alias Optional a b =
        { getOption : a -> Maybe b
        , set : b -> a -> a
        }
Example
    addressRegionOptional : Optional Address String
    addressRegionOptional =
        Optional .region (\b a -> { a | region = Just b })

    string2IntPrism : Prism String Int
    string2IntPrism = Prism String.toInt String.fromInt

    addressRegionIntOptional: Optional Address Int
    addressRegionIntOptional =
        addressRegionOptional |> Monocle.Compose.optionalWithPrism string2IntPrism

    string2CharListIso : Iso String (List Char)
    string2CharListIso = Iso String.toList String.fromList

    addressRegionListCharOptional: Optional Address (List Char)
    addressRegionListCharOptional =
        addressRegionOptional |> Monocle.Compose.optionalWithIso string2CharListIso

    modifyRegion: String -> String
    modifyRegion region = String.reverse region

    modifyAddressRegion: Address -> Maybe Address
    modifyAddressRegion address = Optional.modifyOption addressRegionOptional modifyRegion address

    modifyRegion: String -> String
    modifyRegion region = String.reverse region

    modifyAddressRegion: Address -> Address
    modifyAddressRegion address = Optional.modify addressRegionOptional modifyRegion address

Traversal

A Traversal allows you to modify many elements at once.

    type alias Traversal a b =
        (b -> b) -> a -> a

(Traversal a b is just an alias for a function that applies a transformation over b elements of a larger a structure.)

Example
    firstNameLens : Lens Friend String
    firstNameLens =
        Lens .firstName (\b a -> { a | firstName = b })

    bestFriendsTraversal : Traversal (List Friend) Friend
    bestFriendsTraversal =
        Traversal.some
            Traversal.list
            (\friend -> friend.value == Best)

    friendsLens : Lens Account (List Friend)
    friendsLens =
        Lens .friends (\b a -> { a | friends = b })

    firstNamesOfBestFriends : Traversal Account String
    firstNamesOfBestFriends =
        friendsLens
            |> Compose.lensWithTraversal bestFriendsTraversal
            |> Compose.traversalWithLens firstNameLens

    upcaseBestFriendsFirstNames : Account -> Account
    upcaseBestFriendsFirstNames account =
        Traversal.modify firstNamesOfBestFriends String.toUpper

Common

Common lenses/prisms/optionals that most projects will use.

Step into a Maybe value.

    maybe.set 5 Nothing
    > Just 5

Step into an Array at the given index.

    .getOption (array 2) (Array.fromList [ 10, 11, 12, 13 ])
    > Just 12

    .getOption (array 8) (Array.fromList [ 10, 11, 12, 13 ])
    > Nothing

Step into a Dict with the given key.

    .getOption (dict "Tom") (Dict.fromList [ ( "Tom", "Cat" ) ])
    > Just "Cat"

    .getOption (dict "Jerry") (Dict.fromList [ ( "Tom", "Cat" ) ])
    > Nothing

Step into the success value of a Result.

    result.getOption (Ok 5)
    > Just 5

    result.getOption (Err "500")
    > Nothing

Step into a record with an id key.

Since records with an id field are incredible common, this is included for convenience. It also serves as a simple recipe for creating record lenses.

    id.get { id = 1000, name = ... }
    > 1000

Step into the first element of a pair.

    first.get ( 'a', 'b' )
    > 'a'

Step into the second element of a pair.

    second.get ( 'a', 'b' )
    > 'b'

Build

Prerequisites

  • Node.js
  • Yarn
  • Run yarn install-with-elm

Compile

Run yarn compile

Test

Run elm-test

More Repositories

1

buffer-and-slice

Lightweight Buffer and Slice abstractions for Scala.
Scala
25
star
2

make-it-g8

A tool to convert any project into a giter8 template
Scala
25
star
3

akka-io-sctp

Akka I/O driver for SCTP protocol based on Oracle JDK 7/8 support.
Scala
23
star
4

play-2.4-crud-with-reactive-mongo

Play 2.4 project seed: Generic CRUD with Macwire and ReactiveMongo
Scala
21
star
5

cross-scala.g8

Simple Scala template with cross-version and multi-target compilation
Scala
11
star
6

validator

Simple but versatile Scala validator using Either[Error,Unit] to represent validation status.
Scala
7
star
7

github-activity

Elm
5
star
8

scala-data-structures

scala data structures and graph algorithms
Scala
5
star
9

scala-tree

General purpose, covariant, immutable, low overhead, efficient, monadic tree-like data structure with comprehensive API.
Scala
5
star
10

gitignore

Scala
5
star
11

stock-exchange

Take-home assignment to implement Stock Exchange
Java
4
star
12

scala-xml-security

XML Security library for Scala - handy tool for easy signing, verifying, encrypting and decrypting XML documents
Scala
4
star
13

scala-sbt-workshop

sbt internals workshop for Avaus devs @wroclaw
Scala
3
star
14

spring-boot-flights-search

Flights search demo REST service
Java
2
star
15

min-max-missing-number

An algorithm to find min and max integer number missing from the provided set of numbers.
Scala
1
star
16

baseball-game-exercise

Conceptual component to study normalisation of streamed game events using baseball example.
Scala
1
star
17

math-game-elm

Math game written in Elm
JavaScript
1
star
18

polymer3-typescript-webpack-seed

Seed of reactive web application using Polymer 3.x + Typescript + Webpack
JavaScript
1
star
19

recipes-api

Simple Recipe API microservice written with Node.js and React.js
JavaScript
1
star
20

traffic-lights-control

Traffic lights control simulator (Akka)
Scala
1
star