Ask Your Question
1

How and where do I define and use custom functions?

asked 2016-06-19 22:45:46 -0600

kindohm gravatar image

updated 2016-06-20 08:26:23 -0600

Let's say I have a crazy idea for a new pattern transformer, or maybe I just want to create a shortcut for a typical chain of transformers that I use a lot. How do I create a custom function to do this? Where do I type this code, and how do I make sure that the function is loaded before using it?

Example. Instead of typing this:

d1 $ every 2 (rev . stut 4 0.25 0.125) $ s "bd cp bd*2 cp"

I want to type this:

d1 $ every 2 (revstut) $ s "bd cp bd*2 cp"
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2016-06-19 23:12:46 -0600

lvm gravatar image

Hi!

How:
I might be wrong, but to define a "function", you can do something like:

let revstut = rev . stut 4 0.25 0.125

Or a bit more complex example:

let revstut sN = rev . stut sN 0.25 0.125

then you could do

d1 $ every 2 (slowspread revstut [2, 4, 8]) $ s "bd cp bd*2 cp"

with 2, 4, 8 which being applied to revstut sN (ie: rev . stut 2 0.25 0.125, rev . stut 4 0.25 0.125, ...)

BTW, you can have a group of functions by using one let ...:

let revstut sN = rev . stut sN 0.25 0.125
    revstut' = revstut 2

So this way you have both, one with a default value (revstut') and one where you can pass an Int param (revstut sN)

disclaimer: Wrote this without testing, but IIRC this should work fine :-)

Where:

That's another subject. Sometimes I add them at the top of the .tidal file I'm working on, sometimes on a custom .tidal file which I evatuate after start coding, I'm not really sure which is the best practice for this.
OTOH, if you use emacs, and have your custom .tidal file with your functions you can do something like the author of TideBox did in these lines (he evaluates automagically a file when the editor starts).

Hope this helps!

edit flag offensive delete link more

Comments

Yep this works. I think there is a way to put functions in the .ghci file so that they're loaded when Tidal boots, but I've struggled to get it to work for some reason. The .tidal file method is easy enough.

kindohm gravatar imagekindohm ( 2016-06-20 09:36:30 -0600 )edit
2

answered 2016-06-20 11:01:36 -0600

bgold gravatar image

updated 2016-06-20 11:03:15 -0600

There are a few options, but I haven't found a perfect way.

I think using the .ghci file has problems because that file will get executed before anything else from Tidal gets loaded, so I don't think it works too well.

In GHCi you can execute commands from a file using :script. The problem I run into with this is that GHCi isn't in "multiline" mode by default, so it might give errors that you wouldn't see if you just sent the block from your Atom session (which has some special code to deal with such things). You can :set +m to get around some of it, but there can still be some issues.

You can write up your functions as a more permanent module, and load that using :load. And you could add a line to BootTidal.hs to automatically load it upon starting Tidal. The trick with this is that you need to write your functions as "normal" Haskell, not the interactive form. So you don't need let, and there are some syntax differences. This can be nice if you hope to merge your functions into core Tidal, or need to use some other external Haskell modules, but can be a bit of work to set up.

In practice, I used to do the :load thing, but it's a bit of extra work and nowadays I just have a snippets.tidal file with a giant let block that I execute by hand whenever I start Tidal.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2016-06-19 22:45:46 -0600

Seen: 116 times

Last updated: Jun 20 '16