souplesse

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 5b9697e283f3b3494963c1024c22b779527175f3
parent dbc2ee667a64e4977f9385293d2003faef832f3f
Author: Daniel Barlow <dan@telent.net>
Date:   Fri, 15 Nov 2024 21:57:45 +0000

subseq of points from start

Diffstat:
Mfrontend/src/Point.elm | 12+++++++++++-
Mfrontend/tests/PointTest.elm | 45+++++++++++++++++++++++++++++++--------------
2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/frontend/src/Point.elm b/frontend/src/Point.elm @@ -1,4 +1,4 @@ -module Point exposing(Pos, Point, decoder, downsample, duration) +module Point exposing(Pos, Point, decoder, downsample, duration, subseq) import Json.Decode as D @@ -62,3 +62,13 @@ duration points = case points of (p::ps) -> (last p ps).time - p.time _ -> 0 + +subseq points start dur = + case points of + [] -> [] + (p::ps) -> + if p.time < start + then subseq ps start dur + else if p.time >= (start + dur) + then [] + else p::(subseq ps start dur) diff --git a/frontend/tests/PointTest.elm b/frontend/tests/PointTest.elm @@ -1,6 +1,6 @@ module PointTest exposing (specs) -import Point exposing (Point, Pos, downsample) +import Point exposing (Point, Pos, downsample, subseq) import Test exposing (..) import Expect exposing (Expectation) @@ -19,17 +19,34 @@ maybe default val = specs: Test specs = - describe "downsample" - [ test "it returns no more points than requested" <| - \_ -> - let orig = (points 10) - sampled = Point.downsample 5 orig - in Expect.equal 5 (List.length sampled) - , test "it drops points which are too close together" <| - \_ -> - let orig = List.map newPoint [ 0, 1, 2, 3, 4, 10, 500, 1000 ] - sampled = Point.downsample 10 orig - idx p = maybe 0 p.power - in Expect.equalLists [0, 500, 1000] (List.map idx sampled) - + describe "Point" + [ describe "downsample" + [ test "it returns no more points than requested" <| + \_ -> + let orig = (points 10) + sampled = Point.downsample 5 orig + in Expect.equal 5 (List.length sampled) + , test "it drops points which are too close together" <| + \_ -> + let orig = List.map newPoint [ 0, 1, 2, 3, 4, 10, 500, 1000 ] + sampled = Point.downsample 10 orig + idx p = maybe 0 p.power + in Expect.equalLists [0, 500, 1000] (List.map idx sampled) + ] + , describe "subseq" + [ test "chooses only points in the time range" <| + \_ -> + let orig = (points 50) + s = 1731437087 + d = 40 + subs = subseq orig s d + in Expect.equal True + (List.all (\ p -> p.time >= s && p.time <= s + d) subs) + , test "is empty if no points" <| + \_ -> + Expect.equalLists [] (subseq [] 10 10) + , test "is empty if no time" <| + \_ -> + Expect.equalLists (subseq (points 50) 1731437067 0) [] + ] ]