[personal kdb+] convert J to K

hi guys,

been looking at rosettacode.org. can anyone help me work out the K
solution for
http://rosettacode.org/wiki/Apply\_a\_callback\_to\_an\_array#J

and here is the J code that was posted:
Solution:

"_1

Example:

callback =: *:
array =: 1 2 3 4 5

callback"_1 array
1 4 9 16 25

ta, jack.
can K beat a 3 character solution? :)


Submitted via Google Groups

’ AttilaOn Thu, May 13, 2010 at 9:54 AM, effbiae wrote:> hi guys,>> been looking at rosettacode.org. ?can anyone help me work out the K> solution for> ? ? http://rosettacode.org/wiki/Apply_a_callback_to_an_array#J>> and here is the J code that was posted:> Solution:>> ? “_1>> Example:>> ? callback =: ?*:> ? array ? ?=: ?1 2 3 4 5>> ? callback”_1 array> 1 4 9 16 25>>> ta, jack.> ?can K beat a 3 character solution? ?:)>> –>

Submitted via Google Groups

Actually - most functional languages require 3 characters - m,a,p

Scala:
scala> def sq(i:Int) = i*i
sq: (Int)Int

scala> 1 to 5 map sq
res13: RandomAccessSeq.Projection[Int] = RangeM(1, 4, 9, 16, 25)

Yoel

output:
1 4 9 16 25

how could you extend the k solution to print both the input and result:

1 1
2 4
3 9
4 16
5 25

?


Submitted via Google Groups

q)k){x,x*x}'1 2 3 4 51 12 43 94 165 25 AttilaOn Thu, May 13, 2010 at 10:45 AM, Jack Andrews wrote:> output:> 1 4 9 16 25>> how could you extend the k solution to print both the input and result:>> 1 ?1> 2 ?4> 3 ?9> 4 ?16> 5 ?25>> ?>> –>

Submitted via Google Groups

On Thu, May 13, 2010 at 7:55 PM, Attila Vrabecz<attila.vrabecz> wrote:> q)k){x,x*x}'1 2 3 4 5i guess there is a ‘square’ function and ‘identity’ function that couldmake the above faster?how would you express it? i know this is probably wrong:(identity,square) ’ 1 2 3 4 5> 1 1> 2 4> 3 9> 4 16> 5 25>> ?Attila>>>> On Thu, May 13, 2010 at 10:45 AM, Jack Andrews wrote:>> output:>> 1 4 9 16 25>>>> how could you extend the k solution to print both the input and result:>>>> 1 ?1>> 2 ?4>> 3 ?9>> 4 ?16>> 5 ?25>>>> ?>>>> –>>

Submitted via Google Groups</attila.vrabecz>

this whole thing is quite pointlesssquare and identity are atomic in both J and Kso no need for map (each ’ "_1) whatsoever AttilaOn Thu, May 13, 2010 at 11:04 AM, Jack Andrews wrote:> On Thu, May 13, 2010 at 7:55 PM, Attila Vrabecz> <attila.vrabecz> wrote:>> q)k){x,x*x}'1 2 3 4 5>> i guess there is a ‘square’ function and ‘identity’ function that could> make the above faster?>> how would you express it? ?i know this is probably wrong:>> (identity,square) ’ 1 2 3 4 5>>>> 1 1>> 2 4>> 3 9>> 4 16>> 5 25>>>> ?Attila>>>>>>>> On Thu, May 13, 2010 at 10:45 AM, Jack Andrews wrote:>>> output:>>> 1 4 9 16 25>>>>>> how could you extend the k solution to print both the input and result:>>>>>> 1 ?1>>> 2 ?4>>> 3 ?9>>> 4 ?16>>> 5 ?25>>>>>> ?>>>>>> –>>>

Submitted via Google Groups</attila.vrabecz>