-
Notifications
You must be signed in to change notification settings - Fork 233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Injection / Bijection wanted? #25
Comments
Another extension to Monoid I was playing around with: // This protocol allows you to call plus directly on an instance.
protocol Addable {
func plus(x: Self) -> Self
}
// And an example of how to implement Addable for some type.
// This feels repetitive... there's certainly some better way to write this thing.
extension Int: Addable {
var monoid: IntMonoid { return IntMonoid() }
func plus(x: Int) -> Int {
return monoid.plus(self, x);
}
} Let me know! |
@sritchie if you renamed your Injection stuff to "read and show" or "printable" and "readable" yes. They're not bijections. @sritchie addable is just Semigroup. We have that merged in already https://github.com/maxpow4h/swiftz/blob/master/swiftz/Semigroup.swift I'm pretty sure we've got a lot of semigroup / monoid stuff,but having decent counting structures could be cool. though it might be a good idea to be conservative on how much gets in, i'm not sure how often ios/os x applications need server class approximate counting data structures. I assume you mean the stuff in https://github.com/twitter/algebird/tree/develop/algebird-core/src/main/scala/com/twitter/algebird ? |
@sritchie Hey! I am absolutely taking any PRs of associative data structures, like in Algebird. As @cartazio mentioned, I have added a couple of instances semigroup and monoid. They are here. https://github.com/maxpow4h/swiftz/blob/27031fea2ce7496715e4d5345149fd8f156b24e5/swiftzTests/swiftzTests.swift#L268-L277 More useful instances would be very good! I just added those ~3 to define a As for Bijection / Injection, they would be very useful! My only ask, is if you could name them Thanks! 😃 |
@maxpow4h I was not aware that prisms were injections? [Sorry folks, pressed the wrong button there!] |
@maxpow4h oh, those are Lens Isos? I thought it was A->String and String->A only, rather than proper Isos ala lens |
@jonsterling That protocol is a Simple Prism. I.e. |
ohh, yeah, they are Isos and prisms, my bad :) |
Oh man, somehow i wasn't getting updates for this thread. Glad I checked in. Let me read up, one sec. |
@cartazio As the author of that library, I COMPLETELY understand, and will for all time, that those examples are not Bijections. That doesn't mean that bijection isn't a useful typeclass. (The Bijection library is mostly full of injections,btw.) @maxpow4h about the Monoid and Semigroup, I did a bit more playing around and came up with this: https://gist.github.com/sritchie/27bdf9329027d5cdf02e#file-algebra-swift-L70 Using "Self" removes the need for the duplication in the type parameter. You also don't have to supply the monoid or semigroup instance as the first argument anymore. Happy to make that change too and update the tests if you're interested. |
@cartazio that's correct about those algebird data structures, I'd love to get some of those in. |
@maxpow4h about the Injection as a Prism, I'll have to read up. |
@sritchie the idea is the a-> Either a b is an injection (aka prism) |
@cartazio yeah, I get that (the injection vs bijection thing). It's a prism vs an injection because of the extra type parameter on Either, correct? |
http://hackage.haskell.org/package/lens-4.2/docs/Control-Lens-Prism.html#g:2 shows some machinery for prisms replicating it inline type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
-- some ways to construct prisms
prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
--- a way to construct a simple prism
prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b
|
so in the really simple case we could have prism' :: (a-> s) -> (s -> Maybe a) -> Prism s s a a where (this is just a teeny piece of the awesome generality of lens (which I myself am still learning)) so a simple prism might be formed out of foo :: a-> Either a b
bar :: Either a b -> Maybe a |
Yeah, so #34 has convinced me So a "Prism", which would be a Function, becomes a class. protocol Prism {
let apply: A -> B
let invert: B -> A?
} exactly the same as the --Bijection-- Injection class definition. Edited that, accidentally hit ctrl enter |
We are talking about the same things, I would just prefer to use the Lens version of terminology 😃 |
'Bijection class definition.' dont you mean injection? |
Yes. |
Advancement of the Data.Lens stuff makes this issue a bit irrelevant at the moment. Closing. |
Hey guys,
I'm the author of Twitter's Bijection library in Scala, and have found the Injection and Bijection typeclasses to be extremely useful. I'd love to get these, and a few other typeclasses, into swiftz. Here's some extremely early playing around:
Would you guys be interested in taking this pull req? I don't want to send it over if this is beyond the library scope.
One other Q:
The text was updated successfully, but these errors were encountered: