Neon.Helper
- Package
- purescript-neon
- Repository
- tfausak/purescript-neon
Helper functions that are defined in terms of other primitives.
Many of these should be type class members with default implementations.
Unfortunately PureScript doesn't support that yet. They are defined in
here to make adding type class instances as easy as possible. The downside
is that things like size
are O(n) when they could be O(1) for some
container types.
#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
#asTypeOf Source
asTypeOf :: forall a. a -> a -> a
A type-restricted version of always
.
[] :asTypeOf [1] -- [] :: Array Int
#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
#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
#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
#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 -- []
#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
#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
#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
#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
#negate Source
negate :: forall a. HasSubtract a => HasZero a => a -> a
Negates the value by subtracting the value from zero.
negate 2 -- -2
#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
#print Source
print :: forall b a. HasInspect a => a -> Eff (console :: CONSOLE | b) Unit
Inspects a value and logs it.
print 123 -- (logs "123")
#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
#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
#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]
#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
#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.
#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"
#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 -- []
#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
- 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