范畴论中filter操作是否为态射?属于何种类型?
filter a morphism in category theory? Great question! Let's unpack how filter fits into category theory, using your Scala examples as context.
Short Answer
Yes, filter is considered a morphism, and in the context of Scala's type system (and the broader category of sets/types and functions), it's an endomorphism—a morphism that maps an object to itself.
Breakdown
First, a quick refresher: in category theory, a morphism is simply an arrow between two objects that adheres to the category's composition rules. For filter:
- The "objects" here are Scala types like
Seq[Int]. Everyfiltercall takes aSeq[Int]and returns anotherSeq[Int]—so the source and target object are the same, making it an endomorphism. - It respects composition rules: if you have two predicates
pandq, runningfilter(p)followed byfilter(q)is equivalent to filtering once withp && q. This aligns perfectly with how function composition works in the category of Scala functions.
Your Scala Examples in Context
Let's look at your code to make this tangible:
val myNums: Seq[Int] = Seq(-1, 3, -4, 2) myNums.filter(_ > 0) // Seq[Int] = List(3, 2) → subset, same type myNums.filter(_ > -99) // Seq[Int] = List(-1, 3, -4, 2) → identical to original myNums.filter(_ > 99) // Seq[Int] = List() → empty sequence, same type
No matter the predicate, filter always outputs a Seq[Int]—it never changes the type, just selects elements within the same "object" (type) structure. Even the empty sequence is a valid member of the Seq[Int] object, so the morphism stays within the same object's bounds.
A Narrower Set Theory Perspective
If we zoom into the category of sets (where objects are sets, morphisms are functions between sets), filter(p) corresponds to a function from the set of all integer sequences to itself. Again, this is an endomorphism—mapping elements of the set to other elements of the same set.
内容的提问来源于stack exchange,提问作者Polymerase

