One Line Procedures Follow-up and Templates V2 and TCL 8.6 demo examples calculator, numerical analysis

One Line Procedures Follow-up and Templates V2 and TCL 8.6 demo examples calculator, numerical analysis

This page is under development. Comments are welcome, but please load any comments in the comments section at the bottom of the page. Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Aside from your courtesy, your wiki MONIKER and date as a signature and minimal good faith of any internet post are the rules of this TCL-WIKI. Its very hard to reply reasonably without some background of the correspondent on his WIKI bio page. Thanks, gold 20Feb2024


The author is retired engineer on Windows 1O and no longer has { proofing access } to Unix machines. Unix is respected after use of so many years in engineering, but my wings are lost.


Note. I did find a useful online IDE, https://www.jdoodle.com/execute-tcl-online/ . I can paste from a text file, edit, and run an output using this online IDE.


Preface

gold 20Sep2024. Here are some follow-up on one line procedures.


Introduction



Not a Replacement for TCL Core


This page on developing one line procedures is not a replacement for the current Tcl core and Tcllib, which is much improved since Tcl version 4, and other <faster> language constructs. math ops, Tcllib routines, and other compiled routines can reduce the cost of big-data tasks by about 1/3. The time savings of the core are not always obvious on small quantities of data, like 4 or 5 numbers. Performance of one-line programs may suffer degradation due to lengthy recursion calls, and may be limited by constraints on recursion. Dependence on math operator notation, helper procedures, math check examples, degradation due to lengthy recursion calls, and special library functions should be noted in the comment lines.



Tool Control Language TCL is a powerful and versatile language


Tool Control Language (TCL) is a programming language used in automation, especially in industrial automation and manufacturing. It is designed to control various tools, machines, and processes, allowing for precise and efficient execution of tasks. TCL is based on the IEC 61131 standard, which is an international standard for programming languages used in industrial automation. It is a high-level, text-based language that provides a clear and concise way to describe actions and sequences, making it easier for automation engineers to develop and maintain control programs. TCL can be used to program a wide range of devices and systems, including PLCs (Programmable Logic Controllers), robots, CNC machines, and other industrial equipment. By using TCL, manufacturers can optimize their production processes, reduce human error, and improve overall efficiency and productivity. Some key features of TCL include:


1. Structured programming: TCL supports structured programming concepts, such as IF-THEN-ELSE statements, FOR and WHILE loops, and functions, which enable the creation of modular and reusable code.


2. Data types: TCL supports various data types, including integers, floats, strings, and arrays, allowing for the manipulation and storage of different types of data.


3. Input/Output operations: TCL provides functions for reading and writing data from/to various sources, such as sensors, actuators, and data storage devices.


4. Timers and counters: TCL includes functions for creating timers and counters, which can be used to control the timing of actions and sequences.


5. User-defined functions: TCL allows users to create their own functions, which can be used to encapsulate and reuse common code sequences.


Overall, TCL is a powerful and versatile language for controlling industrial tools and processes, making it an essential tool for automation engineers and manufacturers.


Start with a working TCL math template or TCL math subroutine to modify it


To start with a working TCL math template and modify it for a new purpose, follow these 6 steps:


1. Choose a TCL math template or TCL math subroutine, for example in the TCLLIB library: Find a suitable TCL math template that closely matches the functionality you want to achieve. You can find various templates online or in TCL programming books and resources.


2. Understand the template: Carefully read and understand the template's code, comments, and structure. This will help you identify the parts of the code that you need to modify to create your new purpose.


3. Identify the necessary modifications: Determine the specific changes you need to make to the template to achieve your desired functionality. This may include modifying input parameters, changing calculations, or altering the output format.


4. Modify the template code: Begin modifying the template code, following the guidelines provided by the TCL programming language. Make sure to update any comments or documentation to reflect the new purpose of the subroutine.


5. Test the new TCL subroutine: After making the necessary modifications, test the new TCL subroutine in a console program to ensure it functions as expected. This will help you identify and fix any errors or issues before integrating the subroutine into a graphical user interface (GUI).


6. Load the new TCL subroutine into a GUI: Once the new TCL subroutine has been tested and verified, you can load it into a GUI program that supports TCL scripting. Make sure to follow the GUI's documentation and guidelines for integrating TCL subroutines.


By following these steps, you can successfully start with a working TCL math template, modify it for a new purpose, and then load the new subroutine into a GUI program for further use.


Still Proof Reading Here


One Liner Programs &

1. Simple One Liner Procedure


# One Liner Procedure
proc greetings {} {  puts "Hello World!"}
greetings

This is a basic example of a one liner procedure in TCL. The procedure definition is done on a single line, and it simply prints out the message "Hello World!" when called.


2. Random One Liner Procedure

# One Liner Procedure
proc random_number {} {  expr {rand() * 100}}
puts [random_number]

This one liner procedure generates a random number between 0 and 100 and prints it out. The random number is generated using the rand() function and multiplied by 100.


3.Factorial One Liner Procedure

# One Liner Procedure
proc factorial n {  expr {$n == 0 ? 1 : $n * [factorial [expr {$n - 1}]]}}
puts [factorial 5]

This one liner procedure calculates the factorial of a given number. It uses a recursive approach to calculate the factorial, starting with the given number and multiplying it by the factorial of the number minus 1 until it reaches 0.


4. Prime Number One Liner Procedure

# One Liner Procedure
proc is_prime n {  for {set i 2} {$i <= sqrt($n)} {incr i} {    if {$n % $i == 0} {      return 0    }  }  return 1}
puts [is_prime 17]

