File tree Expand file tree Collapse file tree 4 files changed +49
-3
lines changed
Expand file tree Collapse file tree 4 files changed +49
-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+ function FuncStack ( isNil , val ) {
2+ this . isNil = isNil
3+ this . val = val
4+ }
5+
6+ exports . functionCompose = function ( f ) {
7+ return function ( g ) {
8+ var res = function composition ( x ) {
9+ return runComposition ( composition , x ) ;
10+ } ;
11+ res . _0 = g
12+ res . _1 = f
13+ return res
14+ } ;
15+ } ;
16+
17+ // https://medium.com/@safareli /stack-safe-function-composition-85d61feee37e
18+ var runComposition = function ( composition , x ) {
19+ var root = composition
20+ var val = x
21+ var stack = [ ]
22+ while ( true ) {
23+ if ( root . _0 !== undefined ) {
24+ stack . push ( root . _1 )
25+ root = root . _0
26+ } else {
27+ val = root ( val )
28+ if ( stack . length == 0 ) {
29+ return val
30+ }
31+ root = stack . shift ( )
32+ }
33+ }
34+ }
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