Skip to content

Commit

Permalink
combinators.extras: adding a variant to cond.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Feb 21, 2017
1 parent b00aef9 commit 890be49
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
23 changes: 23 additions & 0 deletions extra/combinators/extras/extras-tests.factor
Expand Up @@ -38,3 +38,26 @@ IN: combinators.extras.tests
{ t } [ "resource:" [ exists? ] ?1arg >boolean ] unit-test
{ f } [ f [ exists? ] ?1arg ] unit-test
{ f } [ "/homeasdfasdf123123" [ exists? ] ?1arg ] unit-test

{ "hi " "there" } [
"hi there" {
{ [ "there" over start ] [ cut ] }
[ f ]
} cond*
] unit-test

{ "hi " "there" } [
"hi there" {
{ [ "foo" over start ] [ head f ] }
{ [ "there" over start ] [ cut ] }
[ f ]
} cond*
] unit-test

{ "hi there" f } [
"hi there" {
{ [ "foo" over start ] [ head f ] }
{ [ "bar" over start ] [ cut ] }
[ f ]
} cond*
] unit-test
24 changes: 21 additions & 3 deletions extra/combinators/extras/extras.factor
@@ -1,8 +1,9 @@
! Copyright (C) 2013 Doug Coleman, John Benediktsson.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators combinators.smart fry generalizations
kernel macros math quotations sequences locals math.order
sequences.generalizations sequences.private system ;
USING: arrays assocs combinators combinators.smart fry
generalizations kernel macros math quotations sequences locals
math.order sequences.generalizations sequences.private
stack-checker.transforms system words ;
IN: combinators.extras

: once ( quot -- ) call ; inline
Expand Down Expand Up @@ -88,3 +89,20 @@ MACRO:: n-falsify ( n -- quot )

: ?2arg ( obj1 obj2 quot: ( obj1 obj2 -- ? ) -- obj1/f obj2/f )
[ ?2res ] 3keep drop '[ _ _ ] [ f f ] if ; inline

<<
: alist>quot* ( default assoc -- quot )
[ rot \ if* 3array append [ ] like ] assoc-each ;

: cond*>quot ( assoc -- quot )
[ dup pair? [ [ drop ] prepend [ t ] swap 2array ] unless ] map
reverse! [ no-cond ] swap alist>quot* ;

DEFER: cond*
\ cond* [ cond*>quot ] 1 define-transform
\ cond* t "no-compile" set-word-prop
>>
: cond* ( assoc -- )
[ dup callable? [ drop t ] [ first call ] if ] map-find
[ dup callable? [ nip call ] [ second call ] if ]
[ no-cond ] if* ;

0 comments on commit 890be49

Please sign in to comment.