Code Monkey home page Code Monkey logo

bs-mithril-stream's People

Contributors

kristianmandrup avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

tzkmx

bs-mithril-stream's Issues

Modelling mithril/stream

After taking a look at the docs ( https://mithril.js.org/stream.html#stream ), I believe Mithril streams are implemented as essentially callbacks. E.g. in https://mithril.js.org/stream.html#combining-streams there's an example:

var a = stream(5)
var b = stream(7)

var added = stream.combine(function(a, b) {
    return a() + b()
}, [a, b])

console.log(added()) // logs 12

According to the docs for combine, the a and b that get passed into the combiner function are themselves stream values. Since they get called as functions, therefore they must be functions. So based on that, I came up with the following sample set of bindings to give you an idea of the approach I would take:

/* Mithril_Stream.re */

/** Need to uncurry to prevent generating JS like x(0)
    See https://bucklescript.github.io/docs/en/function#curry-uncurry */
type t('a) = (. unit) => 'a;

/* See
   https://bucklescript.github.io/docs/en/import-export#import-a-default-value
   Test: let s = make(1); */
[@bs.module] external make: 'a => t('a) = "mithril/stream";

/* See https://bucklescript.github.io/docs/en/common-data-types#tuple
   Test:
   let s1 = make(1);
   let s2 = make(2);
   let s3 = combine2((. s1, s2) => s1(. ) + s2(. ), (s1, s2)); */
[@bs.module "mithril/stream"] external combine2: (
  (. t('a1), t('a2)) => 'b,
  (t('a1), t('a2)),
) => t('b) = "combine";

[@bs.module "mithril/stream"] external combine3: (
  (. t('a1), t('a2), t('a3)) => 'b,
  (t('a1), t('a2), t('a3)),
) => t('b) = "combine";

[@bs.module "mithril/stream"] external combine4: (
  (. t('a1), t('a2), t('a3), t('a4)) => 'b,
  (t('a1), t('a2), t('a3), t('a4)),
) => t('b) = "combine";

/* Test:
   let s = make(1);
   let t = map((. x) => x + 1, s); */
[@bs.send.pipe: t('a)] external map: ((. 'a) => 'b) => t('b) = "";

[@bs.send.pipe: t('a)] tells BuckleScript to inject a final parameter of type t('a) into the external, and generate a method call to that parameter. It's very useful but was somewhat prematurely removed from the BuckleScript doc site.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.