I know you mentioned 8!:2. Personally, I would probably skip that and
use sprintf coming from a C background...

load 'format/printf'

'%d-%02d-%02dT%02d:%02d:%02d' sprintf 6!:0''
2014-03-02T05:47:14

It's convenient that the indexes line up, otherwise it'd be a handful
of selections

This took less than 2 minutes, which is more than what I spent reading
the 8!:2 page. When would 8!:2 be used over sprintf? Presumably 8!:2
is faster

On Sun, Mar 2, 2014 at 4:04 AM, Raul Miller <rauldmil...@gmail.com> wrote:
> Given
>    6!:0''
> 2014 3 2 3 30 46.034
>
> I'd like to generate the string
> 2014-03-02T03:30:46
>
> And, to make things just a bit strange, I'd like to use 8!:2 instead of the
> obvious.
>
> Just to be clear, though, here's my idea of "obvious":
>
>    }:;(1,5#3)<@}."_1'--T:: ',.~":,.10000+<.6!:0''
>
> By obvious, I do not mean that I instantly thought of that expression, I
> had to think a few minutes, and I had a few errors to fix in my initial
> concept. But by "obvious" I mean that I am using the basic J vocabulary.
>
> Picking up the vocabulary isn't instant, though, and I'm using 14 different
> words from the vocabulary (plus some nouns and a pair of parenthesis). So
> if we expect a person to pick up five words from the vocabulary a week,
> that's at least three weeks of training before a person could be reasonably
> comfortable composing a sentence like that.
>
> Now... the 8!:0, 8!:1 and 8!:2 verbs do have their own tiny vocabulary as
> represented by their format strings. But these are examples of extremely
> specialized words - not very powerful at all, which means they require less
> study than J as a whole, and (within their limited capabilities) will tend
> to be faster that a more general approach.
>
> Formatting is not likely a speed bottleneck for iso date formatting - it's
> more when you have a big sheet of numbers and a weak computer that you
> might care about formatting time. But it's still an interesting exercise.
> (Or a boring one, if you are not interested in formatting text.):
>
> So... here's how I approached this exercise:
>
>    <.2014 3 2 3 30 46.034
> 2014 3 2 3 30 46
>
> Let's just ignore the decimal part, for now.
>
>    '4' 8!:2 <.2014 3 2 3 30 46.034
> 2014.00003.00002.00003.000030.000046.0000
>
> Looks like a number by itself is decimal places after the decimal point.
>
>    '4.' 8!:2 <.2014 3 2 3 30 46.034
> 2014   3   2   3  30  46
>
> But a decimal point in the format specifier fixes that.
>    '4.,2.,2.,2.,2.,2.' 8!:2 <.2014 3 2 3 30 46.034
> 2014 3 2 33046
>
> I need commas to separate the format specifiers for independent columns.
>
>    '4.,r<0>2.,2.,2.,2.,2.' 8!:2 <.2014 3 2 3 30 46.034
> 201403 2 33046
>
> The r option gives "fill" or, in this context "leading zeros", but each
> format needs this spec.
>
>    '4.,r<0>2.,r<0>2.,r<0>2.,r<0>2.,r<0>2.' 8!:2 <.2014 3 2 3 30 46.034
> 20140302033046
>
> Getting bulky, but now all my two digit numbers can have leading zeros. I
> could get fancy here, and use J to build the format specifier, but that's
> premature, watch:
>
>    '4.,p<->r<0>2.,p<->r<0>2.,p<T>r<0>2.,p<:>r<0>2.,p<:>r<0>2.' 8!:2 <.2014
> 3 2 3 30 46.034
> 2014-3-2T3****
>
> Here, I've added a prefix for each number, to get the various separators.
> Unfortunately, I forgot to widen each column to make things fit.
>
>    '4.,p<->r<0>3.,p<->r<0>3.,p<T>r<0>3.,p<:>r<0>3.,p<:>r<0>3.' 8!:2 <.2014
> 3 2 3 30 46.034
> 20140-30-20T3:30:46
>
> That's... better ,but now we can see that the concept of fill interacts
> badly with the concept of using a prefix, so let's try using a suffix
> instead:
>    's<->4.,s<->r<0>3.,s<T>r<0>3.,s<:>r<0>3.,s<:>r<0>3.,r<0>3.' 8!:2 <.2014
> 3 2 3 30 46.034
> |domain error
>
> Oops, suffix is a q, not an s.
>
>  8!:2<.2014 3 2 3 30 46.034
>    'q<->4.,q<->r<0>3.,q<T>r<0>3.,q<:>r<0>3.,q<:>r<0>3.,r<0>3.' 8!:2 <.2014
> 3 2 3 30 46.034
> ****03-02T03:30:046
>
> Almost done but I need to adjust my column widths again.
>
>    'q<->5.,q<->r<0>3.,q<T>r<0>3.,q<:>r<0>3.,q<:>r<0>3.,r<0>2.' 8!:2 <.2014
> 3 2 3 30 46.034
> 2014-03-02T03:30:46
>
> Done.
>
> And, that's not actually a whole lot more verbose than the other expression.
>
> It's tempting to think about making 8!:2 more powerful, but it's not really
> about power - it's a specialized tool for a specialized task. If you want
> the full generality of J, it's available.
>
> Of course, if you really want an iso formatted timestamp, you could always
> find another way to get that. Learning exercises necessarily restrict
> themselves in various ways.
>
> Also... http://en.wikipedia.org/wiki/ISO_8601 points out that the standard
> really covers a lot of ground. And one thing we do not currently have in J
> is a way of getting at what the operating system thinks is the time zone of
> the user. That can matter if you are on a phone (where back a few decades
> ago, computers were a bit harder to carry).
>
> Thanks,
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to