~technomancy/fennel

6 2

Fennel library/module example?

Details
Message ID
<94172c53-ca95-4b14-a22e-d542aafb070d@www.fastmail.com>
DKIM signature
fail
Download raw message
DKIM signature: fail
Can you please give me a "Hello, Fennel Module!" example? That is:

  * what would the my-module.fnl file contain? (Did I get the name
    right? Can it contain dashes, or must they be underscores?)

  * Regarding the code to `require` it, it looks like, [according to the
    tutorial](https://fennel-lang.org/tutorial#modules-and-multiple-files),
    that would be:

    ~~~
    (local fennel (require :fennel))
    (table.insert (or package.loaders package.searchers) fennel.searcher)
    (local my-module (require :lib.my-module))
    ~~~

    Is that correct?

Thanks,
-- John
Details
Message ID
<878seo8y9c.fsf@hagelb.org>
In-Reply-To
<94172c53-ca95-4b14-a22e-d542aafb070d@www.fastmail.com> (view parent)
DKIM signature
missing
Download raw message
John Gabriele <jgabriele@fastmail.fm> writes:

> Can you please give me a "Hello, Fennel Module!" example? That is:
>
>   * what would the my-module.fnl file contain? (Did I get the name
>     right? Can it contain dashes, or must they be underscores?)

Modules almost always end in a table; that table is then returned when
you call (require :my-module). A common pattern is to define a bunch of
functions above, then the table just consists of something like this:

    {: my-first-function : my-second-function : super-third-function}

Which is shorthand for:

    {:my-first-function my-first-function
     :my-second-function my-second-function
     :super-third-function super-third-function}

Occasionally you will see a module whose return value is a function
rather than a table, but this is rare and advised against, as it
interferes with reloading strategies. There is no technical reason you
couldn't return a string or number. Whatever the last value in the file
is will be the module's value.

Dashes should be used in filenames; no silly Clojure-like mapping needed.

>   * Regarding the code to `require` it, it looks like, [according to the
>     tutorial](https://fennel-lang.org/tutorial#modules-and-multiple-files),
>     that would be:
>
>     ~~~
>     (local fennel (require :fennel))
>     (table.insert (or package.loaders package.searchers) fennel.searcher)
>     (local my-module (require :lib.my-module))
>     ~~~

That is the code which teaches `require` how to load Fennel modules,
yeah. You can also perform ahead-of-time compilation so that your Fennel
gets turned into Lua code; then the default package.searchers will be
able to load it without doing that.

-Phil
Details
Message ID
<187dc4e8-2d27-4d07-8756-428c858f4edf@www.fastmail.com>
In-Reply-To
<878seo8y9c.fsf@hagelb.org> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
On Sat, Aug 8, 2020, at 8:21 PM, Phil Hagelberg wrote:
> John Gabriele <jgabriele@fastmail.fm> writes:
> 
> > Can you please give me a "Hello, Fennel Module!" example? That is:
> >
>
> Modules almost always end in a table; that table is then returned when
> you call (require :my-module). {snip}

Ok, I'm running Lua 5.4 with Fennel 0.5.0. Here's the files in my project
directory:

~~~
$ tree
.
├── lib
│   └── my-module.fnl
└── my-program.fnl
~~~

In lib/my-module.fnl:

~~~
(fn say-hi [s]
  (print (.. "Hi, " s "!")))

(fn add-em [a b]
  (+ a b))

;; This table is returned when you `require` the module.
{: say-hi
 : add-em }
~~~

and in my-program.fnl:

~~~
#!/usr/bin/env fennel

(local fennel (require :fennel))
(table.insert package.searchers fennel.searcher)
(local my-module (require :lib.my-module))

(require :lib.my-module)

(lib.my-module.say-hi "Module")

(print (lib.my-module.add-em 1 3))
~~~

After a `chmod +x my my-program.fnl`, I run it but get:

~~~
$ ./my-program.fnl 
Compile error in ./my-program.fnl:8
  unknown global in strict mode: lib


^^^^^^^^^^^^^^^^^^^^
* Try looking to see if there's a typo.
* Try using the _G table instead, eg. _G.lib if you really want a global.
* Try moving this code to somewhere that lib is in scope.
* Try binding lib as a local in the scope of this code.
~~~
Details
Message ID
<4c8a7ecf-e2ad-4322-a261-eb46a8b7d1d2@www.fastmail.com>
In-Reply-To
<187dc4e8-2d27-4d07-8756-428c858f4edf@www.fastmail.com> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
On Sun, Aug 9, 2020, at 2:33 AM, John Gabriele wrote:
> On Sat, Aug 8, 2020, at 8:21 PM, Phil Hagelberg wrote:
> > John Gabriele <jgabriele@fastmail.fm> writes:
> > 
> {snip}
> 
> and in my-program.fnl:
> 
> ~~~
> #!/usr/bin/env fennel
> 
> (local fennel (require :fennel))
> (table.insert package.searchers fennel.searcher)
> (local my-module (require :lib.my-module))
>
> (require :lib.my-module)

Ooof! There's my mistake; unnecessary second call to `require`, then
neglected to call like `(my-module.say-hi "Me")`.
Details
Message ID
<7896d9e9-50e5-4298-82c0-6fa6f2f441a9@www.fastmail.com>
In-Reply-To
<4c8a7ecf-e2ad-4322-a261-eb46a8b7d1d2@www.fastmail.com> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
> Ooof! There's my mistake; unnecessary second call to `require`, then
> neglected to call like `(my-module.say-hi "Me")`.

Put this into a wiki page:
https://github.com/bakpakin/Fennel/wiki/Cookbook-your-own-modules
Details
Message ID
<877du792ke.fsf@hagelb.org>
In-Reply-To
<7896d9e9-50e5-4298-82c0-6fa6f2f441a9@www.fastmail.com> (view parent)
DKIM signature
missing
Download raw message
John Gabriele <jgabriele@fastmail.fm> writes:

> Put this into a wiki page:
> https://github.com/bakpakin/Fennel/wiki/Cookbook-your-own-modules

I guess one thing I forgot to mention that might be a point of confusion
is that the package.searcher stuff is only necessary if you're launching
the program from Lua; either using the `lua` command or inside some
other system that supports Lua, such as Love2d. When you launch with the
`fennel` command from the shell, the package.searcher is already set for
you.

I'll see if I can make that clearer in the tutorial. But in the cookbook
entry you shouldn't need to set it.

-Phil
Details
Message ID
<dce92343-47ce-4e79-a353-8fd79cc7b36e@www.fastmail.com>
In-Reply-To
<877du792ke.fsf@hagelb.org> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
On Sun, Aug 9, 2020, at 1:00 PM, Phil Hagelberg wrote:
> John Gabriele <jgabriele@fastmail.fm> writes:
> 
> > Put this into a wiki page:
> > https://github.com/bakpakin/Fennel/wiki/Cookbook-your-own-modules
> 
> I guess one thing I forgot to mention that might be a point of confusion
> is that the package.searcher stuff is only necessary if you're launching
> the program from Lua; either using the `lua` command or inside some
> other system that supports Lua, such as Love2d. When you launch with the
> `fennel` command from the shell, the package.searcher is already set for
> you.
> 
> I'll see if I can make that clearer in the tutorial. But in the cookbook
> entry you shouldn't need to set it.

Great. Thanks.

Cookbook page updated. Fennel works very simply here!

-- John
Export thread (mbox)