From cf973ad7d1a148304fee03854ad59f1e0eba1443 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Tue, 2 Oct 2018 22:31:03 -0700 Subject: [PATCH 1/5] Add Factor bogosort --- CONTRIBUTORS.md | 4 +++- contents/bogo_sort/bogo_sort.md | 4 ++++ contents/bogo_sort/code/factor/bogo_sort.factor | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 contents/bogo_sort/code/factor/bogo_sort.factor diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 1cbaed966..1d63bea1a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -65,4 +65,6 @@ Trashtalk Cyrus Burt
Patrik Tesarik -
\ No newline at end of file +
+nic-hartley +
diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index d46241c06..b1ff50673 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -43,6 +43,8 @@ In code, it looks something like this: [import:16-18, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} [import:12-16, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="factor" %} +[import:10-12, lang:"factor"](code/factor/bogo_sort.factor) {% endmethod %} That's it. @@ -85,6 +87,8 @@ We are done here! [import, lang:"nim"](code/nim/bogo_sort.nim) {% sample lang="ruby" %} [import, lang:"ruby"](code/ruby/bogo.rb) +{% sample lang="factor" %} +[import, lang:"factor"](code/factor/bogosort.factor) {% endmethod %} diff --git a/contents/bogo_sort/code/factor/bogo_sort.factor b/contents/bogo_sort/code/factor/bogo_sort.factor new file mode 100644 index 000000000..c2602d320 --- /dev/null +++ b/contents/bogo_sort/code/factor/bogo_sort.factor @@ -0,0 +1,12 @@ +! There's no built-in "is sorted" function, so let's make one: +USING: locals ; +: sorted? ( seq -- ? ) + 2 clump ! split it up into overlapping pairs + ! so now, for example, { 1 2 3 } has turned into { { 1 2 } { 2 3 } } + ! and now we make sure that for every pair, the latter is >= the former + [| pair | pair first pair last <= ] all? ; + + +USING: random ; +: bogosort ( seq -- seq' ) + [ dup sorted? ] [ randomize ] until ; From a68ce090d18ebb36ee358bddcaf6708c8daf1450 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Tue, 2 Oct 2018 22:52:12 -0700 Subject: [PATCH 2/5] added missing information --- book.json | 6 +++++- contents/bogo_sort/bogo_sort.md | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/book.json b/book.json index 94676c45b..705d244f0 100644 --- a/book.json +++ b/book.json @@ -139,7 +139,11 @@ { "lang": "f90", "name": "Fortran90" - } + }, + { + "lang": "factor", + "name": "Factor" + } ] } } diff --git a/contents/bogo_sort/bogo_sort.md b/contents/bogo_sort/bogo_sort.md index b1ff50673..62b0aeef8 100644 --- a/contents/bogo_sort/bogo_sort.md +++ b/contents/bogo_sort/bogo_sort.md @@ -88,7 +88,7 @@ We are done here! {% sample lang="ruby" %} [import, lang:"ruby"](code/ruby/bogo.rb) {% sample lang="factor" %} -[import, lang:"factor"](code/factor/bogosort.factor) +[import, lang:"factor"](code/factor/bogo_sort.factor) {% endmethod %} From c8c0b287e1afd30e498d741f596b42762727b34a Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sat, 6 Oct 2018 09:36:55 -0700 Subject: [PATCH 3/5] added explanatory comment --- contents/bogo_sort/code/factor/bogo_sort.factor | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contents/bogo_sort/code/factor/bogo_sort.factor b/contents/bogo_sort/code/factor/bogo_sort.factor index c2602d320..7a1225279 100644 --- a/contents/bogo_sort/code/factor/bogo_sort.factor +++ b/contents/bogo_sort/code/factor/bogo_sort.factor @@ -9,4 +9,7 @@ USING: locals ; USING: random ; : bogosort ( seq -- seq' ) + ! `dup` duplicates the array, because `sorted?` pops its reference to it + ! randomize works in-place + ! so we `randomize` `until` it's `sorted?` [ dup sorted? ] [ randomize ] until ; From 1fd333fb29bd3ee2abcd58e186035331c28403fe Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sat, 6 Oct 2018 09:47:24 -0700 Subject: [PATCH 4/5] complying with .editorconfig from #426 --- contents/bogo_sort/code/factor/bogo_sort.factor | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/contents/bogo_sort/code/factor/bogo_sort.factor b/contents/bogo_sort/code/factor/bogo_sort.factor index 7a1225279..e089e5258 100644 --- a/contents/bogo_sort/code/factor/bogo_sort.factor +++ b/contents/bogo_sort/code/factor/bogo_sort.factor @@ -4,12 +4,14 @@ USING: locals ; 2 clump ! split it up into overlapping pairs ! so now, for example, { 1 2 3 } has turned into { { 1 2 } { 2 3 } } ! and now we make sure that for every pair, the latter is >= the former - [| pair | pair first pair last <= ] all? ; - + [| pair | pair first pair last <= ] all? +; USING: random ; : bogosort ( seq -- seq' ) ! `dup` duplicates the array, because `sorted?` pops its reference to it ! randomize works in-place ! so we `randomize` `until` it's `sorted?` - [ dup sorted? ] [ randomize ] until ; + [ dup sorted? ] [ randomize ] until +; + From 584b431e23a34aeb3236979defc341929bfd38e4 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sun, 7 Oct 2018 10:06:21 -0700 Subject: [PATCH 5/5] added demo of usage --- contents/bogo_sort/code/factor/bogo_sort.factor | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contents/bogo_sort/code/factor/bogo_sort.factor b/contents/bogo_sort/code/factor/bogo_sort.factor index e089e5258..44515b56a 100644 --- a/contents/bogo_sort/code/factor/bogo_sort.factor +++ b/contents/bogo_sort/code/factor/bogo_sort.factor @@ -15,3 +15,11 @@ USING: random ; [ dup sorted? ] [ randomize ] until ; +! WARNING: Increasing this number beyond 5 or so will make this take a very long time. +! That said, if you have an afternoon to kill... +5 >array randomize ! generate a random array to demo +dup . ! show the array +bogosort ! bogosort it +. ! show it again + +