Skip to content

Commit

Permalink
Define the either type
Browse files Browse the repository at this point in the history
  • Loading branch information
oovm committed Jun 6, 2024
1 parent b0a7809 commit b9ef3f4
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/valkyrie-experiment/source/category/Applicative.vk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trait Applicative: Functor {
left_a2<A, B>(f: (A, A) -> B, a: Self<A>, b: Self<B>): Self<B>
}

extends Option<$>: Applicative {
extends Option::<_>: Applicative {
apply<A, B>(self, other: Option<A -> B>) -> Option<B> {
match other {
case Some(f): self.map(f)
Expand All @@ -21,7 +21,7 @@ extends Option<$>: Applicative {
}
}

extends Result<$, E>: Applicative {
extends Result::<_, E>: Applicative {
apply<A, B>(self, other: Result<A -> B, E>) -> Result<B, E> {
match (self, other) {
case (Fine(v), Fine(f)): Fine(f(v))
Expand Down
4 changes: 2 additions & 2 deletions packages/valkyrie-experiment/source/category/Comonad.vk
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace package.category;


trait Comonad {
\\ dual of Monad::unit
# dual of Monad::unit
extract(m: Self<A>) -> A;
\\ dual of Monad::join
# dual of Monad::join
duplicate(m: Self<A>) -> Self<Self<A>>;
}

Expand Down
8 changes: 0 additions & 8 deletions packages/valkyrie-experiment/source/category/Either.vk

This file was deleted.

6 changes: 3 additions & 3 deletions packages/valkyrie-experiment/source/category/Functor.vk
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace package.category;

trait Functor<$> {
trait Functor<_> {
#functional
map<A, B>(self: Self<A>, f: A -> B): Self<B>
}


extends Option<$>: Functor {
extends Option::<_>: Functor {
map<A, B>(self: Option<A>, f: A -> B): Option<B> {
match self {
case Some(v): Some(f(v))
Expand All @@ -15,7 +15,7 @@ extends Option<$>: Functor {
}
}

extends Result<$, E>: Functor {
extends Result::<_, E>: Functor {
map<A, B>(self: Result<A, E>, f: A -> B): Result<B, E> {
match self {
case Fine(v): Some(f(v))
Expand Down
8 changes: 8 additions & 0 deletions packages/valkyrie-experiment/source/category/Laterality.vk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@



trait Laterality {

}
# some in option
# fine in result
12 changes: 6 additions & 6 deletions packages/valkyrie-experiment/source/coroutine/sequence.vk
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ trait Sequence<T>: Iterable<IteratorType: SequenceIterator, Item: T> {



\\\ 可扩容的序列
#? 可扩容的序列
trait ScalableSequence<T>: Sequence<T> {
\\\ 保证容量至少为 `capacity`,如果容量不足则扩容
#? 保证容量至少为 `capacity`,如果容量不足则扩容
push(mut self, item: T)
\\\ 保证容量至少为 `capacity`,如果容量不足则扩容
#? 保证容量至少为 `capacity`,如果容量不足则扩容
pop(mut self) -> Sequence::Item?
\\\ 能把 other 的元素逐个放到左边
#? 能把 other 的元素逐个放到左边
append(mut self, other: Sequence);
\\\ 能把 other 的元素逐个放到右边
#? 能把 other 的元素逐个放到右边

shrink(mut self)
clear(mut self)
Expand All @@ -71,7 +71,7 @@ trait ScalableSequence<T>: Sequence<T> {
trait NonEmptySequence<T>: ScalableSequence<T> {
override pop(ska self) -> Sequence::Item?
}
\\\ 可变的序列
#? 可变的序列
trait MutableSequence: Sequence {
set(mut self, index: Integer, item: Sequence::Item);
each(mut self, f: (Integer, Sequence::Item) -> ()) {
Expand Down
16 changes: 16 additions & 0 deletions packages/valkyrie-standard/source/category/List.valkyrie
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@




trait List {

}

trait Dict {

}

trait Set {

}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace! package.collection.array;

class Array⟨T, L: usize? = None⟩ {
↯instruction("array.new_default")
constructor(size: usize) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace! package.collection.tree.binary;

class BinaryTree⟨T⟩ {
data: T,
lhs: BinaryTree⟨T⟩?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace! package.collection.tree.doubly;

class DoublyTree⟨T⟩ {
parent: Self?,
sibling_left: Self?,
sibling_riht: Self?,
child_head: Self?,
child_tail: Self?,
data: T,
}
12 changes: 12 additions & 0 deletions packages/valkyrie-standard/source/collection/either/_.valkyrie
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace! package.collection.either;

unity Either⟨L, R⟩ {
# Failure
Lyft {
wrong: L
},
# Success
Riht {
right: R
},
}
11 changes: 11 additions & 0 deletions packages/valkyrie-standard/source/collection/either/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| Language | Wasm Type |
|:---------|:-----------------|
| `T` | `ref $T` |
| `T?` | `ref null $T` |
| `T??` | `ref null $Some` |
| `u8` | `i32` |
| `u8?` | `ref null i31` |
| `u8??` | `ref null i32` |



Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace! package.collection.list.linked;

class LinkedList<T> {
data: T,
rhs: LinkedList<T>?
sibling_riht: LinkedList<T>?
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ class OutOfRange⟨T⟩ {
min: T?
max: T?

constructor(current: T, range: Range⟨T⟩) {
this.current = current;
this.min = Some(range.min)
this.max = Some(range.max)
}
extract(self): (value: T, min: T?, max: T?) {
(self.current, self.min, self.max)
}
# constructor(current: T, range: Range⟨T⟩) {
# this.current = current;
# this.min = Some(range.min)
# this.max = Some(range.max)
# }
# extract(self): (value: T, min: T?, max: T?) {
# (self.current, self.min, self.max)
# }
}

↯effect.catch
imply⟨T⟩ OutOfRange⟨T⟩ {
typus Resume = T
}
# ↯effect.catch
# imply⟨T⟩ OutOfRange⟨T⟩ {
# typus Resume = T
# }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace package.collection.vector;

class Vector<T> {
private _ptr: Array⟨T⟩
private _len: usize
}
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
class Vector<T> {
private _ptr: Array⟨T⟩
private _len: usize
}
namespace package.collection.vector;

0 comments on commit b9ef3f4

Please sign in to comment.