Skip to content

Commit

Permalink
Std: Future::map, Future::flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Apr 19, 2024
1 parent 349c6bf commit ac50bb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/semantic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ const checkFnDef = (fnDef: FnDef, ctx: Context): void => {
}
enterScope(module, fnScope, ctx)

// TODO: find await-ops and decide whether fn is awaiting

const paramTypes = fnDef.params.map((p, i) => {
checkParam(p, i, ctx)
return p.type!
Expand Down
24 changes: 21 additions & 3 deletions src/std/future.no
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ impl <T> Future<T> {
Future(f, [], Created())
}

pub fn onResolve(self, f: |T|: Unit): Self {
pub fn onResolve(self, f: |T|: Unit): Unit {
match self.state {
Resolved(value) {
f(value)
return self
return unit
}
Created() {
self.spawn(runtime)
}
_ {}
}
self.subscribers.add(f)
self
}

// TODO: belongs to std::control::Functor
pub fn map<U>(self, f: |T|: U): Future<U> {
// TODO: use .await
Future::new(|resolve| self.onResolve(|res| resolve(f(res))))
}

// TODO: belongs to std::control::Monad
pub fn flatMap<U>(self, f: |T|: Future<U>): Future<U> {
// TODO: use .await
Future::new(|resolve| {
self.onResolve(|res| f(res).onResolve(|inner| resolve(inner)))
})
}

pub fn spawn(self, runtime: Runtime): Self {
Expand All @@ -45,6 +61,8 @@ pub type Runtime(
pending: List<Future<_>>,
)

pub let runtime: Runtime = Runtime::new()

impl Runtime {
pub fn new(): Runtime {
Runtime::withPollingRate(10)
Expand Down

0 comments on commit ac50bb7

Please sign in to comment.