Skip to content
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

Possible functions. #20

Closed
joneshf opened this issue May 2, 2015 · 5 comments
Closed

Possible functions. #20

joneshf opened this issue May 2, 2015 · 5 comments

Comments

@joneshf
Copy link
Member

joneshf commented May 2, 2015

How about some additional functions? Some are things I've used in the past, some are others I thought were sort of interesting, but might not be useful. Feel free to reject all of them.

Eq stuff

equaling :: forall b. (Eq b) => (a -> b) -> a -> a -> Boolean
equaling = on eq

Useful for things like intersectBy or nubBy.

Could also use a newtype to change the value of equality.

newtype Diff a = Diff a

instance (Eq a) => Eq (Diff a) where
  (==) (Diff x) (Diff y) = x /= y

Ord stuff

ascending :: forall a b. (Ord b) => (a -> b) -> a -> a -> Ordering
ascending = on compare

clamp :: forall a. (Ord a) => a -> a -> a -> a
clamp low high x = max low (min high x)

clamped :: forall a. (Ord a) => a -> a -> a -> Boolean
clamped low high x = low <= x && x <= high

descending :: forall a b. (Ord b) => (a -> b) -> a -> a -> Ordering
descending = on (flip compare)

max :: forall a. (Ord a) => a -> a -> a
max x y = if x <= y then y else x

min :: forall a. (Ord a) => a -> a -> a
min x y = if x <= y then x else y

ascending and descending are helpful for when you use something like sortBy. Also could use a newtype for changing the order of things, ala haskell:

newtype Desc a = Desc a

instance eqDesc :: (Eq a) => Eq (Desc a) where
  (==) (Desc x) (Desc y) = x == y

instance ordDesc :: (Ord a) => Ord (Desc a) where
  compare (Desc x) (Desc y) = compare y x

Semiring stuff

inc :: forall a. (Semiring a) => a -> a
inc = add one

Ring stuff

abs :: forall a. (Ord a, Ring a) => a -> a
abs x = if x <= zero then negate x else x

dec :: forall a. (Ring a) => a -> a
dec = flip sub one

sigNum :: forall a. (Ord a, ModuloSemiring a) => a -> a
sigNum x
  | x == zero = zero 
  | otherwise =  x `div` abs x
@paf31
Copy link
Contributor

paf31 commented May 2, 2015

I like most of this:

  • Diff isn't an equivalence relation.
  • I would use succ and pred instead of inc and dec personally. inc suggests mutation to me.

You might like @hdgarrood 's purescript-posets library too.

@paf31
Copy link
Contributor

paf31 commented May 2, 2015

Oh, also maybe s/clamped/between?

@joneshf
Copy link
Member Author

joneshf commented May 2, 2015

  • Diff isn't an equivalence relation.

Oh, oops.

  • I would use succ and pred instead of inc and dec personally. inc suggests mutation to me.

Sure.

You might like @hdgarrood 's purescript-posets library too.

Very nice!

Oh, also maybe s/clamped/between?

Sure.

hdgarrood added a commit to hdgarrood/purescript-prelude that referenced this issue May 27, 2015
hdgarrood added a commit to hdgarrood/purescript-prelude that referenced this issue May 27, 2015
@hdgarrood
Copy link
Contributor

The Ord stuff is now all either in prelude or purescript-orders.

I would also like to see generalised abs and signum, but I wonder if they should be members of some type class. I think this deserves a separate issue, I'm going to write this up when I get a moment, so perhaps we should close this?

@joneshf
Copy link
Member Author

joneshf commented Mar 29, 2016

Yeah. Most of this stuff can be implemented better with https://pursuit.purescript.org/packages/purescript-contravariant/0.2.3

@joneshf joneshf closed this as completed Mar 29, 2016
JordanMartinez added a commit that referenced this issue Dec 24, 2020
* first commit

* Fix instances for record fields

* Break modules up

* Deriving Show (#5)

* Initial work on deriving Show

* Add test for Show

* Remove import

* Travis etc.

* Data.Generic.Rep.Bounded (#6)

* Data.Generic.Rep.Bounded

Generic implementations of Prelude.Bounded class's top and bottom.

* GenericBounded - don't support product types

* GenericBounded - only support NoArguments

* Update for PureScript 0.11

* Add Generic instance for Maybe (#9)

* Add missing Bounded instances for Argument

* Add GenericEnum and GenericBoundedEnum

* Add enum tests, convert existing "tests" into assertions

* Product instances in Bounded and Enum

* Added GenericShowFields instances for NoConstructors and NoArguments (#20)

* Added Eq and Show instances to NoArguments and NoConstructors

* Added GenericShowFields

* Removed Show, Eq

* Cleanup

* Removed NoConstructors Show instance

* Remove Rec and Field & update package & bower symbols

* Bump deps for compiler/0.12

* Remove symbols and fix operator fixity issue

* Update dependencies, license

* Added HeytingAlgebra, Semiring, Ring

* Fix type annotation precedence in tests

* Replace monomorphic proxies by Type.Proxy.Proxy (#44)

* Remove Generic Maybe instance

* Remove Generic Enum from src and test

* Move all files to their correct folders and rename files to Generic.purs

* Update module names to match their file names

* Move test file for Data.Generic.Rep into proper folder and rename

* Update generic-rep test file module to match file path

* Rename generic-rep test name to testGenericRep

* Replace generic Show's  Foldable.intercalate usage with FFI

* Replace Tuple with Pair in Data.Generic.Rep tests

* Remove Maybe import from Data.Generic.Rep test file

* Remove Maybe import from Data.Generic.Rep

* Extract AlmostEff and assert to Test.Utils.purs file

* Update Data.Generic.Rep tests to use AlmostEff; include it in main tests

* Import implies in Data.Generic.Rep tests

Co-authored-by: Phil Freeman <paf31@cantab.net>
Co-authored-by: Matthew Leon <ml@matthewleon.com>
Co-authored-by: Gary Burgess <gary.burgess@gmail.com>
Co-authored-by: Liam Goodacre <goodacre.liam@gmail.com>
Co-authored-by: Jorge Acereda <jacereda@gmail.com>
Co-authored-by: Kristoffer Josefsson <kejace@gmail.com>
Co-authored-by: Denis Stoyanov <stoyanov.gr@gmail.com>
Co-authored-by: Harry Garrood <harry@garrood.me>
Co-authored-by: Cyril <sobierajewicz.cyril@gmail.com>
turlando pushed a commit to purescm/purescript-prelude that referenced this issue Sep 3, 2021
* first commit

* Fix instances for record fields

* Break modules up

* Deriving Show (#5)

* Initial work on deriving Show

* Add test for Show

* Remove import

* Travis etc.

* Data.Generic.Rep.Bounded (#6)

* Data.Generic.Rep.Bounded

Generic implementations of Prelude.Bounded class's top and bottom.

* GenericBounded - don't support product types

* GenericBounded - only support NoArguments

* Update for PureScript 0.11

* Add Generic instance for Maybe (purescript#9)

* Add missing Bounded instances for Argument

* Add GenericEnum and GenericBoundedEnum

* Add enum tests, convert existing "tests" into assertions

* Product instances in Bounded and Enum

* Added GenericShowFields instances for NoConstructors and NoArguments (purescript#20)

* Added Eq and Show instances to NoArguments and NoConstructors

* Added GenericShowFields

* Removed Show, Eq

* Cleanup

* Removed NoConstructors Show instance

* Remove Rec and Field & update package & bower symbols

* Bump deps for compiler/0.12

* Remove symbols and fix operator fixity issue

* Update dependencies, license

* Added HeytingAlgebra, Semiring, Ring

* Fix type annotation precedence in tests

* Replace monomorphic proxies by Type.Proxy.Proxy (purescript#44)

* Remove Generic Maybe instance

* Remove Generic Enum from src and test

* Move all files to their correct folders and rename files to Generic.purs

* Update module names to match their file names

* Move test file for Data.Generic.Rep into proper folder and rename

* Update generic-rep test file module to match file path

* Rename generic-rep test name to testGenericRep

* Replace generic Show's  Foldable.intercalate usage with FFI

* Replace Tuple with Pair in Data.Generic.Rep tests

* Remove Maybe import from Data.Generic.Rep test file

* Remove Maybe import from Data.Generic.Rep

* Extract AlmostEff and assert to Test.Utils.purs file

* Update Data.Generic.Rep tests to use AlmostEff; include it in main tests

* Import implies in Data.Generic.Rep tests

Co-authored-by: Phil Freeman <paf31@cantab.net>
Co-authored-by: Matthew Leon <ml@matthewleon.com>
Co-authored-by: Gary Burgess <gary.burgess@gmail.com>
Co-authored-by: Liam Goodacre <goodacre.liam@gmail.com>
Co-authored-by: Jorge Acereda <jacereda@gmail.com>
Co-authored-by: Kristoffer Josefsson <kejace@gmail.com>
Co-authored-by: Denis Stoyanov <stoyanov.gr@gmail.com>
Co-authored-by: Harry Garrood <harry@garrood.me>
Co-authored-by: Cyril <sobierajewicz.cyril@gmail.com>
turlando pushed a commit to purescm/purescript-prelude that referenced this issue Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants