kevinlawler / kona

Open-source implementation of the K programming language
ISC License
1.36k stars 138 forks source link

Feature: monadic ! enumerate for non-integer arguments #13

Closed kevinlawler closed 11 years ago

kevinlawler commented 13 years ago

A. If the argument is a 1. dictionary 2. symbol corresponding to an entry on the k tree (dot-absolute or relative to current working dictionary), return a symbol vector corresponding to the keys. Pick nil or value error for invalid symbol arg.

B. If the argument is a char or char vec (+/- 3) then do an ls on the system directory and return it as a list of char vectors

  _d
`.k
  a:10
  b:20
  .k
.((`a;10;)
  (`b;20;))
  !`".k"
`a `b
  .k.c.d:30
  !`c
,`d

  z[`a `b]:1
  z
.((`a;1;)
  (`b;1;))
  !z
`a `b

  !"/usr/kona"    
("file1",
 "file2")
silentbicycle commented 13 years ago

I'll look into these, I'd like to dig around in the dictionary implementation.

silentbicycle commented 13 years ago

I just posted a commit for issue B (sending a pull request in a moment). Also, typing "!`" makes kona segfault for me, in showAtDepth.

kevinlawler commented 13 years ago

issue b added silentbicycle https://github.com/silentbicycle/kona/commit/e6985b425dc439bf71ad99248993d874ae81e562

isawdrones commented 13 years ago

It would be useful if enumerate accepted vector arguments and returned an enumerated list with the dimensions specified by the vector.

   !3 3 / equivalent to {x#!*/x}
(0 1 2
 3 4 5
 6 7 8)

Is this a useful feature to add to the verb or is it better left as a user defined verb?

kevinlawler commented 13 years ago

The usefulness in the two-dimensional case is apparent. Many problems rely on those sorts of grids. The three-dimensional case doesn't look familiar to me (that doesn't mean there isn't a use case).

((0 1 2
  3 4 5
  6 7 8)
 (9 10 11
  12 13 14
  15 16 17)
 (18 19 20
  21 22 23
  24 25 26))

Do we know of any natural problems involving the 3D or 4D cases?

Is there another generalization that maintains the 2D case but creates something else in higher dimensions?

Let's also add this possibility to the discussion: !1 2 /or similar (0 0 0 1 1 0 1 1)

silentbicycle commented 13 years ago

Making !x where x is a list equivalent to {x#!*/x} sounds like a good idea to me.

isawdrones commented 13 years ago

I can't think of a practical use case for the higher dimensions except for constructing empty arrays of a particular shape and some contrived arithmetical calculations.

I'd expect !1 2 to return ,0 1 making a single list with 2 items, keeping the number of elements consistent with the other cases of enumerate. For the case of enumerating a base, using _vs and ! seems like a better solution to me.

   eb: {+x _vs!y}
   eb[2;5]
(0 0
 0 1
 1 0
 1 1)
kevinlawler commented 13 years ago

For the case of enumerating a base, using _vs and ! seems like a better solution to me.

Right, I mean more for the case of !2 2 2 (I guess the first example should've been written !2 2 ?)

((0 0
  0 0)
 (0 0
  0 1)
 ...
 (1 1
  1 1))

Still undecided about things. What kind of other enumerations can we come up with on !i j k (keeping in mind !,i is different from !i)? Does J have anything?

isawdrones commented 13 years ago

In addition to the feature already mentioned, it also interprets negative numbers as reversing the elements along a particular axis.

   !-3
2 1 0

   !-2 -2 -2
((7 6
  5 4)
 (3 2
  1 0))

   !2 -2
(1 0
 3 2)

   !-2 2
(3 2
 1 0)

It has nothing that would be analogous to !,1.

In Nial the result of vector enumerations is an array of the cartesian product of the numbers in the shape of the vector.

   !2 3
((0 0
  0 1
  0 2)
 (1 0
  1 1
  1 2))
silentbicycle commented 13 years ago

"In Nial the result of vector enumerations is an array of the cartesian product of the numbers in the shape of the vector." I think this is more useful than x#!*/x ,actually.

kevinlawler commented 13 years ago

another candidate: the next lexicographic permutation in the list

so !2 1 3 2 3 1

and !\0 1 2 0 1 2 0 2 1 1 0 2 1 2 0 2 0 1 2 1 0

if x is a 2D array of integers !x could compute...a graph algorithm? matrix algorithm?

kevinlawler commented 13 years ago

the next lexicographic permutation in the list

this also has the property that it works sensibly on a list of arbitrary depth (the top-level elements are compared in their entirety against each other)

!\x works sensibly even if x has repeated elements (multiset)

kevinlawler commented 13 years ago

fyi k4 uses ab`c!0 1 2 to make a dictionary. I think that's worth copying

isawdrones commented 13 years ago

another candidate: the next lexicographic permutation in the list

That makes a lot of sense (enumerating over all the permutations of a list) and is often used. Should this also work for strings, ! already has another behaviour for strings. It is trivial to implement permutations for strings using the integer ones so that is probably not needed.

ab`c!0 1 2

This is a nice shortcut we should copy.

kevinlawler commented 13 years ago

! already has another behaviour for strings

Good point. It would be awkward if that functionality worked on all lists except strings. If next-permutation is implemented it should reclaim that. The ls functionality is easily enough reimplemented with the `4:"ls ...." that silentbicycle added.