Giter VIP home page Giter VIP logo

Comments (20)

sritchie avatar sritchie commented on May 18, 2024

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!

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

@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 ?

from swiftz.

mxswd avatar mxswd commented on May 18, 2024

@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 Monoid type that can satisfy lots of different problems. (The technique I use in Monoid stops you from needing an Int, Int8, etc. version of everything).

As for Bijection / Injection, they would be very useful! My only ask, is if you could name them Isomorphism and Prism respectively.

Thanks! 😃

from swiftz.

jonsterling avatar jonsterling commented on May 18, 2024

@maxpow4h I was not aware that prisms were injections?

[Sorry folks, pressed the wrong button there!]

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

@maxpow4h oh, those are Lens Isos? I thought it was A->String and String->A only, rather than proper Isos ala lens

from swiftz.

mxswd avatar mxswd commented on May 18, 2024

@jonsterling That protocol is a Simple Prism. I.e. Prism s s a a.
But I don't think we can support polymorphic lenses any time soon ;)

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

ohh, yeah, they are Isos and prisms, my bad :)

from swiftz.

sritchie avatar sritchie commented on May 18, 2024

Oh man, somehow i wasn't getting updates for this thread. Glad I checked in. Let me read up, one sec.

from swiftz.

sritchie avatar sritchie commented on May 18, 2024

@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.

from swiftz.

sritchie avatar sritchie commented on May 18, 2024

@cartazio that's correct about those algebird data structures, I'd love to get some of those in.

from swiftz.

sritchie avatar sritchie commented on May 18, 2024

@maxpow4h about the Injection as a Prism, I'll have to read up.

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

@sritchie the idea is the a-> Either a b is an injection (aka prism)
because you can't write a safe Either a b -> a

from swiftz.

sritchie avatar sritchie commented on May 18, 2024

@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?

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

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

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

so in the really simple case we could have

prism' :: (a-> s) -> (s -> Maybe a) -> Prism s s a a

where s= Either a b
might be one choice in s

(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

from swiftz.

mxswd avatar mxswd commented on May 18, 2024

Yeah, so #34 has convinced me Control.Lens is not useful in Swift, but Data.Lens is.

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

from swiftz.

mxswd avatar mxswd commented on May 18, 2024

We are talking about the same things, I would just prefer to use the Lens version of terminology 😃

from swiftz.

cartazio avatar cartazio commented on May 18, 2024

'Bijection class definition.' dont you mean injection?

from swiftz.

mxswd avatar mxswd commented on May 18, 2024

Yes.

from swiftz.

CodaFi avatar CodaFi commented on May 18, 2024

Advancement of the Data.Lens stuff makes this issue a bit irrelevant at the moment. Closing.

from swiftz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.