Open eric-j-ason opened 7 months ago
(I thought I replied to it but apparently I never submitted... oops)
I like this idea and pondered for a while to start a kind of "standard library" with useful functionality implemented in ucode. Question is how to package it (ucode-stdlib?) and how to organize the "classes" within that library, e.g. whether to use python style short names (math, itertools, array, ...) or Java style name hierarchies...
I'm slightly leaning towards the short name approach but would love to hear other opinions.
Question is how to package it (ucode-stdlib?) and how to organize the "classes" within that library, e.g. whether to use python style short names (math, itertools, array, ...) or Java style name hierarchies...
It was many thousands of years ago since I wrote Java. Would you remind us of what its name hierarchies are like?
Would you remind us of what its name hierarchies are like?
Example from the official documentation: https://docs.oracle.com/javase/7/docs/api/index.html
Example from the official documentation: https://docs.oracle.com/javase/7/docs/api/index.html
Got it. So, more levels, I take it. Personally, I don't have a strong opinion one way or the other.
Okay, so let's define a number of "classes" based upon your submission:
/usr/share/ucode/array.uc
import { uniform, ... } from 'array';
/usr/share/ucode/output.uc
import { println, ... } from 'output';
/usr/share/ucode/datatime.uc
import { iso8601 } from 'datatime';
/usr/share/ucode/random.uc
import { rand_int, ... } from 'random';
Maybe the "random" and "output" modules are too narrow in scope and should be made into a "numeric" and "data" module to deal with anything numerical (calculations, randomness, ...) and data structure (formatting, transforming, higher level structures) respectively.
I added the functions product
, prettyprint
and prettyprintln
.
Okay, so let's define a number of "classes" based upon your submission:
Cool!
Some of the functions in "general" are not array related, or only partially so, so if there will be a class called "array", they should probably be outside of it:
bool
compose
equal
* Usage: `import { iso8601 } from 'datatime';` * Scope: time & calendar related functions, could gain functionality to calculate delta between dates, strptime & strftime equivalents etc.
(I assume it was meant to say "datetime".) Yeah, date calculation and manipulation would be useful. It's a mess to deal with, so I'm thinking that it makes sense to call upon some existing library.
Maybe the "random" and "output" modules are too narrow in scope and should be made into a "numeric" and "data" module to deal with anything numerical (calculations, randomness, ...) and data structure (formatting, transforming, higher level structures) respectively.
The "random" category should probably be extended:
Updated the equal
function to use strict equality (===
). It's probably good to default to strict equality. It might come as an unfortunate surprise for a user to have 0
equal ""
and similar.
I propose here a suite of functions that I found myself reaching for, and missing in the standard library. I present them in ucode, as that's how I use them; possibly, they should be implemented in C instead.
The tests at the end demonstrates what the functions do.
General
General-purpose functions, most of them for analyzing or manipulating arrays. A source of inspiration is Python's itertools. However, as ucode does not have iterators with lazy evaluation (right?), some of the itertools functions do not make sense in ucode, and some other might be less efficient.
Printing
The pretty printer was written before I learned that there already is one! However, I think I can offer two improvements with my version: it prints
[ ]
and{ }
on a single line, and it breaks arrays and objects into multiple lines only when needed to avoid values being too long. Also I'm happy about how short the function could be made (after several rewrites).Time
Random
Returns random data in a few useful forms. Gets its randomness from
/dev/random
.Tests