Module

Neon.Class

Package
purescript-neon
Repository
tfausak/purescript-neon

Every type class in Neon starts with "Has". There are two reasons for this:

  • It avoids collisions with data types. The HasMap type class is distinct from the Map data type.

  • Each type class has one function, so naming the class after that function results in better error messages.

    newtype MyArray a = MyArray (Array a)
    MyArray [1, 2, 3] :map (_ + 1)
    -- Error found:
    --   No type class instance was found for
    --     Neon.Class.HasMap.HasMap MyArray
    

Re-exports from Neon.Class.HasAdd

#HasAdd Source

class HasAdd a  where

Represents types that can be added together. This is also known as a semigroup.

2 :add 1 -- 3
"a" :add "b" -- "ab"

Members

  • add :: a -> a -> a

Instances

Re-exports from Neon.Class.HasAnd

#HasAnd Source

class HasAnd a  where

Represents types that can be conjoined. This is also known as logical conjunction.

true :and false -- false

Some types can't be conjoined per se but it is still useful to be able to use the && operator with them. For example, you can and two arrays together to get their intersection.

[1, 2, 3] :and [2, 3, 4] -- [2, 3]

For integers, and is bitwise.

5 :and 3 -- 3

The instance for functions is perhaps the hardest to understand. Combining two functions with and returns a new function that calls each function and then ands the results together.

even :and odd -- \ x -> (even x) :and (odd x)
(even :and odd) 3 -- false

Members

  • and :: a -> a -> a

Instances

Re-exports from Neon.Class.HasApply

#HasApply Source

class HasApply a  where

Represents types that can be applied from within a container. In other words, given both a function and a value in a container, apply the function to the value and return the result in a container. This is also known as an applicative functor.

[3, 4] :apply [(_ + 2), (_ * 2)] -- [5, 6, 6, 8]
Just 2 :apply (Just (_ + 1)) -- Just 3

Members

  • apply :: forall c b. a (b -> c) -> a b -> a c

Instances

Re-exports from Neon.Class.HasBottom

#HasBottom Source

class HasBottom a  where

Represents types that have a lower bound.

bottom :: Boolean -- false
bottom :: Char -- '\0'

Members

Instances

Re-exports from Neon.Class.HasChain

#HasChain Source

class HasChain a  where

Represents types that can express sequential actions. This is also known as a monad.

[3, 5] :chain (\ x -> [x, x * 2]) -- [3, 6, 5, 10]

Members

  • chain :: forall c b. (b -> a c) -> a b -> a c

Instances

Re-exports from Neon.Class.HasCompare

#HasCompare Source

class (HasEqual a, HasGreater a, HasLess a) <= HasCompare a  where

Represents type that have a total order.

2 :compare 1 -- GT
2 :compare 2 -- EQ
2 :compare 3 -- LT

Members

Instances

Re-exports from Neon.Class.HasDivide

#HasDivide Source

class HasDivide a  where

Represents types that are divisible.

7 :divide 2 -- 3
7.0 :divide 2.0 -- 3.5

Members

Instances

Re-exports from Neon.Class.HasEqual

#HasEqual Source

class HasEqual a  where

Represents types that can be equal to each other.

equal 1 2 -- false
equal 3 3 -- true

Members

Instances

Re-exports from Neon.Class.HasFilter

#HasFilter Source

class HasFilter a  where

Represents types that can have elements filtered out of them.

[1, 2, 3, 4] :filter (_ > 2) -- [3, 4]

Members

Instances

Re-exports from Neon.Class.HasFromArray

#HasFromArray Source

class HasFromArray a b  where

Represents types that can be converted from an array.

fromArray [1] :: List Int -- Cons 1 Nil
fromArray [1] :: Maybe Int -- Just 1
fromArray ['a', 'b'] :: String -- "ab"

Members

Instances

Re-exports from Neon.Class.HasFromInt

#HasFromInt Source

class HasFromInt a  where

Represents types that can be converted from integers. This is typically used for enumerations.

fromInt 1 :: Maybe Bool -- Just true
fromInt 2 :: Maybe Bool -- Nothing

Members

Instances

Re-exports from Neon.Class.HasGreater

#HasGreater Source

class HasGreater a  where

Represents types where one value can be greater than another.

2 :greater 1 -- true
1 :greater 2 -- false

Members

Instances

Re-exports from Neon.Class.HasInspect

#HasInspect Source

class HasInspect a  where

Represents types that can be converting to a string. This is typically used for debugging. The result of inspect x should be a valid PureScript expression.

inspect 123 -- "123"
inspect (Just 123) -- "Just (123)"

