File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -38,3 +38,17 @@ FoldlIsInjectTest >> test4 [
38
38
FoldlIsInjectTest >> test5 [
39
39
self assert: ({1 . 2 . 3 } inject: 4 into: [ :x :y | 2 * x + y ]) equals: 43
40
40
]
41
+
42
+ { #category : #tests }
43
+ FoldlIsInjectTest >> testFactorial0 [
44
+ self
45
+ assert: 0 factorialHylomorphically
46
+ equals: 1
47
+ ]
48
+
49
+ { #category : #tests }
50
+ FoldlIsInjectTest >> testFactorial5 [
51
+ self
52
+ assert: 5 factorialHylomorphically
53
+ equals: 120
54
+ ]
Original file line number Diff line number Diff line change
1
+ Extension { #name : #Integer }
2
+
3
+ { #category : #' *PreSmalltalks-Tests' }
4
+ Integer >> factorialHylomorphically [
5
+ self < 0 ifTrue: [self error: ' Not valid for negative integers' ].
6
+ ^ (1 to: self ) " anamorphism: unfold scalar to an inductive datatype"
7
+ inject: 1 into: #* " catamorphism: fold inductive datatype to scalar"
8
+ ]
You can’t perform that action at this time.
0 commit comments