On Mon, Jun 18, 2012 at 5:15 AM, Gian Medri <gianme...@gmail.com> wrote: > I shall replace some text in a vector. My function is > Replace=:13 :'(p{.y),(>1{x),(($>0{x)+p=.I. (>0{x) E. y)}.y' > Example : ('can be';'is') Replace 'Life can be good' > Life is good > I would like to see if there is a better solution, more J-like.
"Better" needs to be defined before it can be achieved. There are all sorts of "better". Perhaps of interest, however, are the functions made available using require 'regex' (also: open 'regex' lets you view the file from J6, it does not work in some versions of J7 but may work in some versions?). > I have also 2 functions for interval: > 1) int1=:({.@] < [)*.([ < {:@]) > 6 int1 4 9 > 1 > This is very clear how it works. > > 2) int2=:[: ~:/ >:/~ > This is more concise and elegant, but I don't understand how it works. > Can somebody give me an explanation? I did not see anyone give the sort of explanation which I prefer, which is to break the execution down into steps. 6 ([: ~:/ >:/~) 4 9 1 first computes the value prescribed by the right tine of the fork: 6 (>:/~) 4 9 0 1 and the ~ swaps arguments of its verb, so this is equivalent to 4 9 (>:/) 6 0 1 also the / is irrelevant here (we are using a verb it derived in a dyadic context and one of the dyadic arguments is rank 0). So: 4 9 (>:) 6 0 1 in other words the first thing we find is that 4 is not greater than or equal to 6 but 9 is. And then we process this intermediate result ~:/ 0 1 1 The / adverb inserts its verb between each item of its argument, so this is equivalent to 0 ~: 1 1 In other words, the statement is true when the right argument is greater than one of the left argument values but not the other. Stepping back, the / in the right tine would be useful when we have multiple values in the right argument: 2 3 5 7 11 ([: ~:/ >:/~) 4 9 0 0 1 1 0 Here, instead of eliminating the rightmost /, I would note that we are finding the result of >: for all paired elements from the left and right arguments of the verb derived by / when its dyadic definition is used. Finally, note that we can test against multiple intervals if we arrange them properly: 2 3 5 7 11 ([: ~:/ >:/~) 1 5,.4 9,.6 10 1 1 1 0 0 0 0 1 1 0 0 0 0 1 0 I hope this helps, -- Raul ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm