+
Skip to content

Some form of iterative function for lists? #95

@tmhglnd

Description

@tmhglnd

While trying to code the Clapping Music piece from Steve Reich I noticed that Mercury actually lacks some method/function/way to allow creating a list iteratively.

The result of the code is currently this:

// the initial rhythm, played 8 times
list ptn copy([1 1 1 0 1 1 0 1 0 1 1 0] 8)

// the repetitions with shifts for every step
list ptn2 join(ptn rot(ptn -1) rot(ptn -2) rot(ptn -3) rot(ptn -4) rot(ptn -5) rot(ptn -6) rot(ptn -7) rot(ptn -8) rot(ptn -9) rot(ptn -10) rot(ptn -11))

It is clear there is lots of repetition needed in order to be able to rotate the pattern after every 8 duplicates. It would be interesting to see if there is a way to do this without the repetition of the code. For example providing a list on a function argument that would usually only accept a single number could make that function perform it multiple times:

list ptn copy([1 1 1 0 1 1 0 1 0 1 1 0] 8)

list ptn2 join(ptn rot(ptn [-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11]))

This could be written even shorter, by using the spreadInc() method:

list ptn copy([1 1 1 0 1 1 0 1 0 1 1 0] 8)

list ptn2 rot(ptn spreadInc(12 0 -11))

Another option is that there is some kind of for() or iterate()/iter() method, that performance a function iteratively by putting the value from another list as argument. For example:

list ptn copy([1 1 1 0 1 1 0 1 0 1 1 0] 8)

// performance the function rot() multiple times for every value of the first list
// using i as a placeholder for the value from that list
list ptn2 iter([0 -1 -2 -3 ... -11] rot(ptn i)) 

The current result of coding the Clapping Music:

https://mercury-playground.pages.dev/#Ly8gQ2xhcHBpbmcgTXVzaWMgLSBTdGV2ZSBSZWljaCAoMTk3MikKLy8gQW4gYWxnb3JpdGhtaWMgbXVzaWMgcGllY2Ugd2hlcmUgMiBjbGFwcGluZyBwZXJmb3JtZXJzIHN0YXJ0IHdpdGggdGhlIHNhbWUgcmh5dGhtLgovLyBBZnRlciA4IHJlcGV0aXRpb25zIG9mIHRoZSByaHl0aG0gb25lIHBlcmZvcm1lciBzaGlmdHMgdGhlIHBhdHRlcm4gYnkgMSBzdGVwLAovLyB3aGlsZSB0aGV5IG90aGVyIHBlcmZvcm1lciBjb250aW51ZXMgcGxheWluZyB0aGUgb3JpZ2luYWwgcmh5dGhtLgovLyBFdmVyeSA4IHJlcGV0aXRpb25zIHRoZSByaHl0aG0gc2hpZnRzIGJ5IDEsIHVudGlsbCBpbiB0aGUgZW5kIHRoZXkgbGluZSBiYWNrIHVwCgpzZXQgdGVtcG8gMTcwCgovLyB0aGUgaW5pdGlhbCByaHl0aG0sIHBsYXllZCA4IHRpbWVzCmxpc3QgcHRuIGR1cGxpY2F0ZShbMSAxIDEgMCAxIDEgMCAxIDAgMSAxIDBdIDgpCgovLyB0aGUgcmVwZXRpdGlvbnMgd2l0aCBzaGlmdHMgZm9yIGV2ZXJ5IHN0ZXAKbGlzdCBwdG4yIGpvaW4ocHRuIHJvdChwdG4gLTEpIHJvdChwdG4gLTIpIHJvdChwdG4gLTMpIHJvdChwdG4gLTQpIHJvdChwdG4gLTUpIHJvdChwdG4gLTYpIHJvdChwdG4gLTcpIHJvdChwdG4gLTgpIHJvdChwdG4gLTkpIHJvdChwdG4gLTEwKSByb3QocHRuIC0xMSkpCgovLyBwbGF5IGJvdGggcmh5dGhtcywgb25lIG9uIHRoZSBsZWZ0IHNwZWFrZXIgYW5kIG9uZSBvbiB0aGUgcmlnaHQKbmV3IHNhbXBsZSBjbGFwXzkwOSB0aW1lKDEvOCkgcGxheShwdG4pIHBhbigtMC41KSBuYW1lKHBsYXllcjEpCm5ldyBzYW1wbGUgY2xhcF84MDggdGltZSgxLzgpIHBsYXkocHRuMikgcGFuKDAuNSkgbmFtZShwbGF5ZXIyKQoKLy8gc29tZSByZXZlcmIgb24gYm90aCBzb3VuZHMKc2V0IGFsbCBmeChyZXZlcmIgMC4zIDIp

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载