Search code examples
arraysnestedfilteringapldyalog

Dyalog APL: How to filter an array like filter()


I need to filter out from the array (namespaces) all values that do not fulfil the required condition: field 'status' = 'TRADING'

I use this variant

tmpSymbolsT←(data←⎕JSON(HttpCommand.Get'api.binance.com/api/v3/exchangeInfo').Data).symbols
tmpSymbols←{(⍵.status≡'TRADING')⌿⍵}⍨¨tmpSymbolsT

However, it returns every row in the wrapper, and I want clean rows

      tmpSymbols[1] ⍝ I have
┌─────────────────────────────────┐
│┌───────────────────────────────┐│
││ #.[JSON object].[JSON object] ││
│└───────────────────────────────┘│
└─────────────────────────────────┘


      tmpSymbolsT[1] ⍝ I need like this
#.[JSON object].[JSON object]

How to do it?


Solution

  • The problem here is that you filter each (¨) object separately, rather than filtering the entire collection.

    (By the way, you also have an unnecessary commute () which doesn't do anything in this context.)

    So, you really want something along the lines of tmpSymbols←{(⍵.status≡'TRADING')⌿⍵}tmpSymbolsT but now ⍵.status is a vector (list) of statuses, which of course won't match () the single status 'TRADING'. You have a few options for fixing this:

    1. ⍵.status≡¨⊂'TRADING' uses an explicit each and encloses () the single status so it is used for every comparison.
    2. 'TRADING'∘≡¨⍵.status binds () the single status to the match function, thus deriving an "is trading" function, which is then applied to each status.
    3. ⍵.status∊⊂'TRADING' which encloses the single status and checks all the statuses for which ones are a member of () the array of a single status.

    Here is an example of code that does what you want:

          tmpSymbolsT←(data←⎕JSON(HttpCommand.Get'api.binance.com/api/v3/exchangeInfo').Data).symbols
          tmpSymbols←{(⍵.status∊⊂'TRADING')⌿⍵}tmpSymbolsT
          tmpSymbols[1]
    #.[JSON object].[JSON object]