Skip to content

Commit

Permalink
Initial sketch
Browse files Browse the repository at this point in the history
  • Loading branch information
shingarov committed May 28, 2024
1 parent 187ca4c commit 25ad0de
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Functors-Tests/FuntorialityTestTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Class {
#name : #FuntorialityTestTest,
#superclass : #TestCase,
#category : #'Functors-Tests'
}

{ #category : #tests }
FuntorialityTestTest >> checkFunctorialityFor: aClass [
self
assert: ((aClass with: 42) collect: #squared)
equals: (aClass with: 42 squared)
]

{ #category : #tests }
FuntorialityTestTest >> functorsUnderTest [
^{ Array. Bag. Set. OrderedCollection. Just }
]

{ #category : #tests }
FuntorialityTestTest >> testCommutativityEmpty [
self
assert: (nil collect: #moooooo)
equals: nil
]

{ #category : #tests }
FuntorialityTestTest >> testFunctoriality [
self functorsUnderTest do: [ :F | self checkFunctorialityFor: F ]
]

{ #category : #tests }
FuntorialityTestTest >> testIdentityEmpty [
self
assert: (nil collect: #yourself)
equals: nil
]
1 change: 1 addition & 0 deletions src/Functors-Tests/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #'Functors-Tests' }
5 changes: 5 additions & 0 deletions src/Functors/ApplicativeFunctor.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Class {
#name : #ApplicativeFunctor,
#superclass : #Functor,
#category : #Functors
}
11 changes: 11 additions & 0 deletions src/Functors/Class.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Extension { #name : #Class }

{ #category : #'*Functors' }
Class >> changeSuperclassTo: newSuperclass [
| oldSuperclass |
oldSuperclass := self superclass.
self basicSuperclass: newSuperclass.
oldSuperclass removeSubclass: self.
newSuperclass addSubclass: self.

]
22 changes: 22 additions & 0 deletions src/Functors/Functor.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Class {
#name : #Functor,
#superclass : #Object,
#category : #Functors
}

{ #category : #'class initialization' }
Functor class >> initialize [
Collection changeSuperclassTo: Functor.
]

{ #category : #'Functor API' }
Functor >> collect: aMorphism [
"Lift a morphism a→b to Fa→Fb.
The resulting morphism is expressed set-theoretically aka pointwise:
Assuming self is an element of Fa, and given the a→b,
answer an element of Fb.
Implementors of #collect: must respect identity and composition."

^self subclassResponsibility
]
41 changes: 41 additions & 0 deletions src/Functors/Just.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Class {
#name : #Just,
#superclass : #Maybe,
#instVars : [
'content'
],
#category : #Functors
}

{ #category : #'instance creation' }
Just class >> with: anObject [
^self basicNew
content: anObject;
yourself
]

{ #category : #comparing }
Just >> = rhs [
rhs class = Just ifFalse: [ ^false ].
^content = rhs content
]

{ #category : #'Functor API' }
Just >> collect: aBlock [
^Just with: (aBlock value: content)
]

{ #category : #private }
Just >> content [
^ content
]

{ #category : #private }
Just >> content: anObject [
content := anObject
]

{ #category : #comparing }
Just >> hash [
^content hash
]
10 changes: 10 additions & 0 deletions src/Functors/Maybe.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Class {
#name : #Maybe,
#superclass : #Functor,
#category : #Functors
}

{ #category : #'class initialization' }
Maybe class >> initialize [
UndefinedObject changeSuperclassTo: Maybe
]
6 changes: 6 additions & 0 deletions src/Functors/UndefinedObject.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Extension { #name : #UndefinedObject }

{ #category : #'*Functors' }
UndefinedObject >> collect: _ [
^self
]
1 change: 1 addition & 0 deletions src/Functors/package.st
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Package { #name : #Functors }

0 comments on commit 25ad0de

Please sign in to comment.