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 7
7
},
8
8
"devDependencies" : {
9
9
"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 " ,
12
12
"rimraf" : " ^2.6.1"
13
13
}
14
14
}
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
13
13
compose :: forall b c d . a c d -> a b c -> a b d
14
14
15
15
instance 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 )
17
19
18
20
infixr 9 compose as <<<
19
21
Original file line number Diff line number Diff line change @@ -6,11 +6,21 @@ type AlmostEff = Unit -> Unit
6
6
7
7
main :: AlmostEff
8
8
main = do
9
+ functionComposition
9
10
testNumberShow show
10
11
testOrderings
11
12
testOrdUtils
12
13
testIntDegree
13
14
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
+
14
24
foreign import testNumberShow :: (Number -> String ) -> AlmostEff
15
25
foreign import throwErr :: String -> AlmostEff
16
26
You can’t perform that action at this time.
0 commit comments