# If not feasible to implement a functionality in Motoko, is it possible to create a Motoko interface able to call an existing library?

**URL:** https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569
**Category:** Language Support
**Tags:** Motoko
**Created:** [March 18, 2022, 7:06am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569 "2022-03-18T07:06:42Z")
**Posts on this page:** 18
**Page:** 2

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 22, 2022, 12:09pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/21 "2022-03-22T12:09:13Z")

</div>

@skilesare@paulyoung. It seems that a library responsible to execute a large computation should divide this computation in chunks. Ok, I get the idea. So, probably Dfinity has already thought about this and they may have a “best practice” implementation pattern. I would imagine, such implementation pattern should be actually implemented in some core package provided by Dfinity due to the close connection with the details of the protocol specification. Does this package or implementation pattern exist? if it does not exist, then should we propose and implement this core package first?

---

<div class="post-metadata">

### Author: ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)
#### Post date: [March 22, 2022, 12:36pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/22 "2022-03-22T12:36:39Z")

</div>

> [@paulyoung](#):
>
> A way to achieve something similar today would be to compile an existing package to Wasm and expose it as a canister.

Can you also expose it as a `module` instead of of an `actor` and just reference it locally to include in your actor build?

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 22, 2022, 12:41pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/23 "2022-03-22T12:41:25Z")

</div>

When you write module, do you mean a motoko package?

---

<div class="post-metadata">

### Author: ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)
#### Post date: [March 22, 2022, 12:42pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/24 "2022-03-22T12:42:51Z")

</div>

Yes exactly, that’s what I mean.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 22, 2022, 12:46pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/25 "2022-03-22T12:46:47Z")

</div>

This is also the suggestion by @skilesare. It seems that this is the right way. However, now the discussion moved to the problem of how to implement a motoko package where methods require a lot of computation and need to be divided in chunks to prevent errors at the end of a consensus round. I suggested/asked about the need of a standardized implementation pattern for modules doing large computations. this was the last issue along that line…

---

<div class="post-metadata">

### Author: ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)
#### Post date: [March 22, 2022, 1:52pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/26 "2022-03-22T13:52:03Z")

</div>

I have written [GitHub - skilesare/pipelinify.mo: Move data chunks between canisters](https://github.com/skilesare/pipelinify.mo) to do this. It allows for sequential or parallel computation. There are a few tests, but they are lacking and the documentation is even more sparse. I’m happy to answer questions about it and would love pull requests. It also allows for streaming workloads to other canisters and pushing/pulling data back and forth.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 23, 2022, 9:12am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/27 "2022-03-23T09:12:44Z")

</div>

@skilesare, thank you for sharing your project. As it is now, it would be a very significant effort to reverse engineer your code to figure out how it works (motivation of design decisions and their implementation), how to use it and whether it is at all relevant to the case of implementing a linear algebra library.

In my opinion, the following documentation would help: 1) the problem that this library is trying to solve, 2) the design decisions that you took to address this problem, 3) functions should be commented to connect code with the design decisions, 4) finally, there should be a description of how to implement a library that use your helper library.

@paulyoung, probably the problem of executing a motoko function beyond the consensus round period is a problem that you have already addressed in some forum post and/or internally in some of your daily development tasks. Could you please advice us on how to proceed?

---

<div class="post-metadata">

### Author: ![Harsh](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@Harsh](https://forum.dfinity.org/u/Harsh)
#### Post date: [March 23, 2022, 10:30am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/28 "2022-03-23T10:30:51Z")

</div>

My team will try create couple of ML and advanced algebra modules for Motoko from their python counterparts later this year. But for the present, my question is: if JS works well in combination with Motoko and JS does have some libraries like tensorflow that can be used, wouldn’t such a combination be helpful for now?

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 23, 2022, 10:38am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/29 "2022-03-23T10:38:17Z")

</div>

@Harsh That is great. What are for you the “ML and algebra” methods with most priority and why? It would be also interesting to know what are your plans to solve the problem we discussing in this post: how to deal with functions with longer execution time than the consensus period?

Concerning using JS, I guess it depends of your application: if it is fine for your application to execute javascript in the browser, then do it. A different case is to develop a backend service always processing data (e.g. oracles, bots, etc). In the second case, rust and motoko are, by the time being, the only supported options (I heard typescript is soon coming but not yet fully supported)

---

<div class="post-metadata">

### Author: ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)
#### Post date: [March 23, 2022, 11:34am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/30 "2022-03-23T11:34:19Z")

</div>

> [@ildefons](#):
>
> In my opinion, the following documentation would help: 1) the problem that this library is trying to solve, 2) the design decisions that you took to address this problem, 3) functions should be commented to connect code with the design decisions, 4) finally, there should be a description of how to implement a library that use your helper library.

Yes, it desperately needs some documentation. It is on my list of todos.

In the mean time:

The simplest implementation is in the tests on [pipelinify.mo/tests/\_pipelinifyTest-Processor.mo at a34056ba1109060a92d67802027f8aaa9c67aae3 · skilesare/pipelinify.mo · GitHub](https://github.com/skilesare/pipelinify.mo/blob/a34056ba1109060a92d67802027f8aaa9c67aae3/tests/_pipelinifyTest-Processor.mo#L149)

The consumer test file also shows how to push and pull data between canisters as well.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [March 23, 2022, 12:29pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/31 "2022-03-23T12:29:18Z")

</div>

> [@skilesare](#):
>
> The simplest implementation is in the tests on [pipelinify.mo/\_pipelinifyTest-Processor.mo at a34056ba1109060a92d67802027f8aaa9c67aae3 · skilesare/pipelinify.mo · GitHub](https://github.com/skilesare/pipelinify.mo/blob/a34056ba1109060a92d67802027f8aaa9c67aae3/tests/_pipelinifyTest-Processor.mo#L149)
> 
> The consumer test file also shows how to push and pull data between canisters as well

Thank you. I will study it.

@skilesare, something that I did not understand until now is that it looks like you are assuming that the motoko module (i.e my linear algebra library) is supposed to be an independent canister and all calls to the new library will be canister calls. Is this correct?

---

<div class="post-metadata">

### Author: ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)
#### Post date: [March 23, 2022, 1:01pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/32 "2022-03-23T13:01:17Z")

</div>

You could build it that way, but the pipelinify module supports local serialized and parallel processing.

When you call .process you pass in a process config and you have a number of options:

```auto
public type DataConfig = {
        #dataIncluded : {
            data: [AddressedChunk]; //data if small enough to fit in the message
        };
        #local : Nat;

        #pull : {
            sourceActor: ?DataSource;
            sourceIdentifier: ?Hash.Hash;
            mode : { #pull; #pullQuery;};
            totalChunks: ?Nat32;
            data: ?[AddressedChunk];
        };
        #push;
        #internal;
    };

```

#dataIncluded means that the data is in the request  
#local(id) means that you’ve stored the data somewhere in your canister and the system will call the function you configure for getLocalWorkspace on the initialization interface to get a handle on the data. You would only use push and pull if you were calling the library from another canister.

---

<div class="post-metadata">

### Author: ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)
#### Post date: [March 23, 2022, 8:03pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/33 "2022-03-23T20:03:35Z")

</div>

> [@ildefons](#):
>
> @paulyoung, probably the problem of executing a motoko function beyond the consensus round period is a problem that you have already addressed in some forum post and/or internally in some of your daily development tasks.

I’m afraid not. At least not yet.

I am interested in solutions that abstract this problem away from us as developers though.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [April 3, 2022, 11:34am UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/34 "2022-04-03T11:34:48Z")

</div>

@paulyoung @skilesare, today I learned about the system “hearbeat” function and I wondered whether this periodic system signal is related to the “consensus period”. If this was the case, it would be possible to create a generic method to interrupt/save/recover the state of long computations. Does it make sense or I am confused?

---

<div class="post-metadata">

### Author: ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)
#### Post date: [April 3, 2022, 2:29pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/35 "2022-04-03T14:29:58Z")

</div>

Yes. I have on my todo list to add heartbeat to [GitHub - skilesare/pipelinify.mo: Move data chunks between canisters](https://github.com/skilesare/pipelinify.mo). Heartbeat is expensive though, so a external crank turner will likely save you some money.

The library supports you just calling process(process\_id) until you get a #done back.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [April 3, 2022, 2:57pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/36 "2022-04-03T14:57:00Z")

</div>

> [@skilesare](#):
>
> Heartbeat is expensive though

Does this mean that calling a “hearbeat” method consumes much more cycles that calling a normal canister method?

> [@skilesare](#):
>
> so a external crank turner will likely save you some money.

What is an “external crank turner”?

---

<div class="post-metadata">

### Author: ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)
#### Post date: [April 3, 2022, 3:08pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/37 "2022-04-03T15:08:04Z")

</div>

> [@ildefons](#):
>
> Does this mean that calling a “hearbeat” method consumes much more cycles that calling a normal canister method?

It is a function called every second I think. So even if you just check a flag, you are using cycles.

> [@ildefons](#):
>
> What is an “external crank turner”?

Just an external process. It can be another canister, a web browser waiting for work to be done, an aws instance I charge if processing requests.

---

<div class="post-metadata">

### Author: ![ildefons](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ildefons/32/2638_2.png) [@ildefons](https://forum.dfinity.org/u/ildefons)
#### Post date: [April 5, 2022, 2:21pm UTC](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569/38 "2022-04-05T14:21:59Z")

</div>

> [@skilesare](#):
>
> It is a function called every second I think. So even if you just check a flag, you are using cycles.

@skilesare Why the cost in cycles of an implementation based on “heartbeat” is much higher than another implementation based on an external process? aren’t both just update calls?

[Previous page](https://forum.dfinity.org/t/if-not-feasible-to-implement-a-functionality-in-motoko-is-it-possible-to-create-a-motoko-interface-able-to-call-an-existing-library/11569.md?page=1)
