Message Boards Message Boards

0
|
5812 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Function vs one line, What am I missing?

Posted 9 years ago

rose2 := {{z, 0, 1, 2, 3, 4}, {y, j, k, l, m, 5}, {x, i, b, c, n, 6}, {w, h, a, d, o, 7}, {v, g, f, e, p, 8}, {u, t, s, r, q, 9}};

roseEncode[str_] := Module[{strIn := str, toEn, cLst, rosePos}, toEn := StringCases[ToLowerCase[strIn], RegularExpression["[a-z,0,9]+"]];

cLst := Characters[StringJoin[toEn]];

rosePos := FirstPosition[rose2, #] &/@ cLst];

Return[Partition[rosePos, 2]]; ];

Print["Rose Encode: ", roseEncode["safe"]];

bark := FirstPosition[rose2, #] - 1 & /@ {s, t, a, r, t};

Print["Line Encode: ", bark]

Output: Rose Encode: {{Missing[NotFound],Missing[NotFound]},{Missing[NotFound],Missing[NotFound]}} Line Encode: {{5,2},{5,1},{3,2},{5,3},{5,1}}

The one liner works but the function doesn't. What am I missing? I have printed out cLst in the function and it is {s,t,a,r,t} just like in the one liner, I've tried replacing /@ with Map[rose2,cLst] with the same result. I also tried to Debug but have not figured out how to step through the Map and see each variable. Any help would be appreciated.

Mark

Attachments:
POSTED BY: Mark Henwood
4 Replies
Posted 9 years ago

Thanks Eric, You were right on the money. When I quoted the characters it worked. Thanks again for the help. Mark

POSTED BY: Mark Henwood
Posted 9 years ago

Mark, it's not clear to me whether you understood my comment. The reason your one-liner works and your roseEncode method doesn't is that your rose2 structure contains no strings. Your one-liner is applied to Symbols, and rose2 does contain some matching Symbols. Your roseEncode function is applied to a String, and rose2 contains no Strings (no Characters), so it reports Missing. In other words, both your one-liner and your function are working.

To simplify your function, let's start with your hard-coded one-liner (slightly simplified for clarity):

FirstPosition[rose2, #] & /@ {s, t, a, r, t}

The algorithm is clear, we just need to replace the specific, hard-coded values with variables. Basically, you're trying to create a FirstPosition function that works with a list of patterns (FirstPosition would look for an actual list if you passed it a list as a pattern). Let's name it FirstPositions.

FirstPositions[structure_, patterns_List] := FirstPosition[structure, #] & /@ patterns

By calling out _List this way, I can create an alternate version that works with a string.

FirstPositions[structure_, patterns_String] := FirstPositions[structure, Characters[patterns]]

We can check how FirstPositions works:

In[8]:= FirstPositions[rose2, {s, t, a, r, t}]
Out[8]= {{6, 3}, {6, 2}, {4, 3}, {6, 4}, {6, 2}}

In[9]:= FirstPositions[rose2, "start"]
Out[9]= {Missing["NotFound"], Missing["NotFound"], 
 Missing["NotFound"], Missing["NotFound"], Missing["NotFound"]}

In[10]:= FirstPositions[Map[ToString, rose2, {-1}], "start"]
Out[10]= {{6, 3}, {6, 2}, {4, 3}, {6, 4}, {6, 2}}

From here, you can add the checking you want (ignoring spaces, normalizing to lowercase, etc).

POSTED BY: Eric Rimbey
Posted 9 years ago

Mark,

In your attached notebook, rose2 includes Integers and Symbols, but no Strings. Your roseEncode function creates a string pattern to look for. Your one-liner, bark, is looking for Symbols, and it finds them.

Also, I'd suggest you check out the documentation for Set and SetDelayed. You are excessively using SetDelayed, and this is complicating your code. Your Module-based definition of roseEncode is also excessively complex. The one-liner you have that works is very easily translated into a simple function without the need to introduce Module.

POSTED BY: Eric Rimbey
Posted 9 years ago

Rose2 is a definition, a 2 dim array of scrambled alpha-numerics. The roseEn takes a string, throws away spaces etc and passes the results to map. It is called in the print statement and passed the string "start". If cLst is printed out during the function it is {s,t,a,r,t} the same as is passed into the one liner. I will look into the assignment differences, I've tried = before and kept getting errors so rather than fighting with it all the time. Mark

POSTED BY: Mark Henwood
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract