2

I am trying to add an item into a darray

    |> darray(JSON::decodeMap($$))

I want to append one more item

discover_arg => xxxxx in to this darray, how could I achieve so?

Pythoner
  • 5,265
  • 5
  • 33
  • 49
  • 1
    Why do you want to do it in the pipe instead of with two lines? The latter is likely to be a lot easier to read: `$arr = whatever() |> darray(JSON::decodeMap($$)); $arr['discover_arg'] = 'xxxxx';` – Josh Watzman Nov 01 '20 at 12:34
  • Agreeing with Josh; I probably would avoid trying to do 100% pipes here, as they make it harder to read for the next person through. (Or you, a year from now.) – Dean J Mar 13 '21 at 22:44

1 Answers1

0

There is a merge function for dicts, if you're able to use these instead of darray.

But for the sake of one element on a potentially large array, I wouldn't use it — many of the HSL functions often incur a linear penalty for constant-time/space things. They are immutable functions, and PHP/Hack's collections aren't implemented for fast partial cloning (like linked lists and trees are).

At the risk of muddying the purity of the pipe, especially if you're really setting this element right after generating it, you may just make your own lambda set + return:

  |> darray(JSON::decodeMap($$))
  |> ((darray<string, string> $d) ==> { $d['discover_arg'] = 'xxxxx'; return $d; })($$)
concat
  • 3,107
  • 16
  • 30