MCPcopy Index your code
hub / github.com/disnet/sweet-interfaces

github.com/disnet/sweet-interfaces @v2.0.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.1 ↗ · + Follow
49 symbols 89 edges 6 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

sweet interfaces

This is an experimental implementation of the ECMAScript first-class protocols proposal using Sweet.js.

Install

npm install @sweet-js/cli sweet-interfaces

Use

Import class, protocol, and implements from the sweet-interfaces packages:

'lang sweet.js';
import { class, protocol, implements } from 'sweet-interfaces';
import 'proposal-first-class-protocols';

export protocol Functor {
  map;
}

and compile with Sweet:

sjs functor.js

Sample Output

Input:

'lang sweet.js';
import { class, protocol, implements } from './src/index';

protocol Foldable {
  foldr;

  toArray() {
    return this[Foldable.foldr](
      (m, a) => [a].concat(m),
      []
    );
  }

  get length() {
    return this[Foldable.foldr](m => m + 1, 0);
  }

  contains(eq, e) {
    return this[Foldable.foldr](
      (m, a) => m || eq(a, e),
      false
    );
  }
}

class NEList implements Foldable {
  constructor(head, tail) {
    this.head = head;
    this.tail = tail;
  }

  [Foldable.foldr](f, memo) {
    if (this.tail != null) memo =
      this.tail[Foldable.foldr](f, memo);
    return f(memo, this.head);      
  }
}

let a = new NEList(1, null);
let b = new NEList(0, a);

console.log(b[Foldable.toArray]());
console.log(b[Foldable.length]);

Output:

const Foldable_297 = (function(_extends_300, _unused_301) {
  return new Protocol({
    name: "Foldable",
    extends: _extends_300,
    symbols: { foldr: Symbol("Foldable.foldr") },
    staticSymbols: {},
    protoProperties: Object.getOwnPropertyDescriptors({
      toArray() {
        return this[Foldable_297.foldr](
          (m_302, a_303) => [a_303].concat(m_302),
          []
        );
      },
      get length() {
        return this[Foldable_297.foldr](m_304 => m_304 + 1, 0);
      },
      contains(eq_305, e_306) {
        return this[Foldable_297.foldr](
          (m_307, a_308) => m_307 || eq_305(a_308, e_306),
          false
        );
      }
    }),
    staticProperties: Object.getOwnPropertyDescriptors({})
  });
})([], 0);

class NEList {
  constructor(head_309, tail_310) {
    this.head = head_309;
    this.tail = tail_310;
  }
  [Foldable_297.foldr](f_311, memo_312) {
    if (this.tail != null)
      memo_312 = this.tail[Foldable_297.foldr](f_311, memo_312);
    return f_311(memo_312, this.head);
  }
}

Protocol.implement(NEList, Foldable_297);

let a_298 = new NEList(1, null);
let b_299 = new NEList(0, a_298);
console.log(b_299[Foldable_297.toArray]());
console.log(b_299[Foldable_297.length]);

Extension points exported contracts — how you extend this code

Monad (Interface)
(no doc) [2 implementers]
demo/maybe.js
Functor (Interface)
(no doc)
demo/maybe.js
Apply (Interface)
(no doc)
demo/maybe.js
Applicative (Interface)
(no doc)
demo/maybe.js
Chain (Interface)
(no doc)
demo/maybe.js

Core symbols most depended-on inside this repo

compileAndEval
called by 25
test/_compile.js
compile
called by 24
test/_compile.js
join
called by 8
src/index.js
matchIdentifier
called by 3
src/match-util.js
matchBraces
called by 3
src/match-util.js
matchPropertyName
called by 3
src/match-util.js
isDone
called by 3
src/match-util.js
compileTopLevel
called by 2
test/_compile.js

Shape

Function 26
Class 9
Method 9
Interface 5

Languages

TypeScript100%

Modules by API surface

src/match-util.js20 symbols
demo/maybe.js18 symbols
test.js4 symbols
src/index.js4 symbols
test/_compile.js3 symbols

For agents

$ claude mcp add sweet-interfaces \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact