Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Range package #925

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/Examples/range.candy
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
range = use "Range"
[iterator] = use "Core"

main := { environment ->
a = 1 | range.until 10
environment.stdout a

1 | range.until 10 | range.iterate | iterator.forEach { a ->
environment.stdout a
}
}
103 changes: 103 additions & 0 deletions packages/Range/_.candy
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
bound = use ".bound"
[bool, ifElse, int, iterator] = use "Core"
[print] = use "Builtins"

is range := range %
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Empty -> True
All -> True
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
[start, end] -> bound.is start | bool.and (bound.is end)
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
_ -> False

from a :=
needs (int.is a)
[Start: a, End: Unbounded]
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
to a b :=
needs (int.is a)
needs (int.is b)
[Start: Inclusive a, End: Exclusive b]
until a b :=
needs (int.is a)
needs (int.is b)
[Start: Inclusive a, End: Inclusive b]

normalize range :=
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
needs (is range)
range %
Empty -> Empty
All -> All
[start, end] ->
if (start | int.isGreaterThanOrEqualTo end) {
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Empty
} {
(start, end) %
(Unbounded, Unbounded) -> All
_ -> [start, end]
}

contains range value :=
needs (is range)
range %
Empty -> False
All -> True
[start, end] -> bool.and
start %
Unbounded -> True
Inclusive a -> value | int.isGreaterThanOrEqualTo a
Exclusive a -> value | int.isGreaterThan a
end %
Unbounded -> True
Inclusive a -> value | int.isGreaterThanOrEqualTo a
Exclusive a -> value | int.isGreaterThan a
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved

union a b =
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
needs (is a)
needs (is b)
(a, b) %
(Empty, _) -> Empty
(_, Empty) -> Empty
(All, b) -> b
(a, All) -> a
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
(a, b) -> [
Start: a.start %
Unbounded -> b.start
Inclusive a -> b.start %
Unbounded -> Inclusive a
Inclusive b -> Inclusive (max a b)
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Exclusive b -> if (a | int.isGreaterThanOrEqualTo b) { Inclusive a } { Exclusive b }
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Exclusive a -> b %
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Unbounded -> Exclusive a
Inclusive b -> if (b | int.isGreaterThan a) { Inclusive a } { Exclusive b }
Exclusive b -> Exclusive (min a b),
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
End: a.end %
Unbounded -> b.end
Inclusive a -> b.end %
Unbounded -> Inclusive a
Inclusive b -> Inclusive (max a b)
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Exclusive b -> if (a | int.isGreaterThanOrEqualTo b) { Inclusive a } { Exclusive b }
Exclusive a -> b %
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Unbounded -> Exclusive a
Inclusive b -> if (b | int.isGreaterThan a) { Inclusive a } { Exclusive b }
Exclusive b -> Exclusive (min a b),
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
]

iterate range :=
needs (is range)
range.start %
Unbounded -> needs False "The range needs to have a bounded start."
_ -> Nothing
first = range.start %
Inclusive a -> a
Exclusive a -> a | int.add 1
range.end %
Unbounded -> iterator.generateWithState first { next ->
More [Item: next, State: next | int.add 1]
}
bound ->
end = bound %
Inclusive a -> a | int.add 1
Exclusive a -> a
iterator.generateWithState [Next: first, end] { state ->
ifElse (state.next | int.isGreaterThanOrEqualTo state.end) { Empty } {
More [Item: state.next, State: [Next: state.next | int.add 1, End: state.end]]
}
}
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
Empty file added packages/Range/_package.candy
Empty file.
7 changes: 7 additions & 0 deletions packages/Range/bound.candy
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[int] = use "Core"

is bound := bound %
Unbounded -> True
Inclusive a -> int.is a
Exclusive a -> int.is a
MarcelGarus marked this conversation as resolved.
Show resolved Hide resolved
_ -> False
Loading