The instance for functions and objects do not return valid expressions. This is because there is no way in general to generate an expression for them.

inspect identity -- "{- Function -}"
inspect {} -- "{- Object -}"

Members

Instances

Re-exports from Neon.Class.HasLess

#HasLess Source

class HasLess a  where

Represents types where one value can be less than another.

1 :less 2 -- true
2 :less 1 -- false

Members

Instances

Re-exports from Neon.Class.HasMap

#HasMap Source

class HasMap a  where

Represents types that can be mapped over. This is also know as a functor.

[1, 2, 3] :map (_ + 1) -- [2, 3, 4]

Members

  • map :: forall c b. (b -> c) -> a b -> a c

Instances

Re-exports from Neon.Class.HasMultiply

#HasMultiply Source

class HasMultiply a  where

Represents values that can be multiplied together. This is also known as a near-ring.

2 :multiply 3 -- 6

Members

Instances

Re-exports from Neon.Class.HasNot

#HasNot Source

class HasNot a  where

Represents types can be negated. This is known as negation.

not false -- true

The instance for functions is a little tricky. Calling not on a function returns a new function that calls the original function and then nots the result.

not even -- \ x -> not (even x)
(not even) 3 -- true

Members

Instances

Re-exports from Neon.Class.HasOne

#HasOne Source

class HasOne a  where

Represents types that have an multiplicative identity. This is also known as a semiring.

one :: Int -- 1
one :: Number -- 1.0

Members

Instances

Re-exports from Neon.Class.HasOr

#HasOr Source

class HasOr a  where

Represents types that can be disjoined. This is also known as logical disjunction.

true :or false -- true

Some types can't be disjoined per se but it is still useful to be able to use the || operator with them. For example, you can or two arrays together to get their union.

[1, 2, 3] :or [2, 3, 4] -- [1, 2, 3, 4]

For integers, or is bitwise.

5 :or 2 -- 7

The instance for functions is perhaps the hardest to understand. Combining two functions with or returns a new function that calls each function and then ors the results together.

even :or odd -- \ x -> (even x) :or (odd x)
(even :or odd) 3 -- true

Members

  • or :: a -> a -> a

Instances

Re-exports from Neon.Class.HasPower

#HasPower Source

class HasPower a  where

Represents types that can be exponentiated.

2 :power 3 -- 8

Members

Instances

Re-exports from Neon.Class.HasPure

#HasPure Source

class HasPure a  where

Represents types that allow injecting values into a container.

pure 1 :: Maybe Int -- Just 1
pure 1 :: Array Int -- [1]

Members

  • pure :: forall b. b -> a b

Instances

Re-exports from Neon.Class.HasReduce

#HasReduce Source

class HasReduce a  where

Represents types that can be reduced to a single value. This is also known as a fold.

["wo", "rl", "d!"] :reduce (\ a e -> a + e) "hello" -- "helloworld!"

Members

  • reduce :: forall c b. (c -> b -> c) -> c -> a b -> c

Instances

Re-exports from Neon.Class.HasRemainder

#HasRemainder Source

class HasRemainder a  where

Represents types that are divisible.

7 :remainder 2 -- 1
7.0 :remainder 2.0 -- 1.0

Members

Instances

Re-exports from Neon.Class.HasSubtract

#HasSubtract Source

class HasSubtract a  where

Represents types that can be subtracted from each other.

3 :subtract 2 -- 1

Members

Instances

Re-exports from Neon.Class.HasToArray

#HasToArray Source

class HasToArray a b  where

Represents types that can be converted to an array.

toArray (Cons 1 Nil) :: Array Int -- [1]
toArray (Just 1) :: Array Int -- [1]
toArray "ab" :: String -- ['a', 'b']

Members

Instances

Re-exports from Neon.Class.HasToInt

#HasToInt Source

class HasToInt a  where

Represents types that can be converted to integers. This is typically used for enumerations.

toInt false -- 0

Members

Instances

Re-exports from Neon.Class.HasTop

#HasTop Source

class HasTop a  where

Represents types that have an upper bound.

top :: Boolean -- true
top :: Char -- '\65535'

Members

Instances

Re-exports from Neon.Class.HasTraverse

#HasTraverse Source

class HasTraverse t  where

Represents data structures that can be traversed from left to right. Unlike Reduce, these structures can be traversed while keeping their shape.

[1, 2] :traverse (\ x -> x :inspect :Just) -- Just ["1", "2"]

Members

Instances

Re-exports from Neon.Class.HasZero

#HasZero Source

class HasZero a  where

Represents types that have an additive identity. This is also known as a monoid.

zero :: Int -- 0
zero :: Number -- 0.0

Members

Instances