Neon
- Package
- purescript-neon
- Repository
- tfausak/purescript-neon
This is the top-level entry point into Neon. Typically to use Neon you would simply import this unqualified.
import Neon
If you want to avoid namespace collisions, you can import Neon qualified. It is a good idea to still import the operators unqualified, otherwise it will be annoying to use.
import Neon as N
import Neon.Operator
Here is an overview of the re-exported modules:
- Primitive: Functions for stuff built into PureScript, like
Number
. This is for basics like converting between numeric types. - Data: Re-exports from other libraries. Neon does not define any of its own data types.
- Effect: More re-exports. Neon also does not define any of its own effect types.
- Class: This is why Neon exists. These classes are what separate Neon
from the
Prelude
. - Helper: Things built on top of everything else. These are all nice to have but can be derived from simpler things.
- Operator: A handful of operators for doing common tasks. Neon doesn't go overboard with operators like many other PureScript libraries.
All of Neon's documentation assumes you are familiar with the :
operator. If you aren't, you should start by reading its documentation.
Re-exports from Neon.Class
#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 and
s the results together.
even :and odd -- \ x -> (even x) :and (odd x)
(even :and odd) 3 -- false
Members
and :: a -> a -> a
Instances
#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
#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
#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
#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
HasFromArray a (Array a)
HasFromArray a (List a)
HasFromArray a (Maybe a)
HasFromArray Char String
#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
#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
(HasEqual a, HasGreater a) => HasGreater (Array a)
HasGreater Boolean
HasGreater Char
HasGreater Int
(HasEqual a, HasGreater a) => HasGreater (List a)
HasGreater Number
HasGreater Ordering
HasGreater String
#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
#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
#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
multiply :: a -> a -> a
Instances
#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 not
s
the result.
not even -- \ x -> not (even x)
(not even) 3 -- true
Members
not :: a -> a
Instances
#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 or
s the results together.
even :or odd -- \ x -> (even x) :or (odd x)
(even :or odd) 3 -- true
Members
or :: a -> a -> a
Instances
#HasRemainder Source
class HasRemainder a where
Represents types that are divisible.
7 :remainder 2 -- 1
7.0 :remainder 2.0 -- 1.0
Members
remainder :: a -> a -> a
Instances
#HasSubtract Source
class HasSubtract a where
Represents types that can be subtracted from each other.
3 :subtract 2 -- 1
Members
subtract :: a -> a -> a
Instances
(HasEqual a) => HasSubtract (Array a)
HasSubtract Int
(HasEqual a) => HasSubtract (List a)
HasSubtract Number
#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
HasToArray (Array a) a
HasToArray (List a) a
HasToArray (Maybe a) a
HasToArray String Char
#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.Data
#Tuple Source
data Tuple a b
A simple product type for wrapping a pair of component values.
Constructors
Tuple a b
Instances
(Show a, Show b) => Show (Tuple a b)
(Eq a, Eq b) => Eq (Tuple a b)
(Eq a) => Eq1 (Tuple a)
(Ord a, Ord b) => Ord (Tuple a b)
(Ord a) => Ord1 (Tuple a)
(Bounded a, Bounded b) => Bounded (Tuple a b)
Semigroupoid Tuple
(Semigroup a, Semigroup b) => Semigroup (Tuple a b)
The
Semigroup
instance enables use of the associative operator<>
onTuple
s whenever there areSemigroup
instances for the component types. The<>
operator is applied pairwise, so:(Tuple a1 b1) <> (Tuple a2 b2) = Tuple (a1 <> a2) (b1 <> b2)
(Monoid a, Monoid b) => Monoid (Tuple a b)
(Semiring a, Semiring b) => Semiring (Tuple a b)
(Ring a, Ring b) => Ring (Tuple a b)
(CommutativeRing a, CommutativeRing b) => CommutativeRing (Tuple a b)
(HeytingAlgebra a, HeytingAlgebra b) => HeytingAlgebra (Tuple a b)
(BooleanAlgebra a, BooleanAlgebra b) => BooleanAlgebra (Tuple a b)
Functor (Tuple a)
The
Functor
instance allows functions to transform the contents of aTuple
with the<$>
operator, applying the function to the second component, so:f <$> (Tuple x y) = Tuple x (f y)
Invariant (Tuple a)
Bifunctor Tuple
(Semigroup a) => Apply (Tuple a)
The
Functor
instance allows functions to transform the contents of aTuple
with the<*>
operator whenever there is aSemigroup
instance for thefst
component, so:(Tuple a1 f) <*> (Tuple a2 x) == Tuple (a1 <> a2) (f x)
Biapply Tuple
(Monoid a) => Applicative (Tuple a)
Biapplicative Tuple
(Semigroup a) => Bind (Tuple a)
(Monoid a) => Monad (Tuple a)
Extend (Tuple a)
Comonad (Tuple a)
(Lazy a, Lazy b) => Lazy (Tuple a b)
Foldable (Tuple a)
Bifoldable Tuple
Traversable (Tuple a)
Bitraversable Tuple
(TypeEquals a Unit) => Distributive (Tuple a)
#Ordering Source
data Ordering
The Ordering
data type represents the three possible outcomes of
comparing two values:
LT
- The first value is less than the second.
GT
- The first value is greater than the second.
EQ
- The first value is equal to the second.
Constructors
Instances
#Maybe Source
data Maybe a
The Maybe
type is used to represent optional values and can be seen as
something like a type-safe null
, where Nothing
is null
and Just x
is the non-null value x
.
Constructors
Instances
Functor Maybe
The
Functor
instance allows functions to transform the contents of aJust
with the<$>
operator:f <$> Just x == Just (f x)
Nothing
values are left untouched:f <$> Nothing == Nothing
Apply Maybe
The
Apply
instance allows functions contained within aJust
to transform a value contained within aJust
using theapply
operator:Just f <*> Just x == Just (f x)
Nothing
values are left untouched:Just f <*> Nothing == Nothing Nothing <*> Just x == Nothing
Combining
Functor
's<$>
withApply
's<*>
can be used transform a pure function to takeMaybe
-typed arguments sof :: a -> b -> c
becomesf :: Maybe a -> Maybe b -> Maybe c
:f <$> Just x <*> Just y == Just (f x y)
The
Nothing
-preserving behaviour of both operators means the result of an expression like the above but where any one of the values isNothing
means the whole result becomesNothing
also:f <$> Nothing <*> Just y == Nothing f <$> Just x <*> Nothing == Nothing f <$> Nothing <*> Nothing == Nothing
Applicative Maybe
The
Applicative
instance enables lifting of values intoMaybe
with thepure
orreturn
function (return
is an alias forpure
):pure x :: Maybe _ == Just x return x :: Maybe _ == Just x
Combining
Functor
's<$>
withApply
's<*>
andApplicative
'spure
can be used to pass a mixture ofMaybe
and non-Maybe
typed values to a function that does not usually expect them, by usingpure
for any value that is not alreadyMaybe
typed:f <$> Just x <*> pure y == Just (f x y)
Even though
pure = Just
it is recommended to usepure
in situations like this as it allows the choice ofApplicative
to be changed later without having to go through and replaceJust
with a new constructor.Alt Maybe
The
Alt
instance allows for a choice to be made between twoMaybe
values with the<|>
operator, where the firstJust
encountered is taken.Just x <|> Just y == Just x Nothing <|> Just y == Just y Nothing <|> Nothing == Nothing
Plus Maybe
The
Plus
instance provides a defaultMaybe
value:empty :: Maybe _ == Nothing
Alternative Maybe
The
Alternative
instance guarantees that there are bothApplicative
andPlus
instances forMaybe
.Bind Maybe
The
Bind
instance allows sequencing ofMaybe
values and functions that return aMaybe
by using the>>=
operator:Just x >>= f = f x Nothing >>= f = Nothing
Monad Maybe
The
Monad
instance guarantees that there are bothApplicative
andBind
instances forMaybe
. This also enables thedo
syntactic sugar:do x' <- x y' <- y pure (f x' y')
Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
MonadZero Maybe
Extend Maybe
The
Extend
instance allows sequencing ofMaybe
values and functions that accept aMaybe a
and return a non-Maybe
result using the<<=
operator.f <<= Nothing = Nothing f <<= Just x = Just (f x)
Invariant Maybe
(Semigroup a) => Semigroup (Maybe a)
The
Semigroup
instance enables use of the operator<>
onMaybe
values whenever there is aSemigroup
instance for the type theMaybe
contains. The exact behaviour of<>
depends on the "inner"Semigroup
instance, but generally captures the notion of appending or combining things.Just x <> Just y = Just (x <> y) Just x <> Nothing = Just x Nothing <> Just y = Just y Nothing <> Nothing = Nothing
(Semigroup a) => Monoid (Maybe a)
(Eq a) => Eq (Maybe a)
Eq1 Maybe
(Ord a) => Ord (Maybe a)
Ord1 Maybe
(Bounded a) => Bounded (Maybe a)
(Show a) => Show (Maybe a)
The
Show
instance allowsMaybe
values to be rendered as a string withshow
whenever there is anShow
instance for the type theMaybe
contains.
#List Source
data List a
Constructors
Instances
(Show a) => Show (List a)
(Eq a) => Eq (List a)
Eq1 List
(Ord a) => Ord (List a)
Ord1 List
Semigroup (List a)
Monoid (List a)
Functor List
FunctorWithIndex Int List
Foldable List
FoldableWithIndex Int List
Unfoldable List
Traversable List
TraversableWithIndex Int List
Apply List
Applicative List
Bind List
Monad List
Alt List
Plus List
Alternative List
MonadZero List
MonadPlus List
Extend List
Re-exports from Neon.Effect
#Eff Source
data Eff :: Row Effect -> Type -> Type
The Eff
type constructor is used to represent native effects.
See Handling Native Effects with the Eff Monad for more details.
The first type parameter is a row of effects which represents the contexts in which a computation can be run, and the second type parameter is the return type.
Instances
#unsafePerformEff Source
unsafePerformEff :: forall a eff. Eff eff a -> a
Run an effectful computation.
Note: use of this function can result in arbitrary side-effects.
Re-exports from Neon.Helper
#withDefault Source
withDefault :: forall a. a -> Maybe a -> a
If the given value is Nothing
, return the default. Otherwise return the
value.
withDefault 2 Nothing -- 2
withDefault 2 (Just 1) -- 1
#upTo Source
upTo :: forall a. HasFromInt a => HasGreater a => HasToInt a => a -> a -> Array a
Creates an array that ranges from the given lower bound down to the upper bound.
1 :upTo 3 -- [1, 2, 3]
1 :upTo 1 -- [1]
3 :upTo 1 -- []
#unsafeCoerce Source
unsafeCoerce :: forall b a. a -> b
A wildly unsafe function that can convince the type system that any value is any type. Use this carefully!
unsafeCoerce 1 :: Number -- 1.0
unsafeCoerce 'a' :: String -- "a"
#todo Source
todo :: forall a. a
A wildly unsafe function that can be used to stand in for any value.
todo :: Unit -- unit
todo :: Boolean -- ?
This should never end up in production, but it can be useful when developing a function.
#sign Source
sign :: forall a. HasGreater a => HasLess a => HasOne a => HasSubtract a => HasZero a => a -> a
Returns the sign of a number.
sign 2 -- 1
sign 0 -- 0
sign (-2) -- -1
#sequence Source
sequence :: forall c b a. HasApply b => HasMap b => HasTraverse a => HasPure b => a (b c) -> b (a c)
Sequences actions and collects the results.
sequence [Just 1, Just 2] -- Just [1, 2]
#reciprocal Source
reciprocal :: forall a. HasDivide a => HasOne a => a -> a
Returns the reciprocal of the value by dividing one by it.
reciprocal 2 -- 0.5
#product Source
product :: forall b a. HasMultiply b => HasOne b => HasReduce a => a b -> b
Multiplies all of the elements of a container together.
product [2, 3] -- 6
product [] -- 1
#print Source
print :: forall b a. HasInspect a => a -> Eff (console :: CONSOLE | b) Unit
Inspects a value and logs it.
print 123 -- (logs "123")
#notANumber Source
notANumber :: Number -> Boolean
Returns true
if the number is not a valid number. This is useful to test
for nan
.
notANumber 1 -- false
notANumber infinity -- false
notANumber nan -- true
#negate Source
negate :: forall a. HasSubtract a => HasZero a => a -> a
Negates the value by subtracting the value from zero.
negate 2 -- -2
#maximum Source
maximum :: forall b a. HasGreater b => HasReduce a => a b -> Maybe b
Returns the greatest value.
maximum [1, 3, 2] -- Just 3
maximum [] -- Nothing
#lessOrEqual Source
lessOrEqual :: forall a. HasEqual a => HasLess a => a -> a -> Boolean
Returns true if the value is less than or equal to the other.
2 :lessOrEqual 1 -- false
2 :lessOrEqual 2 -- true
2 :lessOrEqual 3 -- true
#increment Source
increment :: forall a. HasFromInt a => HasToInt a => a -> Maybe a
Increases a value by one. If the value is already the top, nothing will be returned.
increment 'a' -- Just 'b'
increment '\65535' -- Nothing
#greaterOrEqual Source
greaterOrEqual :: forall a. HasEqual a => HasGreater a => a -> a -> Boolean
Returns true if the value is greater than or equal to the other.
2 :greaterOrEqual 1 -- true
2 :greaterOrEqual 2 -- true
2 :greaterOrEqual 3 -- false
#downTo Source
downTo :: forall a. HasFromInt a => HasLess a => HasToInt a => a -> a -> Array a
Creates an array that ranges from the given upper bound down to the lower bound.
3 :downTo 1 -- [3, 2, 1]
1 :downTo 1 -- [1]
1 :downTo 3 -- []
#divisibleBy Source
divisibleBy :: forall a. HasEqual a => HasRemainder a => HasZero a => a -> a -> Boolean
Returns true if the number is divisible by the other.
9 :divisibleBy 3 -- true
8 :divisibleBy 3 -- false
#decrement Source
decrement :: forall a. HasFromInt a => HasToInt a => a -> Maybe a
Decreases a value by one. If the value is already the bottom, nothing will be returned.
decrement 'b' -- Just 'a'
decrement '\0' -- Nothing
#clamp Source
clamp :: forall a. HasGreater a => HasLess a => a -> a -> a -> a
Clamps a value between some bounds. If the lower bound is greater than the upper bound, they will be swapped.
2 :clamp 3 5 -- 3
4 :clamp 3 5 -- 4
6 :clamp 3 5 -- 5
6 :clamp 5 3 -- 5
#asTypeOf Source
asTypeOf :: forall a. a -> a -> a
A type-restricted version of always
.
[] :asTypeOf [1] -- [] :: Array Int
#absoluteValue Source
absoluteValue :: forall a. HasLess a => HasSubtract a => HasZero a => a -> a
Returns the absolute value of a number.
absoluteValue (-2) -- 2
absoluteValue 3 -- 3
Re-exports from Neon.Operator
#_subtract Source
_subtract :: forall a. HasSubtract a => a -> a -> a
#_remainder Source
_remainder :: forall a. HasRemainder a => a -> a -> a
#_multiply Source
_multiply :: forall a. HasMultiply a => a -> a -> a
#_greaterOrEqual Source
_greaterOrEqual :: forall a. HasEqual a => HasGreater a => a -> a -> Boolean
#_greater Source
_greater :: forall a. HasGreater a => a -> a -> Boolean
#(||) Source
Operator alias for Neon.Operator._or (right-associative / precedence 2)
Returns the logical disjunction of both arguments.
false || false -- false
false || true -- true
#(^) Source
Operator alias for Neon.Operator._power (right-associative / precedence 7)
Raises a number to a power. This is exponentiation, not exclusive or
(xor). In some JavaScript implementations, this would be **
.
2 ^ 3 -- 8
This operator is right-associative.
4 ^ 3 ^ 2 == 4 ^ (3 ^ 2) -- 262144
#(>=) Source
Operator alias for Neon.Operator._greaterOrEqual (non-associative / precedence 4)
Returns true
if the left argument is greater than or equal to the right.
2 >= 1 -- true
2 >= 2 -- true
2 >= 3 -- false
#(>) Source
Operator alias for Neon.Operator._greater (non-associative / precedence 4)
Returns true
if the left argument is greater than the right.
2 > 1 -- true
2 > 2 -- false
2 > 3 -- false
#(==) Source
Operator alias for Neon.Operator._equal (non-associative / precedence 4)
Returns true
if the two things are equal.
2 == 2 -- true
2 == 3 -- false
#(<=) Source
Operator alias for Neon.Operator._lessOrEqual (non-associative / precedence 4)
Returns true
if the left argument is less than or equal to the right.
2 <= 1 -- false
2 <= 2 -- true
2 <= 3 -- true
#(<) Source
Operator alias for Neon.Operator._less (non-associative / precedence 4)
Returns true
if the left argument is less than the right.
2 < 1 -- false
2 < 2 -- false
2 < 3 -- true
#(:) Source
Operator alias for Neon.Operator._call (left-associative / precedence 8)
Passes an argument to a function. This is reversed function application.
Since every function in Neon takes its "subject" last, it can be useful to
think of this operator like .
in object-oriented languages.
'a' :toUpper == toUpper 'a' -- "A"
"ab" :add "cd" == add "cd" "ab" -- "abcd"
This operator has the highest precedence so that it can be combined with other operators.
1.2 :round + 3 == (1.2 :round) + 3 -- 4
This operator is left associative. It is designed to be chained together.
'a' :toUpper :add "bc" :add "de" -- "abcde"
#(/) Source
Operator alias for Neon.Operator._divide (left-associative / precedence 6)
Divides a number by another number.
4 / 2 -- 2
5 / 2 -- 2
5.0 / 2.0 -- 2.5
#(-) Source
Operator alias for Neon.Operator._subtract (left-associative / precedence 5)
Subtracts one number from another.
3 - 2 -- 1
#(+) Source
Operator alias for Neon.Operator._add (left-associative / precedence 5)
Adds two numbers together.
2 + 3 -- 5
#(*) Source
Operator alias for Neon.Operator._multiply (left-associative / precedence 6)
Multiplies two numbers together.
2 * 3 -- 6
#(&&) Source
Operator alias for Neon.Operator._and (right-associative / precedence 3)
Returns the logical conjunction of both arguments.
true && false -- false
true && true -- true
#(%) Source
Operator alias for Neon.Operator._remainder (left-associative / precedence 6)
Finds the remainder after division.
4 % 2 -- 0
5 % 2 -- 1
5.0 % 2.0 -- 1.0
5.5 % 2.5 -- 0.5
#(!=) Source
Operator alias for Neon.Operator._notEqual (non-associative / precedence 4)
Returns true
if the two things are not equal.
2 != 3 -- true
2 != 2 -- false
Note that this is different than the Prelude
, which uses /=
for
inequality.
Re-exports from Neon.Primitive
#identity Source
identity :: forall a. a -> a
Returns the value it was given.
identity 1 -- 1
This is the identity function.
#flip Source
flip :: forall c b a. (a -> b -> c) -> (b -> a -> c)
Flips the first two arguments of a function.
"a" :add "b" -- "ab"
"a" :flip add "b" -- "ba"
#compose Source
compose :: forall c b a. (b -> c) -> (a -> b) -> (a -> c)
Returns a new function that calls the first function with the result of calling the second.
let addTwo x = x + 2
let double x = x * 2
let addTwoThenDouble x = addTwo :compose double
addTwoThenDouble 3 -- 10
This is function composition.
#always Source
always :: forall b a. a -> b -> a
Always returns the first argument.
"anything" :always 1 -- 1
This is the constant function.
- Modules
- Neon
- Neon.
Class - Neon.
Class. HasAdd - Neon.
Class. HasAnd - Neon.
Class. HasApply - Neon.
Class. HasBottom - Neon.
Class. HasChain - Neon.
Class. HasCompare - Neon.
Class. HasDivide - Neon.
Class. HasEqual - Neon.
Class. HasFilter - Neon.
Class. HasFromArray - Neon.
Class. HasFromInt - Neon.
Class. HasGreater - Neon.
Class. HasInspect - Neon.
Class. HasLess - Neon.
Class. HasMap - Neon.
Class. HasMultiply - Neon.
Class. HasNot - Neon.
Class. HasOne - Neon.
Class. HasOr - Neon.
Class. HasPower - Neon.
Class. HasPure - Neon.
Class. HasReduce - Neon.
Class. HasRemainder - Neon.
Class. HasSubtract - Neon.
Class. HasToArray - Neon.
Class. HasToInt - Neon.
Class. HasTop - Neon.
Class. HasTraverse - Neon.
Class. HasZero - Neon.
Data - Neon.
Effect - Neon.
Helper - Neon.
Operator - Neon.
Primitive - Neon.
Primitive. Char - Neon.
Primitive. Function - Neon.
Primitive. Int - Neon.
Primitive. Number
Allows
Tuple
s to be rendered as a string withshow
whenever there areShow
instances for both component types.