File tree Expand file tree Collapse file tree 4 files changed +46
-3
lines changed
Expand file tree Collapse file tree 4 files changed +46
-3
lines changed Original file line number Diff line number Diff line change 77 },
88 "devDependencies" : {
99 "eslint" : " ^3.17.1" ,
10- "purescript-psa" : " ^0.5.0-rc. 1" ,
11- "pulp" : " ^10 .0.4 " ,
10+ "purescript-psa" : " ^0.5.1" ,
11+ "pulp" : " ^11 .0.0 " ,
1212 "rimraf" : " ^2.6.1"
1313 }
1414}
Original file line number Diff line number Diff line change 1+ "use strict" ;
2+
3+ // https://medium.com/@safareli /stack-safe-function-composition-85d61feee37e
4+ var runComposition = function ( composition , x ) {
5+ var root = composition ;
6+ var val = x ;
7+ var stack = [ ] ;
8+ for ( ; ; ) {
9+ if ( root . _0 !== undefined ) {
10+ stack . push ( root . _1 ) ;
11+ root = root . _0 ;
12+ } else {
13+ val = root ( val ) ;
14+ if ( stack . length === 0 ) {
15+ return val ;
16+ }
17+ root = stack . shift ( ) ;
18+ }
19+ }
20+ } ;
21+
22+ exports . functionCompose = function ( f ) {
23+ return function ( g ) {
24+ var res = function composition ( x ) {
25+ return runComposition ( composition , x ) ;
26+ } ;
27+ res . _0 = g ;
28+ res . _1 = f ;
29+ return res ;
30+ } ;
31+ } ;
Original file line number Diff line number Diff line change @@ -13,7 +13,9 @@ class Semigroupoid a where
1313 compose :: forall b c d . a c d -> a b c -> a b d
1414
1515instance semigroupoidFn :: Semigroupoid (-> ) where
16- compose f g x = f (g x)
16+ compose = functionCompose
17+
18+ foreign import functionCompose :: forall b c d . (c -> d ) -> (b -> c ) -> (b -> d )
1719
1820infixr 9 compose as <<<
1921
Original file line number Diff line number Diff line change @@ -6,11 +6,21 @@ type AlmostEff = Unit -> Unit
66
77main :: AlmostEff
88main = do
9+ functionComposition
910 testNumberShow show
1011 testOrderings
1112 testOrdUtils
1213 testIntDegree
1314
15+ functionComposition :: AlmostEff
16+ functionComposition =
17+ assert
18+ (" composition is stack safe" )
19+ (composeGo (_ + 1 ) id 0 0 == 100000 )
20+
21+ composeGo :: forall x a . Semigroupoid x => x a a -> x a a -> Int -> x a a
22+ composeGo f acc n = if n == 100000 then acc else composeGo f (compose acc f) (n + 1 )
23+
1424foreign import testNumberShow :: (Number -> String ) -> AlmostEff
1525foreign import throwErr :: String -> AlmostEff
1626
You can’t perform that action at this time.
0 commit comments