pete-murphy/elm-nlds/1.0.1

NOTE: the latest version is 3.0.1

Peach

A priority search data structure, ported from Unison's pchiusano/peachy library.

Peach is similar to Each but expands branches with smaller weights first. It allows you to explore multiple possibilities, prioritizing by weight.

Core Types

type Peach a

A Peach a represents a priority search computation that can yield multiple weighted results. Results with smaller weights are explored first.

Internally, this is represented as a heap of weighted values.

Primitive Operations

peach : List ( Float, a ) -> Peach a

Create a Peach from a list of weighted values. The values will be explored in order of increasing weight (smallest first).

peach [ ( 2.0, "b" ), ( 1.0, "a" ), ( 3.0, "c" ) ]
    |> head
--> Just ( 1.0, "a" )

A Peach that yields no results (represents failure).

fail
    |> head
--> Nothing

Combinators

map : (a -> b) -> Peach a -> Peach b

Transform the values in a Peach computation.

peach [ ( 1.0, 2 ), ( 2.0, 3 ) ]
    |> map (\x -> x * 2)
    |> toList
--> [ ( 1.0, 4 ), ( 2.0, 6 ) ]
flatMap : (a -> Peach b) -> Peach a -> Peach b

Chain computations where the second depends on the result of the first. The weights are combined additively.

peach [ ( 1.0, "a" ), ( 2.0, "b" ) ]
    |> flatMap (\x -> peach [ ( 0.5, x ++ "1" ), ( 1.0, x ++ "2" ) ])
    |> toList
--> [ ( 1.5, "a1" ), ( 2.0, "a2" ), ( 2.5, "b1" ), ( 3.0, "b2" ) ]
choose : List (Peach a) -> Peach a

Choose between multiple Peach computations, exploring them in priority order.

choose
    [ peach [ ( 1.0, "a" ) ]
    , peach [ ( 2.0, "b" ) ]
    , peach [ ( 0.5, "c" ) ]
    ]
    |> toList
--> [ ( 0.5, "c" ), ( 1.0, "a" ), ( 2.0, "b" ) ]

Extractors

toList : Peach a -> List ( Float, a )

Convert a Peach computation to a list of all results, ordered by weight (smallest first).

peach [ ( 2.0, "b" ), ( 1.0, "a" ), ( 3.0, "c" ) ]
    |> toList
--> [ ( 1.0, "a" ), ( 2.0, "b" ), ( 3.0, "c" ) ]
take : Int -> Peach a -> List ( Float, a )

Take the first n results from a Peach computation, ordered by weight.

peach [ ( 2.0, "b" ), ( 1.0, "a" ), ( 3.0, "c" ) ]
    |> take 2
--> [ ( 1.0, "a" ), ( 2.0, "b" ) ]

Convenience Functions

rankBy : (a -> Float) -> List a -> Peach a

Rank values by a function and create a Peach from them.

rankBy (\s -> toFloat (String.length s)) [ "apple", "kiwi", "banana" ]
    |> head
--> Just ( 4.0, "kiwi" )
each : List a -> Peach a

Create a Peach from a list, treating each element equally (weight 0).

each [ "a", "b", "c" ]
    |> toList
    |> List.length
--> 3
optional : Maybe a -> Peach a

Convert an Optional value to a Peach.

optional (Just "hello")
    |> head
--> Just ( 0.0, "hello" )


optional Nothing
    |> head
--> Nothing