> Materials for tonight's meeting are here -
> http://www.jsoftware.com/jwiki/NYCJUG/2011-08-09 - to peruse. Sorry, no
> conferencing in the Heartland.
Living no less than 7 time zones from NYC, I am not going to attend
the meating anyway :) But browsing through some of the materials,
part of them, in my opinion, hardly demonstrate any advantages of
coding in J (if they are meant to do that at all). Here are comments
on two titles.
`One Off by Sliding'
====================
The proposed J solution solves a somewhat restricted problem: it
appears to be specialized on strings rather than applying to any
sequence. And regarding the string case -- which is meant by the
original poster to be merely an illustration -- the J program gets
even more specialized by interpreting the phrase `not preceded by
a letter' to mean `possibly preceded by a space character'.
In fact, it even works erroneously if the argument string contains
something else beside letters and space chars.
More importantly, the J program does not address the deeper problem
as stated by the original poster, i.e. to find a form of abstraction
that would not require special-casing the first (or last) item as
the sequence gets shortened.
`A Functional Approach to Shifting Certain Array Elements'
==========================================================
Once more, the J program in this article solves a restricted
problem and not the one originally posted. The latter requires
that the solution deals with sequences of arbitrary type, to
which it should be able to apply any relevant predicate. The J
program only works for strings, and actually only for strings of
letters (ending abnormally for other strings). Moreover, the
check it does (for upper-case letters) is `wired-in' in it.
I should add that, even though the J program solves a simpler problem,
its size and complexity hardly make it attractive, compared to the
simple and direct imperative solution by `huynhjl'.
Of course, most other solutions were unnecessarily complicated, too.
For a contrast, in Haskell one would write, instead:
shift p zs = f zs where
f (x:zs@(y:zs')) | not (p y) = x : f zs
| p x = x: y : f zs'
| otherwise = y : f (x:zs')
f zs = zs
Or, avoiding explicit recursion:
shift p xs = reverse $ maybe as (:as) m where
(as,m) = foldl f ([],Nothing) xs
f (as,m) x = if p x then (x:as,m) else (maybe as (:as) m, Just x)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm