Skip to content

Instantly share code, notes, and snippets.

@chrispsn
Last active July 10, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrispsn/4e2930558993d1c19f7bb5cfbcf97824 to your computer and use it in GitHub Desktop.
Save chrispsn/4e2930558993d1c19f7bb5cfbcf97824 to your computer and use it in GitHub Desktop.
k design scrapheap
# Unevaluated
A `wrap attribute that causes overindexing to auto-mod based on length of list.
Avoids mismatch of behaviour of i# and -i#. Cut will auto-fill if not long enough.
Negative indexing wraps backwards: (`wrap"abc")[-1] is "c"
Requires `null to go back to default behaviour?
## Slice as basis for lots of primitives
Inspired by ngn's https://chat.stackexchange.com/transcript/message/59004538#59004538 ...
// cycle
slice:{s:x>0; x+:0&1+x+#y; l:(x#y;x_y); $[s;l;|l]}
s:"supercali"
slice[3;s]
slice[-3;s]
slice[-10;s]
slice[10;s]
cuts:{*+{slice[y;*|x]}\[(();y);x]}
cuts[2 1 3 2;s]
/ what if more than length of string?
""
// non-cycle
slice:{s:x>0; x+:0&1+x+#y; l:(x$y;x_y); $[s;l;|l]}
s:"supercali"
slice[3;s]
slice[-3;s]
slice[-10;s]
slice[10;s]
cuts:{*+{slice[y;*|x]}\[(();y);x]}
cuts[2 1 3 2;s]
// why join/split when we can group?
/ (does not work same for all cases eg "\n\n" as no empty strings)
/ group then filter
{x(=(~a)*1+\a:"\n"=x)_0} "hello\nworld\nhow\nare\nyou\n"
{(=(~a)*x!1+\a:"\n"=x)_0} / interesting that can't do x!x too early as stops the mask working (since a is a dict)
/ filter then group
{= :[;a]_x!+\a:"\n"=x}
{= :[;a]_+\a:"\n"=x!x} / self-dict (doesn't quite work yet in ngn/k)
{= :[;.a]_+\a:"\n"=x!x} / same (works; notice the dot before a to just get values)
how could this be written without 'a'?
could write in a stackish language...
push to end with "\n"= applied
push to end based on last elem in stack with +\ applied
swap last two
where-not stack based on last elem in stack (removes last elem and filters remaining stack elements)
group stack based on last element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment