Git Product home page Git Product logo

purescript-contravariant's Introduction

PureScript

A small strongly typed programming language with expressive types that compiles to JavaScript, written in and inspired by Haskell.

Hackage Build Status

Language info

Resources

Help!

Community Spaces

The following spaces are governed by the PureScript Community Code of Conduct. The majority of PureScript users use these spaces to discuss and collaborate on PureScript-related topics:

Unaffiliated Spaces

Some PureScript users also collaborate in the below spaces. These do not fall under the code of conduct linked above. They may have no code of conduct or one very different than the one linked above.

purescript-contravariant's People

Contributors

chfi avatar garyb avatar hdgarrood avatar jordanmartinez avatar kl0tl avatar mankykitty avatar milesfrain avatar paf31 avatar paluh avatar rhendric avatar thimoteus avatar thomashoneyman avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

purescript-contravariant's Issues

Add `>*<` alias for `divided`

In haskell contravariant we can find a new operator >*< which is an alias for divided. It allows us to nicely capture duality of Divide and Apply - we can write:

nonNegative = Predicate (_ >= 0)

q = (_.x &&& _.y &&& _.z) >$< nonNegative >*< nonNegative >*< nonNegative

Should I provide a PR with such addition to the library?

Add alias for `chosen`

Same as #32

For example, if >|<, then we can write

choser = case _ of
  x | x < 0 then in1 x
    | x > 0 then in2 x
    | otherwise then in3 x
f4 = choser >$< f1 >|< f2 >|< f3

This is not the reverse of <|> so there may be a more appropriate alias.

New release for 0.13.0?

Hello!

The new version of the compiler is having a bad time with the self import that has been fixed in #21 - would it be possible to do a new release with this fix in?

Thanks!
Dan

Add Free Divisible functor

I think this could potentially be useful in some rare cases:

import Prelude

import Data.Functor.Contravariant (class Contravariant, cmap)
import Data.Divide (class Divide, divide)
import Data.Divisible (class Divisible, conquer)
import Data.Monoid (class Monoid)
import Data.Newtype (un)
import Data.Op (Op(..))
import Data.Profunctor.Strong (first)
import Data.Tuple (Tuple(..), snd)
import Unsafe.Coerce (unsafeCoerce)

data Divided_ f a b c = Divided_ (a -> Tuple b c) (f b) (Free f c)
data Divided (f :: Type -> Type) a

divided :: forall f a b c. Divided_ f a b c -> Divided f a
divided = unsafeCoerce

unDivided :: forall f a r. (forall b c. Divided_ f a b c -> r) -> Divided f a -> r
unDivided = unsafeCoerce

data Free f a
  = Conquer
  | Divide (Divided f a)
  
instance contravariantFree :: Contravariant (Free f) where
  cmap _ Conquer = Conquer
  cmap f (Divide d) = Divide (unDivided (\(Divided_ g x y) -> divided (Divided_ (g <<< f) x y)) d)
  
instance divideFree :: Contravariant f => Divide (Free f) where
  divide f x y = 
    case x of
      Conquer -> cmap (snd <<< f) y
      Divide d -> unDivided (\(Divided_ g a b) -> 
        case divide id b y of
          c -> Divide (divided (Divided_ (assoc <<< first g <<< f) a c))) d
    where
      assoc :: forall a b c. Tuple (Tuple a b) c -> Tuple a (Tuple b c)
      assoc (Tuple (Tuple x y) z) = Tuple x (Tuple y z)
  
instance divisibleFree :: Contravariant f => Divisible (Free f) where
  conquer = Conquer
  
interpretFree :: forall f g. Divisible g => (f ~> g) -> Free f ~> g
interpretFree _ Conquer = conquer
interpretFree f (Divide d) = unDivided (\(Divided_ g x y) -> divide g (f x) (interpretFree f y)) d

foldFree :: forall f m a. Monoid m => (forall b. f b -> b -> m) -> Free f a -> a -> m
foldFree f = un Op <<< interpretFree (Op <<< f)

Runnable version

`Comparison` could work for any monoid not just Ordering

-- | An adaptor allowing `>$<` to map over the inputs of a comparison function.
newtype Comparison o a = Comparison (a -> a -> o)

derive instance newtypeComparison :: Newtype (Comparison o a) _

instance contravariantComparison :: Contravariant (Comparison o) where
  cmap f (Comparison g) = Comparison (g `on` f)

instance semigroupComparison :: Semigroup o => Semigroup (Comparison o a) where
  append (Comparison p) (Comparison q) = Comparison (p <> q)

instance monoidComparison :: Monoid o => Monoid (Comparison o a) where
  mempty = Comparison (\_ _ -> mempty) -- mempty of `Ordering` is `EQ`

-- | The default comparison for any values with an `Ord` instance.
defaultComparison :: forall a. Ord a => Comparison Ordering a
defaultComparison = Comparison compare

No instance found for Prelude.Semigroup (a6536 -> a6536 -> Prelude.Ordering)

Hi,

I am using purescript-contravariant with psc version 0.6.8 and it gives me:

Error at bower_components/purescript-contravariant/src/Data/Comparison.purs line 16, column 1 - line 19, column 1:
Error in declaration semigroupComparison
No instance found for Prelude.Semigroup (a6536 -> a6536 -> Prelude.Ordering)

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.