lue-bird/elm-linear-direction/9.0.0

NOTE: the latest version is 11.2.0

linear direction

  • -r/-l
    • for example: foldr, foldl
    • does foldr mean fold to the right? unclear and easy to mix up
    • no Array.getr/l, setr/l but foldr, foldl?
  • negative indices
    • Array.slice 0 -1 is handy! But you can't do slice 2 -0
    • many operations support negative indices, but others don't
    • not explicit → can have unintended side-effects

How about taking the Direction as an argument!

import Linear exposing (Direction(..))
import List.Linear
import Array exposing (Array)
import Array.Linear

[ 'l', 'i', 'v', 'e' ]
    |> List.Linear.foldFrom "" Up String.cons
--> "evil"

[ 'l', 'i', 'v', 'e' ]
    |> List.Linear.foldFrom "" Down String.cons
--> "live"

Array.fromList [ 'e', 'v', 'i', 'l' ]
    |> Array.Linear.element ( Down, 0 )
--> Just 'l'
  • → a less cluttered API, e.g.

    • foldFrom instead of foldr, foldl
    • toChunksOf instead of chunksFromLeft/-Right
    • padToLength instead of resizerRepeat, resizelRepeat, resizerIndexed, resizelIndexed
  • → deal with both directions at once

    import Linear exposing (DirectionLinear)
    import Array exposing (Array)
    import Array.Linear
    
    elementAlter :
        ( DirectionLinear, Int )
        -> (element -> element)
        -> (Array element -> Array element)
    elementAlter location alter =
        \array ->
            case array |> Array.Linear.element location of
                Nothing ->
                    array
    
                Just elementAtLocation ->
                    array
                        |> Array.Linear.elementReplace location
                            (\() -> elementAtLocation |> alter)
  • → direction is always explicit

where linear direction is already being used

suggestions?

→ contribute