Skip to content

Instantly share code, notes, and snippets.

@narskidan
Created July 12, 2024 03:55
Show Gist options
  • Save narskidan/b7d435ac72b50a90d55ee3e20c0ae6e3 to your computer and use it in GitHub Desktop.
Save narskidan/b7d435ac72b50a90d55ee3e20c0ae6e3 to your computer and use it in GitHub Desktop.
Sleep sort with Gall agent & Behn
/+ default-agent, dbug
|%
+$ versioned-state
$% state-0
==
+$ state-0
$: [%0 length=@ud values=(list @)]
==
+$ card card:agent:gall
--
%- agent:dbug
=| state-0
=* state -
^- agent:gall
|_ =bowl:gall
+* this .
default ~(. (default-agent this %|) bowl)
++ on-init
^- (quip card _this)
~& > '%bravo initialized successfully'
=. state [%0 0 *(list @)]
`this
++ on-save on-save:default
++ on-load on-load:default
++ on-poke
|= [=mark =vase]
^- (quip card _this)
?> ?=(%sortable mark)
=/ sortable !<((list @) vase)
:_ this(state [-.state (lent sortable) `(list @)`~])
%+ turn
sortable
|=(s=@ud [%pass /timer/(scot %ud s) %arvo %b %wait (add now.bowl `@dr`(mul s `@ud`~s1))])
++ on-arvo
|= [=wire =sign-arvo]
^- (quip card _this)
?> ?=([%timer *] wire)
=/ sorted `@ud`(slav %ud `@t`-.t.wire)
`this(state [-.state length.state (snoc values.state sorted)])
++ on-watch on-watch:default
++ on-leave on-leave:default
++ on-peek
|= =path
^- (unit (unit cage))
``noun+!>([=((lent values.state) length.state) values.state])
++ on-agent on-agent:default
++ on-fail on-fail:default
--
@narskidan
Copy link
Author

Usage (in the Dojo):

> :sleep-sort &sortable ~[10 3 7 1 5 2]
>=
> .^([finished=?(%.y %.n) values=(list @)] %gx /=sleep-sort=/values/noun)
[finished=%.n values=~[1 2]]
> .^([finished=?(%.y %.n) values=(list @)] %gx /=sleep-sort=/values/noun)
[finished=%.n values=~[1 2 3]]
> .^([finished=?(%.y %.n) values=(list @)] %gx /=sleep-sort=/values/noun)
[finished=%.y values=~[1 2 3 5 7 10]]
> :: woooo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment