Module

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

#all Source

all :: forall b a. HasReduce a => (b -> Boolean) -> a b -> Boolean

Returns true if all of the elements in the collection pass the predicate.

[2, 3] :all (_ > 1) -- true
[2, 1] :all (_ > 1) -- false

#any Source

any :: forall b a. HasReduce a => (b -> Boolean) -> a b -> Boolean

Returns true if any of the elements in the collection pass the predicate.

[1, 2] :any (_ > 1) -- true
[1, 0] :any (_ > 1) -- false

#asTypeOf Source

asTypeOf :: forall a. a -> a -> a

A type-restricted version of always.

[] :asTypeOf [1] -- [] :: Array Int

#bind Source

bind :: forall c b a. HasChain a => a b -> (b -> a c) -> a c

A version of chain with the arguments flipped. This is provided only to support desugaring do notation. It is not recommended to use explicitly.

#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

#contains Source

contains :: forall b a. HasEqual b => HasReduce a => b -> a b -> Boolean

Returns true if the container contains the element.

[1, 2, 3] :contains 2 -- true
[1, 0, 3] :contains 2 -- false

#curry Source

curry :: forall c b a. (Tuple a b -> c) -> (a -> b -> c)

Converts a function that operates on tuples to a normal function.

let f (Tuple x y) = x + y
curry f "a" "b" -- "ab"

#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 -- []

#empty Source

empty :: forall b a. HasReduce a => a b -> Boolean

Returns true if the container is empty.

empty [] -- true
empty [1] -- false

#even Source

even :: Int -> Boolean

Returns true if the number is even.

even 2 -- true
even 3 -- false

#flatten Source

flatten :: forall b a. HasChain a => a (a b) -> a b

Removes a level of nesting from a container.

flatten [[1, 2], [3, 4]] -- [1, 2, 3, 4]

#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

#infinite Source

infinite :: Number -> Boolean

Returns true if the number is infinite.

infinite infinity -- true
infinite (-infinity) -- true
infinite nan -- true
infinite 1.2 -- false

#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

#max Source

max :: forall a. HasGreater a => a -> a -> a

Returns the greater value.

max 1 2 -- 2
max 2 1 -- 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

#min Source

min :: forall a. HasLess a => a -> a -> a

Returns the lesser value.

min 1 2 -- 1
min 2 1 -- 1

#minimum Source

minimum :: forall b a. HasLess b => HasReduce a => a b -> Maybe b

Returns the least value.

minimum [2, 1, 3] -- Just 1
minimum [] -- 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

#notEqual Source

notEqual :: forall a. HasEqual a => a -> a -> Boolean

Returns true if the value is not equal to the other.

2 :notEqual 1 -- true
1 :notEqual 1 -- true

#odd Source

odd :: Int -> Boolean

Returns true if the number is odd.

odd 3 -- true
odd 4 -- false

#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

#size Source

size :: forall b a. HasReduce a => a b -> Int

Returns the size of a container.

size [2, 3, 5] -- 3
size [] -- 0

#sum Source

sum :: forall b a. HasAdd b => HasReduce a => HasZero b => a b -> b

Adds all the elements of a container together. If the container is empty, returns zero.

[1, 2, 3] :sum -- 6
([] :: Array Int) :sum -- 0
["ab", "cd", "ef"] :sum -- "abcdef"
([] :: Array String) :sum -- ""

#swap Source

swap :: forall b a. Tuple a b -> Tuple b a

Swaps the values in a tuple.

swap (Tuple 1 'a') -- Tuple 'a' 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.

#truncate Source

truncate :: Number -> Int

Removes the decimal part of a number and returns an integer.

truncate 1.0 -- 1
truncate 1.9 -- 1
truncate (-1.9) -- -1

#uncurry Source

uncurry :: forall c b a. (a -> b -> c) -> (Tuple a b -> c)

Converts a regular function into one that takes a tuple.

let f x y = x + y
uncurry f (Tuple "a" "b") -- "ab"

#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"

#unsafeLog Source

unsafeLog :: forall a. String -> a -> a

Unsafely write a string to the console.

unsafeLog "unsafe!" unit -- unit (logs "unsafe!")

#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 -- []

#void Source

void :: forall b a. HasMap a => a b -> a Unit

Replaces all values in the input container with unit. This is mostly useful for ignoring the value from an effect.

void [1, 2] -- [unit, unit]

#when Source

when :: forall a. HasPure a => Boolean -> a Unit -> a Unit

If the predicate is true, run the effect. Otherwise run an effect that does nothing.

when true [unit, unit] -- [unit, unit]
when false [unit, unit] -- [unit]

#while Source

while :: forall a. (a -> Boolean) -> (a -> a) -> a -> a

Keeps calling the function while the predicate is true.

1 :while (_ < 3) (_ + 1) -- 3
9 :while (_ < 3) (_ + 1) -- 9

#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