This one liner procedure checks if a given number is prime. It uses a for loop to iterate from 2 to the square root of the given number, checking if the number is divisible by any of the iterated numbers. If it is divisible, it returns 0 (false), indicating that the number is not prime. If the loop completes without finding a divisor, it returns 1 (true), indicating that the number is prime.


5.Text Search One Liner Procedure

# One Liner Procedure
proc search_text text needle {  if {[string first $needle $text] != -1} { puts "Found $needle in $text"  }}
search_text "Hello World!" "World"

This one liner procedure searches for a specified substring (needle) within a given text. It uses the string first command to find the index of the first occurrence of the needle in the text. If the needle is found, it prints out a message indicating that it was found in the text. These one liner procedures demonstrate the simplicity and conciseness of TCL programming, while still being able to perform useful tasks.



Approach to One Line Procedures


There is a gold mine of One Liners Programs and content in the Tool Control language TCL 8.6 core distribution, TCL manual pages, and TCLLIB library that can be adapted or recast into brief one liners programs. These one liners programs or procedures can be pasted into the TCL 8.6 console window for quick results, reference the TCL Wiki. Some one liners programs use the return statement, return $value, or return 1 to return the results to the program line. Although many TCL programmers just rely on the last computation being returned by the one liner procedure in the TCL console window.


There is some room in the Wiki publications for programming style differences. But it is usually best to put usage, credits, or TCL documentation references for the one liners procedures on separate comment lines. The random rand procedures make use of the random function and do not return the same answer every time. Dependence on math operator notation, helper procedures, math check examples, and special library functions should be noted in the comment lines. The terms program, routine, subroutine, procedure, and proc are used interchangeably in this article. Recognize that the TCL nomenclature uses the exceptional term procedure and proc, but the internet search engines and general public do not accept or pull the terms procedure and proc as equitably with respect to the other computer languages using terms subroutine and program. There are pros and cons to one liner programs in TCL.


One may contrast the approach to one liners programs in problem solving versus the traditional procedural approach. There are better routines and methods in faster language constructs in the current TCL core distribution and TCLLIB. Working with recursion, primes, text search, and timing the procedures will quickly show the warts on the one liners programs. To gain speed and shorter computation times, one will generally have to access the TCL core distribution and TCLLIB. Since the TCL interpreter collapses the carriage returns, skips, blank lines, and dead space of traditional written procedures into a single line of machine code, is not every script a one liner program to the parser? As grandfather remarked, the gourmet omelet, beef mulligan stew, and farm buttermilk all go to the same place.


References:


  • Wikipedia search engine < diabetes insulin resistance >
  • Wikipedia search engine < Richard K. Bernstein >
  • Wikipedia search engine < Programming Examples >
  • Google search engine < vaporware >
  • One Liners Programs Pie in the Sky
  • One Liners
  • One Liners Programs Compendium [L1 ]
  • WIKI BOOKS, Programming_Examples pdf
  • WIKI BOOKS, Tcl_Programming_Introduction pdf
  • Note. I did find a useful online IDE, https://www.jdoodle.com/execute-tcl-online/ .
  • Note. I can paste, edit, and run an output using this online IDE.

Testcases Section

In planning any software, it is advisable to gather a number of testcases to check the results of the program.


Testcase 1,





Testcase 2,


Example calculation


Testcase 3,


Example calculation



Testcase 4, Protocol



Screenshots Section


figure 1. Screenshot,





Appendix TCL programs and scripts *

Pretty Print Version


        #  pretty print from autoindent and ased editor
 


Conversion unit formulas in one line TCL procs



    #  conversion unit formulas in one line TCL procs
    #  HOMA-IR standing for Homeostatic Model Assessment of Insulin Resistance 
    #  HOMA_IR = expr { $insulin * $glucose * $scale_factor}
    #  fasting blood insulin in units  uIU/mL
    #  fasting blood glucose in units mg/dL
    proc HOMA_IR2 { insulin  glucose }  {
            set scale_factor [ expr { 1. / 405. } ]
            return [expr { $insulin * $glucose * $scale_factor}]}
    #  HOMA-IR calculations here requires U.S. standard units.
    #  European SI units as best understood. 
    #  To convert component terms of  HOMA-IR ( $insulin & $glucose)
    #  from international S.I. units:
    #  Insulin: pmol/L to uIU/mL, divide by (÷) 6
    #  Glucose: mmol/L to mg/dL, multiply by (x) 18
    #  hbA1c_convert_to_average_blood_glucose mg/dl  HbA1c
    #  HbA1c test is a simple blood test that measures your 
    #  average blood sugar levels over the past 3 months.
    #  As a peg point, 5 HbA1c units converts to 100 mg/dl,
    #  mg/dl is abbreviation for milligrams per deciliter.
    proc a1c hbA1c {  expr { 35.6*$hbA1c -77.3} }
    #  convert mg/dl to mmol/L average blood glucose
    #   European SI units conversion on blood glucose
    #  some papers round off 18.016 to mgdl/18.
    proc mgdl_to_mmoll mgdl {  expr { $mgdl/18.0 } }
    #  convert  mmol/L to mg/dl average blood glucose
    proc mmoll_to_mgdl mmoll {  expr { $mmoll*18.0 } }
    #  formula QUICKI_index insulin resistance = 1/(log(insulin) + log(glucose)), decimal logs 
    proc QUICKI_INDEX_IR { insulin  glucose } { return [ expr { 1./(log10($insulin) + log10($glucose))} ] }
    #  Usage set answer [  QUICKI_INDEX_IR 4. 100. ] #  eval 0.38


Hidden Comments Section

Please include your wiki MONIKER and date in your comment with the same courtesy that I will give you. Thanks, gold 20Feb2024