diff --git a/fluid/example/linked-outputs/moving-average.fld b/fluid/example/linked-outputs/moving-average.fld index 85e4f713d..c34edf155 100644 --- a/fluid/example/linked-outputs/moving-average.fld +++ b/fluid/example/linked-outputs/moving-average.fld @@ -3,17 +3,17 @@ let nthPad n xs = movingAvg ys window = [ sum [ nthPad n ys | n <- [ i - window .. i + window ] ] / (1 + 2 * window) | i <- [ 0 .. length ys - 1 ] ]; - movingAvg' ps window = + movingAvg' rs window = zipWith - (fun year quantity -> {x: year, y: quantity}) - (map (fun p -> p.year) ps) - (movingAvg (map (fun p -> p.quantity) ps) window); -let points' = - map (fun p -> { x: p.year, y: floor p.quantity }) dataset + (fun x y -> {x: x, y: y}) + (map (fun r -> r.x) rs) + (movingAvg (map (fun r -> r.y) rs) window); +let points = + [ { x: r.year, y: r.emissions } | r <- methane, r.type == "Agriculture" ] in LineChart { - tickLabels: { x: Default, y: Default }, + tickLabels: { x: Rotated, y: Default }, size: { width: 330, height: 285 }, - caption: "Moving averages", - plots: [ LinePlot { name: "Moving average", points: movingAvg' dataset 1 }, - LinePlot { name: "Original curve", points: points' } ] + caption: "SSP5-8.5 projected methane emissions (Agriculture)", + plots: [ LinePlot { name: "Moving average", points: movingAvg' points 1 }, + LinePlot { name: "Original curve", points: points } ] } diff --git a/fluid/lib/prelude.fld b/fluid/lib/prelude.fld index b7070a420..6788f3a8c 100644 --- a/fluid/lib/prelude.fld +++ b/fluid/lib/prelude.fld @@ -87,6 +87,14 @@ let filter p [] = []; let ys = filter p xs in if p x then x : ys else ys; +-- (a -> Option b) -> List a -> List b +let filterMap p [] = []; + filterMap p (x : xs) = + match p x as { + None -> filterMap f xs; + Some y -> y : filterMap f xs + }; + -- (a -> b -> a) -> a -> List b -> a let foldl op z [] = z; foldl op z (x : xs) = foldl op (op z x) xs; diff --git a/src/Standalone/MovingAverage.purs b/src/Standalone/MovingAverage.purs index a6b57b70a..c6999c6e2 100644 --- a/src/Standalone/MovingAverage.purs +++ b/src/Standalone/MovingAverage.purs @@ -10,4 +10,6 @@ import Test.Specs.LinkedOutputs (movingAverages_spec) import Util ((×)) main :: Effect Unit -main = runAffs_ (uncurry drawFig) [ ("fig" × _) <$> loadFig movingAverages_spec.spec ] +main = runAffs_ (uncurry drawFig) + [ ("fig" × _) <$> loadFig movingAverages_spec.spec + ] diff --git a/test/Specs/LinkedOutputs.purs b/test/Specs/LinkedOutputs.purs index c2aa762b2..405b5fab7 100644 --- a/test/Specs/LinkedOutputs.purs +++ b/test/Specs/LinkedOutputs.purs @@ -58,10 +58,10 @@ linkedOutputs_spec2 = movingAverages_spec :: TestLinkedOutputsSpec movingAverages_spec = { spec: - { datasets: [ "dataset" ↦ "example/linked-outputs/moving-average-data" ] - , imports: [ "lib/nombre" ] + { datasets: [ "methane" ↦ "dataset/methane-emissions" ] + , imports: [] , file: File "linked-outputs/moving-average" - , inputs: [ "dataset" ] + , inputs: [ "methane" ] } , δ_out: identity -- TODO: make this a non-trivial test , out_expect: identity