Let p be the predicate of what to shift:
'lc uc' =. +&(i.26)&.(a.&i.)"0 'aA'
p =. e.&uc
shift =. {~ (;@(<@(1&|.);.1) i.@#)@:-.@:p
shift 'abcDEfghI'
abDEcfgIh
Don't try to shift the first item!
Henry Rich
On 8/9/2011 2:11 PM, Marshall Lochbaum wrote:
> Here's the selection option, which is equally ugly:
>
> shift =. {~ (2>/\ ,&0) ` ((1&|.+i.@#) ,: (i:&0)\)}@:(e.&uc)
>
> Marshall
>
> -----Original Message-----
> From: Marshall Lochbaum [mailto:[email protected]]
> Sent: Tuesday, August 09, 2011 2:01 PM
> To: 'Programming forum'
> Subject: RE: [Jprogramming] Materials for NYCJUG meeting today - Tuesday 8/9
> at Empire State Building
>
> Here is the best code I have for shifting:
>
> 'lc uc' =. +&(i.26)&.(a.&i.)"0 'aA'
> shift =. /: (i.@# - ] - (1 |. ([+*)/\.) * 0 ,~ 2&(</\))@:(e.&uc)
> shift 'abcDEfghI'
> abDEcfgIh
>
> It's still a little complicated. Another option which I'm looking at now is
> to select instead of sorting: then all spaces with capitals in front of them
> take that capital, and all of the last capitals (capitals that come before
> lowercase letters) take the last uppercase before that block of capitals.
>
> Marshall
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Devon McCormick
> Sent: Tuesday, August 09, 2011 11:54 AM
> To: Programming forum
> Subject: Re: [Jprogramming] Materials for NYCJUG meeting today - Tuesday
> 8/9 at Empire State Building
>
> Thanks for your comments.
>
> These were done to come up with some examples to present J to a group of
> functional programmers. I'm hoping to provide J equivalents alongside
> solutions in other languages with which people may be more familiar. I'll
> take a look at coming up with a solution to hew more closely to the problem
> spec but I'd like to use as much "raw" J as possible and not rely on
> equivalents like the apparent built-ins "isLetter" and "toLower" in some of
> the other solutions.
>
> I wasn't very happy with the J code for the shifting problem as it does seem
> too complicated for such a simple problem.
>
> In the "One Off by Sliding" example, I'm trying to illustrate a different
> way to avoid looping by using arrays rather than recursion which seems to be
> a common iteration method in functional languages. Ideally, I'd like an
> example where I can show both an array-based solution and work in some of
> J's iterative methods as well - things like scan and the power conjunction.
>
> On Tue, Aug 9, 2011 at 7:47 AM, Boyko Bantchev<[email protected]> wrote:
>
>>> 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
>
>
>
>
> --
> Devon McCormick, CFA
> ^me^ at acm.
> org is my
> preferred e-mail
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm