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.
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" )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 ) ]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 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" ) ]Get the first (lowest weight) result from a Peach computation, if any.
peach [ ( 2.0, "b" ), ( 1.0, "a" ) ]
|> head
--> Just ( 1.0, "a" )
fail
|> head
--> NothingConvert 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 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" ) ]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" )Create a Peach from a list, treating each element equally (weight 0).
each [ "a", "b", "c" ]
|> toList
|> List.length
--> 3
A
Peach arepresents 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.