Skip to content

Commit

Permalink
generic range
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Oct 10, 2023
1 parent cef476f commit 6df6f76
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 70 deletions.
34 changes: 0 additions & 34 deletions Stdlib/Data/Int/Range.juvix

This file was deleted.

36 changes: 0 additions & 36 deletions Stdlib/Data/Nat/Range.juvix

This file was deleted.

37 changes: 37 additions & 0 deletions Stdlib/Data/Range.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Stdlib.Data.Range;

import Stdlib.Data.Fixity open;

import Stdlib.Data.Bool.Base open;
import Stdlib.Trait.Natural open;
import Stdlib.Trait.Ord open;

--- A range of naturals
type Range N :=
mkRange {
low : N;
high : N;
step : N
};

syntax iterator for {init := 1; range := 1};

{-# specialize: [1, 2, 3, 5] #-}
for {A N} {{Ord N}} {{Natural N}} (f : A → N → A) (a : A) : Range N → A
| mkRange@{low; high; step} :=
let
{-# specialize-by: [f] #-}
terminating
go (acc : A) (n : N) : A :=
if (n > high) acc (go (f acc n) (n + step));
in go a low;

syntax operator to range;

--- `x to y` is the range [x..y]
to {N} {{Natural N}} (l h : N) : Range N := mkRange l h (fromNat 1);

syntax operator step step;

--- `x to y step s` is the range [x,x+s,..,y]
step {N} (r : Range N) (s : N) : Range N := r@Range{step := s};

0 comments on commit 6df6f76

Please sign in to comment.