rlwhitcomb / utilities Goto Github PK
View Code? Open in Web Editor NEWSome of my personal utility programs
License: MIT License
Some of my personal utility programs
License: MIT License
Would like to be able to do this:
def f1($a, $b) = { $a + $b }; def f2($a, $b) = { $a * $b }; def f($c) = { $c ? f1 : f2 }; f(true)(1,2)
which at the moment gives the following exception:
Exception in thread "main" java.lang.ClassCastException: class info.rlwhitcomb.calc.FunctionScope cannot be cast to class info.rlwhitcomb.calc.FunctionDeclaration (info.rlwhitcomb.calc.FunctionScope and info.rlwhitcomb.calc.FunctionDeclaration are in unnamed module of loader 'app')
at info.rlwhitcomb.calc.LValueContext.getLValue(LValueContext.java:417)
at info.rlwhitcomb.calc.CalcObjectVisitor.getLValue(CalcObjectVisitor.java:3872)
at info.rlwhitcomb.calc.CalcObjectVisitor.visitVarExpr(CalcObjectVisitor.java:1906)
...
Right now the manual process is to download the .zip file, run a small Ant script to unzip, then manually edit the build.properties with the latest git revision (short form), possibly update "release.build" to "true" and then run "ant update" or "ant all-install". This is kind of tedious, and possibly there is a way to automate most or all of this process (can we do "wget" or "curl" to download the .zip file?)
Right now you have to specify "@,d" or "@,%" to get separators. It would be nice to have directive and command line option (so it could be set via CALC_OPTIONS) to always do separators (and an option [default] to not do so).
The errors look like:
"stty: 'standard input': Inappropriate ioctl for device"
So, we should detect this, redirect stderr, or something to eliminate the ugly errors.
And maybe the Locale listing should have a verbose option? Or at least list the relevant pieces of Locale information available in the locale (things like currencies).
So that you could find "abc*.bat" for instance.
Instead of having to use an external editor to do so. This begs the question of how to get the changes back into the source code, or where the edited file would reside, or how to load it back again .... Something to think about. Or maybe it is just a development tool to make it easier to do the editing, and not really part of the WordFind GUI itself...
Some possible default extensions are: ".ex", ".ca", ".expr", and ".calc"
Trying to implement "$" formatting in Calc is not giving me the expected currency character, MAYBE because I'm not setting the default Locale correctly ("-loc en-US" isn't working in the unit tests). Suggest using Locale.Builder.setLanguageTag instead of the current method, pending further testing.
Probably should use the locale settings for this.
If you want to do "length(array) - 1" it will complain about "unable to convert array type to decimal", whereas "(length array) - 1" works right.... This shouldn't be, and previous attempts to rearrange the whole presence table has not helped. Maybe "( expr )" should be after the functions, I don't know. Just know it's weird. Or maybe the parens have to be mandatory on function calls, no matter what.
It's awkward to Copy/Paste/Save the results using some other editor...
Right now d'50-01-01' would be 1950, while d'49-01-01' would be 2049:
> d'50/01/01'@e
d'50/01/01' @e -> d'1950-01-01'
> d'49/01/01'@e
d'49/01/01' @e -> d'2049-01-01'
>
This proposal would change the split point to 30 years in the future, so (as of 2021) D'1/1/51' would be 2051, while D'1/1/52' would be 1952.
For example the min and max dates are "-9999-01-01" to "9999-12-31". Taking the difference results in 7,304,483 days, multiplied by nanosperday (of 86,400,000,000,000) gives a duration value of 631,107,331,200,000,000,000 which is quite a bit larger than the max long of 9,223,372,036,854,775,807, so converting to long before calling NumericUtil.convertToDuration is just wrong...
And in fact, that duration gives "dur@dt" -> t'45347.60062355607703703703703703704d' instead of the correct value of t'7304483d'
For instance, "abc"@x -> "616263" ... which isn't a valid input value which will reconstruct the original value ...
So, possible options are:
Here are the relevant options from the BSD man page:
NAME
uniq -- report or filter out repeated lines in a file
SYNOPSIS
uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]]
DESCRIPTION
The uniq utility reads the specified input_file comparing adjacent lines, and writes a copy of each unique input line to the
output_file. If input_file is a single dash (`-') or absent, the standard input is read. If output_file is absent, standard out-
put is used for output. The second and succeeding copies of identical adjacent input lines are not written. Repeated lines in
the input will not be detected if they are not adjacent, so it may be necessary to sort the files first.
The following options are available:
-c Precede each output line with the count of the number of times the line occurred in the input, followed by a single space.
-d Only output lines that are repeated in the input.
-f num Ignore the first num fields in each input line when doing comparisons. A field is a string of non-blank characters sepa-
rated from adjacent fields by blanks. Field numbers are one based, i.e., the first field is field one.
-s chars
Ignore the first chars characters in each input line when doing comparisons. If specified in conjunction with the -f
option, the first chars characters after the first num fields will be ignored. Character numbers are one based, i.e., the
first character is character one.
-u Only output lines that are not repeated in the input.
-i Case insensitive comparison of lines.
The grammar is all there in Calc.g4, and there are TODO-type comments, but the implementation is not done yet.
In addition, the library functions in "test/files" do not properly declare nor use parameters as they should (once this is finished, of course).
Normally the Calc REPL mode takes one line of input and executes it, however, sometimes it would be useful to (for instance) define a function over multiple lines, or continue a long line of input on the following lines for readability. So, it would be nice to implement the ability to end a line with \ (or some continuation character) to allow the input to continue to the following lines. In the GUI or in a file this is already possible without any special support.
Here's a fail case:
def a={loop $a in 10 { $a }}; a; eval a;
will produce:
Defining function 'a' = { loop $a in 10 { $a } }
a -> 10
$a -> 1
$a -> 2
$a -> 3
$a -> 4
$a -> 5
$a -> 6
$a -> 7
$a -> 8
$a -> 9
$a -> 10
eval a -> 10
So, just invoking the function "a" produces the last value from the loop, and that's it. But, doing "eval a" also does the output from each loop iteration, as well as the final result. This seems weird.
A lot of code is there, ported from "C", but it doesn't really do anything at the moment.
Could probably use rewriting a lot because C-like constructs aren't very useful for Java.
-time is a synonym for both -timing and -maxtime (help is actually consistent with the code, but the option is duplicated)
There may be others
Works on strings at least. Not sure about arrays (of bytes). So, needs further definition.
Something like "hash < object > (, algorithm)" where "algorithm" is any expression resulting in a string value (compared case-insensitive) to one of the supported hash algorithms. Result would be the (normal) hex encoding of the bytes of the final hash digest. Using the methods in SecurityUtil we could also add "obfuscate" / "deobfuscate" to the mix.
Something like:
loop expr; expr; expr { block }
where expr1 would execute once at the beginning, expr2 is a controlling boolean expression, and expr3 is executed every time through the loop
Unsure whether this use of ';' would conflict with ENDEXPR, or how $VAR would work into this (maybe loop $VAR in expr;expr;expr...
would still work, but what would the value be???
There is an Antlr v4 grammar now in PreProc.g4, which could/should be incorporated into the main processing loop in "processFile".
The grammar is currently commented as "not quite complete", so we should finish it as we see things in the Java code that are not yet incorporated into the grammar.
Then "loop $I in map" would return keys instead of values, so that map[$I] would work to get the values.
Right now you can read in a JSON file to Calc and then edit the data to add the @j format to pretty print it. It would be nice to have that as an option on the command line, so that it could be scripted.
Note: the grammar is not quite right yet to be able to read in a multi-line JSON file and have it work. Could use "lists" to put into one line, however.
Not sure if this will work, syntax-wise, but I would like to be able to (at least) use the built-in names for variables, even though it would be confusing (at best) to redefine a built-in function with a user-defined one.
Now that JDK 17 is GA
This will do essentially the same things as "splice" does for objects and arrays, but with strings (and by extension numbers converted to strings).
Syntax: a = replace original_string, pattern, replacement [, options ]
where options can be missing, "all", "first", or "last"
corresponding to Java String.replaceAll, String.replaceFirst, and new functionality for "last".
These new keywords will be treated the same way as the mode option keywords ("on", "off", etc.) as valid identifiers in their own right.
The calculation is quite easy to do (repeated multiplications of numerator/denominator). Then Calc could be updated to use the new function.
gmt --PDT
for instance, should give something like:
Wed Jun 9,2021 12:31:26.066 PDT
It is unclear how to do the parsing / selection of timezones, since there are multiple ways to name them (see https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/time/ZoneId.html for some discussion about it).
For an array the correct elements are removed from the original array, but if the origin is an object, the elements are arranged into an array, and the removed elements returned are correct (in order of definition), but the original object is not modified (as per the spec). For instance, this illustrates the current behavior for an object:
c = {a: 1, b: 2, c: 3} -> { a: 1, b: 2, c: 3 }; splice c, 1, 1 -> [ 2 ]; c -> { a: 1, b: 2, c: 3 }
I find I have to specify "-dir:test/files" and "-log" every time I manually run tester. It would be nice if these more-or-less constant options could be specified in an environment variable (as CALC_OPTIONS, WORDFIND_OPTIONS, etc.) so that I don't have to type them every time.
There should be an option to ignore the previously set options also so that I could override them without unsetting (and then resetting) the env var.
Then it would need "-locale" option also and translations.
In ISO-8601 form, d'-9999/01/01' works right, and looks right. However, D'-01/01/9999' works but looks funny (b/c only the year is actually negative, not the month). So, we should be using "D'01/01/-9999' and correspondingly with @e. Although I suppose there is no harm in allowing the "-" first on input, the @e output should have the "-" next to the year.
There is a NumberFormat.getPercentInstance method in Java which could / should be used in preference to rolling our own.
Array of bytes would probably be very inefficient (memory-wise) using ArrayScope, so we might need a different object to store this, but then would need more logic in LValueContext to access these via [...] notation, as well as special logic to format, and create elsewhere ... a whole host of things to think about here.
Possible read/write in CSV format as well (with all the CSVFormat options we have available).
For example: a=[1,2,3]; join a -> "132", since each of the values is treated as if it were listed individually (as in "join 1,2,3"), so the 3rd value is the "glue" value...
It would be better if with one parameter, which is an array or object, the values are simply concatenated, so that join a -> "123". An optional second parameter would be the "glue" value, so that join a, ',' -> "1,2,3" as you would kind of expect.
The other tricky thing is that the order of the values in an object (map) is undefined (based on a hash function), but it would be better if they were listed in alphabetical order by the key names (maybe??) or in the order of definition (how to figure this out?), but in some predictable order.
The current exponential time increase means that finding all the combinations for a 15-letter word (such as OXYPHENBUTAZONE !!) takes an enormous amount of time. Seems like we could do better (and indeed, simple programs online take mere seconds instead of hours for such a lookup).
Not sure if it works, but there is a blog post that indicates ways to get 256 colors using escape sequences: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#256-colors
Potential for cursor movement also.
Calling "start()" inside the constructor means that the "run()" method and therefore the subclass' "task()" is started even before the subclass constructor is finished, so the class is not fully constructed yet, and can fail badly because of this.
This question is explored here: https://stackoverflow.com/questions/84285/calling-thread-start-within-its-own-constructor
Need to completely redo this class and use a thread pool, subclass some form of Future, or something that actually works. A factory method might be good: "startAndRunInBackgroundThread(Runnable)" perhaps...
Perhaps it can accept either a Runnable (lambda) or Callable in case there is a single value to return.
Possible structure: version -> { major: 2, minor: 2, patch: 18, prerelease: "+a", buildmeta: "2a3ba65" }
This corresponds to the SemanticVersion class fields.
Note: this would also require the Calc REPL command "version" to be redone somehow, or else the predefined variable to be named "versioninfo" or something like that. Potentially we could eliminate the "version", "quit", "help" commands in favor of the ":version", ":quit", etc. flavors instead.
I would also be in favor of just naming "prerelease" as "release" and "buildmeta" as just "build" or just "meta".
Just as there are commands to "Load" from libraries of functions, it would be useful to have a "Save" option so that functions and variables that have been defined (and tested) can then be saved to an external library file.
Works fine on MacOS.
Just enter 1+โ
for instance
That is, lines that end with "\" meaning the line logically continues on the next line without considering the embedded newline. Especially with "-single" mode, the line-ending character of "\" should be stripped off before continuing, but maybe / probably in all cases.
Perhaps looking at the PATHEXT variable and matching the extension would be good.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.