Lilypond function; add a rhythm to a note - function

How can you write a Lilypond function that takes in a note and outputs a note with a rhythm? say:
input: c'
output: c'8 c'16 c'

In the LilyPond documentation you can find this example:
rhythm =
#(define-music-function (parser location p) (ly:pitch?)
"Make the rhythm in Mars (the Planets) at the given pitch"
#{ \tuplet 3/2 { $p 8 $p $p } $p 4 $p $p 8 $p $p 4 #})
\new Staff {
\time 5/4
\rhythm c'
\rhythm c''
\rhythm g
}
Hopefully that can be adapted to do what you want! Replace the Mars rhythm with your own. And please note that a space is needed between the variable $p and the durations.

Related

meaning of $value in function formal parameters

I'm looking at the JQ "builtin.jq" file, and find
def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));
and am trying to figure out the semantics of "$value" as a formal parameter. It could mean that the parameter is expected to provide only one value, not a list of them. Or it could be the same as
def _assign(paths; vv): vv as $value | reduce path(paths) as $p (.; setpath($p; $value));
or maybe it's something else?
I can't find anything in the documentation about this kind of function formal-parameter.
You're right about def _assign(paths; vv): vv as $value ...
In essence, a formal parameter, $x, is equivalent to having x in the formal parameter list, followed by x as $x shortly thereafter.
This is briefly mentioned in the jq manual:
Or use the short-hand:
def addvalue($f): ...;
What is not mentioned is that, using this example, f can also be used in the body of addvalue, though doing so might easily be the source of confusion. For example, what result would you expect the following to produce?
echo 1 2 3 | jq -n 'def f($x): x+x; f(input)'

print dictionary keys and values in one column in tcl

I am new learner of tcl scripting language. I am using TCL version 8.5. I read text file through tcl script and count similar words frequency. I used for loop and dictionary to count similar words and their frequency but output of the program print like this: alpha 4 beta 2 gamma 1 delta 1
But I want to print it in one column each key, value pair of dictionary or we could say each key, value pair print line by line in output. Following is my script in tcl and its output at the end.
set f [open input.txt]
set text [read $f]
foreach word [split $text] {
dict incr words $word
}
puts $words
Output of the above script:
alpha 4 beta 2 gamma 1 delta 1
You would do:
dict for {key value} $words {
puts "$key $value"
}
When reading the dict documentation, take care about which subcommands require a dictionaryVariable (like dict incr) and which require a dictionaryValue (like dict for)
For nice formatting, as suggested by Donal, here's a very terse method:
set maxWid [tcl::mathfunc::max {*}[lmap w [dict keys $words] {string length $w}]]
dict for {word count} $words {puts [format "%-*s = %s" $maxWid $word $count]}
Or, look at the source code for the parray command for further inspiration:
parray tcl_platform ;# to load the proc
info body parray

Zip lists in jq's objects construction by {} instead of multiplying them like default

A JSON object like this:
{"user":"stedolan","titles":["JQ Primer", "More JQ"],"years":[2013, 2016]}
And, I want to convert it with lists(assume all lists have equal length N) zipped and output like this:
{"user":"stedolan","title":"JQ Primer","year":2013}
{"user":"stedolan","title":"More JQ","year":2016}
I followed Object - {} example and tried:
tmp='{"user":"stedolan","titles":["JQ Primer", "More JQ"],"years":[2013, 2016]}'
echo $tmp | jq '{user, title: .titles[], year: .years[]}'
then it output:
{"user":"stedolan","title":"JQ Primer","year":2013}
{"user":"stedolan","title":"JQ Primer","year":2016}
{"user":"stedolan","title":"More JQ","year":2013}
{"user":"stedolan","title":"More JQ","year":2016}
It produces N*N ... lines result, instead of N lines result.
Any suggestion is appreciated!
transpose/0 can be used to effectively zip the values together. And the nice thing about the way assignments work is that it can be assigned simultaneously over multiple variables.
([.titles,.years]|transpose[]) as [$title,$year] | {user,$title,$year}
If you want the results in an array rather than a stream, just wrap it all in [].
https://jqplay.org/s/ZIFU5gBnZ7
For a jq 1.4 compatible version, you'll have to rewrite it to not use destructuring but you could use the same transpose/0 implementation from the builtins.
transpose/0:
def transpose:
if . == [] then []
else . as $in
| (map(length) | max) as $max
| length as $length
| reduce range(0; $max) as $j
([]; . + [reduce range(0;$length) as $i ([]; . + [ $in[$i][$j] ] )] )
end;
Here's an alternative implementation that I cooked up that should also be compatible. :)
def transpose2:
length as $cols
| (map(length) | max) as $rows
| [range(0;$rows) as $r | [.[range(0;$cols)][$r]]];
([.titles,.years]|transpose[]) as $p | {user,title:$p[0],year:$p[1]}
If you want the output to have the keys in the order indicated in the Q, then the solution is a bit trickier than would otherwise be the case.
Here's one way to retain the order:
with_entries( .key |= (if . == "titles" then "title" elif . == "years" then "year" else . end) )
| range(0; .title|length) as $i
| .title |= .[$i]
| .year |= .[$i]
The (potential) advantage of this approach is that one does not have to mention any of the other keys.

How to escape special characters while using lsearch?

How to escape special characters(e.g "[]") while using search?
Consider the following scenario:
>> set L { a b c [] }
>> a b c []
>> lsearch $L b
>> 1
>> lsearch $L "[]"
>> -1
I'm looking to get 3 when I run lsearch $L "[]"
When looking for fixed strings rather than patterns, it is easiest to use the -exact option to lsearch. You also need to make sure Tcl doesn't do substitution on the search string, for example by enclosing it inside curly braces. Otherwise you'll tell Tcl to look for an empty string (the result of executing an empty command string):
lsearch -exact $L {[]}

Tcl 8.4 - Plotting a bar graph

I wanted to create a histogram (Look more like a bar chart) of a table with 5 rows and 2 columns, each of the column being an x-axis and y-axis.
I am new to TCL, and before posting here I have tried multiple things! I found that there is a package called plot chart, downloaded form the wiki and tried!
#! /bin/env tclsh
lappend auto_path /u/vbhaskar/work/proj2/ECE_x81_PROJECT_2
package require plotchart
canvas .c -background white -width 400 -height 200
pack .c -fill both
#
# Create the plot with its x- and y-axes
#
set s [::Plotchart::createXYPlot .c {0.0 100.0 10.0} {0.0 100.0 20.0}]
foreach {x y} {0.0 32.0 10.0 50.0 25.0 60.0 78.0 11.0 } {
$s plot series1 $x $y
}
$s title "Data series"
exit 0
Tried the above code, didn't understand completely but better.
I get the error saying package not found.
Is there a simple way of achieving my functionality in Tcl 8.4 version ? I am doing source <file_name.tcl> in the Synopsys DC shell
I will not use any library, but some simple code.
set mylist [list 0 1 2 8 3 9 4 2 5 19 6 12]
array set arr $mylist
#parray arr
foreach element [array names arr] {
puts -nonewline "x($element) "
for {set i 0} {$i <= $arr($element)} {incr i} {
puts -nonewline "-"
}
puts -nonewline "$arr($element)"
puts "\n"
}
Check the o/p:
x(4) ---2
x(0) --1
x(5) --------------------19
x(6) -------------12
x(2) ---------8
x(3) ----------9
Hope it helps.