From: John Benediktsson Date: Sat, 2 Sep 2023 01:10:29 +0000 (-0700) Subject: sudokus: some formatting to understand code X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=11bfb625b2fb387a350225b4cd89b0a9369c05dc sudokus: some formatting to understand code --- diff --git a/extra/models/combinators/combinators.factor b/extra/models/combinators/combinators.factor index 3ff4d98fee..db2f3558a8 100644 --- a/extra/models/combinators/combinators.factor +++ b/extra/models/combinators/combinators.factor @@ -108,7 +108,7 @@ M: (when-model) (model-changed) [ quot>> ] 2keep [ value>> ] [ cond>> ] bi* call( a -- ? ) [ call( model -- ) ] [ 2drop ] if ; ! only used in construction -: with-self ( quot: ( model -- model ) -- model ) [ f dup ] dip call swap [ add-dependency ] keep ; inline +: with-self ( quot -- model ) [ f dup ] dip call swap [ add-dependency ] keep ; inline USE: models.combinators.templates << { "$>" "<$" "fmap" } [ fmaps ] each >> diff --git a/extra/sudokus/sudokus.factor b/extra/sudokus/sudokus.factor index d4110d4f80..c0f714e0e9 100644 --- a/extra/sudokus/sudokus.factor +++ b/extra/sudokus/sudokus.factor @@ -12,10 +12,13 @@ IN: sudokus : nth-or-lower ( n seq -- elt ) [ length 1 - 2dup > [ nip ] [ drop ] if ] keep nth ; :: solutions ( puzzle random? -- solutions ) - f puzzle random? [ indices [ f ] [ random? swap nth-or-lower ] if-empty ] [ index ] if - [ :> pos - 1 9 [a..b] 80 [ pos near ] filter [ puzzle nth ] map members diff - [ 1array puzzle pos cut-slice rest surround ] map >list [ random? solutions ] bind + f puzzle random? [ + indices [ f ] [ random? swap nth-or-lower ] if-empty + ] [ index ] if [| pos | + 1 9 [a..b] 80 [ pos near ] filter + [ puzzle nth ] map members diff + [ 1array puzzle pos cut-slice rest surround ] map >list + [ random? solutions ] bind ] [ puzzle list-monad return ] if* ; : solution ( puzzle random? -- solution ) @@ -33,22 +36,31 @@ IN: sudokus [ f swap [ length random ] keep set-nth ] curry times ] keep ; -: do-sudoku ( -- ) +: ( -- gadget ) [ [ - [ - 81 [ "" ] replicate switch-models [ [ ] map 9 group [ 3 group ] map 3 group - [ [ [ [ [ ->% 2 [ string>number ] fmap ] - map ] map concat ] , ] map concat ] map concat - [ "Difficulty:"