1

For this first row found idiom (w∘{(↓⍺)⍳↓⍵}) there seems no reduction in search time even though the first search would be hashing the array making later searches much faster?

      w← 100000000 3⍴'123'
      w←w,[1]'321'

      z← ⎕AI[3] ⋄ w∘{(↓⍺)⍳↓⍵}'321' ⋄ (⎕AI[3])-z
100000001
2892
      z← ⎕AI[3] ⋄ w∘{(↓⍺)⍳↓⍵}'321' ⋄ (⎕AI[3])-z
100000001
2883
Adám
  • 6,573
  • 20
  • 37
Joe Killian
  • 149
  • 4

1 Answers1

0

From the documentation:

Notice that retaining the hash table pays off only on a second or subsequent application of the derived function. This usually occurs in one of two ways: either the derived function is named for later (and repeated) use, as in the first example below or it is applied repeatedly as the operand of a primitive or defined operator, as in the second example.

      w←100000000 3⍴'123'
      w←w,[1]'321'

      f←w∘{(↓⍺)⍳↓⍵}
      z←⎕AI[3] ⋄ f'321' ⋄ (⎕AI[3])-z
100000001
2782
      z←⎕AI[3] ⋄ f'321' ⋄ (⎕AI[3])-z
100000001
16

Try it online!

      w←100000000 3⍴'123' 
      w←w,[1]'321' 

      w∘{(↓⍺)⍳↓⍵}{z←⎕AI[3] ⋄ ⎕←⍺⍺ ⍵ ⋄ ⎕←(⎕AI[3])-z}¨'321' '321'
100000001
2375
100000001
0

Try it online!

Adám
  • 6,573
  • 20
  • 37
  • Nice payoff on very large arrays. Any other idioms for general purpose lookups that retain the hashed table or some other data structure? – Joe Killian Jun 14 '18 at 20:25
  • @JoeKillian They are all listed in the documentation I linked. – Adám Jun 14 '18 at 20:27
  • The Try It Link is to Tio showing the same idiom for testing and I know of the main Idiom page here http://help.dyalog.com/16.0/Content/Language/Defined%20Functions%20and%20Operators/Idiom%20Recognition/Idiom%20List.htm but I think there is a way to hash a table then apply perhaps commonly used functions on it. This is what I meant. Hash a table and what other commonly used phrases are there that might time benefit. There are probably quite a few but say just the top three. Or is this the only idiom or commonly used phrase that makes great use of a hash table? – Joe Killian Jun 14 '18 at 21:28
  • Oops, I know see the link you were talking about. – Joe Killian Jun 14 '18 at 21:29
  • @JoeKillian It may also have your interest to [hash an array](http://help.dyalog.com/16.0/Content/Language/Primitive%20Operators/Hash%20Array.htm "Hash Array") independently of creating an ad hoc function. – Adám Jun 14 '18 at 21:57