Skip to content

Instantly share code, notes, and snippets.

@YujiSODE
Created April 7, 2018 01:30
Show Gist options
  • Save YujiSODE/b4bdf10ab7f6700a55baf7acfb351977 to your computer and use it in GitHub Desktop.
Save YujiSODE/b4bdf10ab7f6700a55baf7acfb351977 to your computer and use it in GitHub Desktop.
It outputs simplified JavaScript implementation of sample estimated random variable generator by "regLines.tcl" using bootstrap method (Efron,1979).
#linesvar_toJavaScript.tcl
##===================================================================
# Copyright (c) 2018 Yuji SODE <yuji.sode@gmail.com>
#
# This software is released under the MIT License.
##===================================================================
#It outputs simplified JavaScript implementation of sample estimated random variable generator by "regLines.tcl" using bootstrap method (Efron,1979).
#=== Synopsis ===
#linesvar_toJavaScript src ?sampling ?name ?encoding???;
#
# - $src: file path of a script generated by "regLines.tcl" to simplify
# - $sampling: an optional sampling size that is 10 or more
# 100 is default value
# - $name: an optional function name
# default name is "linesVar"
# - $encoding: an optional encoding name to output
# default value is utf-8
#
#output filename has format of "${name}_${sampling}.js"
#=== References ===
# - Efron, B. 1979. Bootstrap Methods: Another Look at the Jackknife. Ann. Statist. vol. 7, no. 1, p. 1-26.
#=== Library list ===
# - regLines/regLines.tcl (Yuji SODE, 2018): the MIT License; https://github.com/YujiSODE/regLines
##===================================================================
#output is implementation of function: linesVar() in Tcl to Tcl
proc linesvar_toJavaScript {src {sampling 100} {name linesvar} {encoding utf-8}} {
source -encoding utf-8 $src;
set sampling [expr {$sampling<10?10:int($sampling)}];
set v {};
set script "//additional Math method for JavaScript\n";
set i 0;
while {$i<$sampling} {
lappend v [expr {linesVar()}];
incr i 1;
};
append script "//Math.$name\(\) returns sample estimated random variable using bootstrap method\n";
append script "Math.$name=function\(\)\{return \[[join $v ,]\]\[Math.floor\(Math.random\(\)*$sampling\)\]\;\}\;";
set C [open "${name}_${sampling}.js" w];
fconfigure $C -encoding $encoding;
puts -nonewline $C $script;
close $C;unset v script;
return "${name}_${sampling}.js";
};
#*** License ***
#MIT License
#
#Copyright (c) 2018 Yuji Sode
#
#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all
#copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment