Module

MCPrelude

This MCPrelude is a modified, PureScript version of the Haskell MCPrelude from the original Monad Challenges, which is a modified version of the Haskell Prelude designed specifically for The Monad Challenges.

#Seed

newtype Seed

A Seed for the pseudo-random generator rand

Instances

#mkSeed

#rand

#toLetter

#GreekData

#greekDataA

#greekDataB

#firstNames

#lastNames

#cardRanks

#cardSuits

#Triple

data Triple a b c

Constructors

Instances

Re-exports from Data.Array

#zipWith

zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> Array c

Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array.

If one array is longer, elements will be discarded from the longer array.

For example

zipWith (*) [1, 2, 3] [4, 5, 6, 7] == [4, 10, 18]

#zip

zip :: forall a b. Array a -> Array b -> Array (Tuple a b)

Takes two arrays and returns an array of corresponding pairs. If one input array is short, excess elements of the longer array are discarded.

zip [1, 2, 3] ["a", "b"] = [Tuple 1 "a", Tuple 2 "b"]

#takeWhile

takeWhile :: forall a. (a -> Boolean) -> Array a -> Array a

Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new array.

takeWhile (_ > 0) [4, 1, 0, -4, 5] = [4, 1]
takeWhile (_ > 0) [-1, 4] = []

#take

take :: forall a. Int -> Array a -> Array a

Keep only a number of elements from the start of an array, creating a new array.

letters = ["a", "b", "c"]

take 2 letters = ["a", "b"]
take 100 letters = ["a", "b", "c"]

#span

span :: forall a. (a -> Boolean) -> Array a -> { init :: Array a, rest :: Array a }

Split an array into two parts:

  1. the longest initial subarray for which all elements satisfy the specified predicate
  2. the remaining elements
span (\n -> n % 2 == 1) [1,3,2,4,5] == { init: [1,3], rest: [2,4,5] }

Running time: O(n).

#snoc

snoc :: forall a. Array a -> a -> Array a

Append an element to the end of an array, creating a new array.

snoc [1, 2, 3] 4 = [1, 2, 3, 4]

#replicate

replicate :: forall a. Int -> a -> Array a

Create an array containing a value repeated the specified number of times.

replicate 2 "Hi" = ["Hi", "Hi"]

#range

range :: Int -> Int -> Array Int

Create an array containing a range of integers, including both endpoints.

range 2 5 = [2, 3, 4, 5]

#length

length :: forall a. Array a -> Int

Get the number of elements in an array.

length ["Hello", "World"] = 2

#filter

filter :: forall a. (a -> Boolean) -> Array a -> Array a

Filter an array, keeping the elements which satisfy a predicate function, creating a new array.

filter (_ > 0) [-1, 4, -5, 7] = [4, 7]

#dropWhile

dropWhile :: forall a. (a -> Boolean) -> Array a -> Array a

Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new array.

dropWhile (_ < 0) [-3, -1, 0, 4, -6] = [0, 4, -6]

#drop

drop :: forall a. Int -> Array a -> Array a

Drop a number of elements from the start of an array, creating a new array.

letters = ["a", "b", "c", "d"]

drop 2 letters = ["c", "d"]
drop 10 letters = []

#cons

cons :: forall a. a -> Array a -> Array a

Attaches an element to the front of an array, creating a new array.

cons 1 [2, 3, 4] = [1, 2, 3, 4]

Note, the running time of this function is O(n).

#concatMap

concatMap :: forall a b. (a -> Array b) -> Array a -> Array b

Apply a function to each element in an array, and flatten the results into a single, new array.

concatMap (split $ Pattern " ") ["Hello World", "other thing"]
   = ["Hello", "World", "other", "thing"]

#concat

concat :: forall a. Array (Array a) -> Array a

Flatten an array of arrays, creating a new array.

concat [[1, 2, 3], [], [4, 5, 6]] = [1, 2, 3, 4, 5, 6]

#(:)

Operator alias for Data.Array.cons (right-associative / precedence 6)

An infix alias for cons.

1 : [2, 3, 4] = [1, 2, 3, 4]

Note, the running time of this function is O(n).

#(..)

Operator alias for Data.Array.range (non-associative / precedence 8)

An infix synonym for range.

2 .. 5 = [2, 3, 4, 5]

Re-exports from Data.Foldable

#foldl

foldl :: forall a b f. Foldable f => (b -> a -> b) -> b -> f a -> b

#foldr

foldr :: forall a b f. Foldable f => (a -> b -> b) -> b -> f a -> b

#sum

sum :: forall a f. Foldable f => Semiring a => f a -> a

Find the sum of the numeric values in a data structure.

#product

product :: forall a f. Foldable f => Semiring a => f a -> a

Find the product of the numeric values in a data structure.

#or

or :: forall a f. Foldable f => HeytingAlgebra a => f a -> a

The disjunction of all the values in a data structure. When specialized to Boolean, this function will test whether any of the values in a data structure is true.

#notElem

notElem :: forall a f. Foldable f => Eq a => a -> f a -> Boolean

Test whether a value is not an element of a data structure.

#elem

elem :: forall a f. Foldable f => Eq a => a -> f a -> Boolean

Test whether a value is an element of a data structure.

#any

any :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b

any f is the same as or <<< map f; map a function over the structure, and then get the disjunction of the results.

#and

and :: forall a f. Foldable f => HeytingAlgebra a => f a -> a

The conjunction of all the values in a data structure. When specialized to Boolean, this function will test whether all of the values in a data structure are true.

#all

all :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b

all f is the same as and <<< map f; map a function over the structure, and then get the conjunction of the results.

Re-exports from Data.Int

#toNumber

toNumber :: Int -> Number

Converts an Int value back into a Number. Any Int is a valid Number so there is no loss of precision with this function.

Re-exports from Data.String.CodeUnits

#fromCharArray

fromCharArray :: Array Char -> String

Converts an array of characters into a string.

fromCharArray ['H', 'e', 'l', 'l', 'o'] == "Hello"

Re-exports from Data.String.Utils

#words

words :: String -> Array String

Split a string into an array of strings which were delimited by white space characters.

Example:

words "Action is eloquence." == ["Action", "is", "eloquence."]

#lines

lines :: String -> Array String

Split a string into an array of strings which were delimited by newline characters.

Example:

lines "Action\nis\neloquence." == ["Action", "is", "eloquence."]

Re-exports from Data.Traversable

#scanr

scanr :: forall a b f. Traversable f => (a -> b -> b) -> b -> f a -> f b

Fold a data structure from the right, keeping all intermediate results instead of only the final result. Note that the initial value does not appear in the result (unlike Haskell's Prelude.scanr).

scanr (+) 0 [1,2,3] = [6,5,3]
scanr (flip (-)) 10 [1,2,3] = [4,5,7]

#scanl

scanl :: forall a b f. Traversable f => (b -> a -> b) -> b -> f a -> f b

Fold a data structure from the left, keeping all intermediate results instead of only the final result. Note that the initial value does not appear in the result (unlike Haskell's Prelude.scanl).

scanl (+) 0  [1,2,3] = [1,3,6]
scanl (-) 10 [1,2,3] = [9,7,4]

Re-exports from Data.Tuple

#Tuple

data Tuple a b

A simple product type for wrapping a pair of component values.

Constructors

Instances

#snd

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

Returns the second component of a tuple.

#fst

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

Returns the first component of a tuple.

Re-exports from Prelude

#Ordering

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

#Bounded

class (Ord a) <= Bounded a  where

The Bounded type class represents totally ordered types that have an upper and lower boundary.

Instances should satisfy the following law in addition to the Ord laws:

  • Bounded: bottom <= a <= top

Members

Instances

#Eq

class Eq a  where

The Eq type class represents types which support decidable equality.

Eq instances should satisfy the following laws:

  • Reflexivity: x == x = true
  • Symmetry: x == y = y == x
  • Transitivity: if x == y and y == z then x == z

Note: The Number type is not an entirely law abiding member of this class due to the presence of NaN, since NaN /= NaN. Additionally, computing with Number can result in a loss of precision, so sometimes values that should be equivalent are not.

Members

Instances

#EuclideanRing

class (CommutativeRing a) <= EuclideanRing a  where

The EuclideanRing class is for commutative rings that support division. The mathematical structure this class is based on is sometimes also called a Euclidean domain.

Instances must satisfy the following laws in addition to the Ring laws:

  • Integral domain: one /= zero, and if a and b are both nonzero then so is their product a * b
  • Euclidean function degree:
    • Nonnegativity: For all nonzero a, degree a >= 0
    • Quotient/remainder: For all a and b, where b is nonzero, let q = a / b and r = a `mod` b; then a = q*b + r, and also either r = zero or degree r < degree b
  • Submultiplicative euclidean function:
    • For all nonzero a and b, degree a <= degree (a * b)

The behaviour of division by zero is unconstrained by these laws, meaning that individual instances are free to choose how to behave in this case. Similarly, there are no restrictions on what the result of degree zero is; it doesn't make sense to ask for degree zero in the same way that it doesn't make sense to divide by zero, so again, individual instances may choose how to handle this case.

For any EuclideanRing which is also a Field, one valid choice for degree is simply const 1. In fact, unless there's a specific reason not to, Field types should normally use this definition of degree.

The EuclideanRing Int instance is one of the most commonly used EuclideanRing instances and deserves a little more discussion. In particular, there are a few different sensible law-abiding implementations to choose from, with slightly different behaviour in the presence of negative dividends or divisors. The most common definitions are "truncating" division, where the result of a / b is rounded towards 0, and "Knuthian" or "flooring" division, where the result of a / b is rounded towards negative infinity. A slightly less common, but arguably more useful, option is "Euclidean" division, which is defined so as to ensure that a `mod` b is always nonnegative. With Euclidean division, a / b rounds towards negative infinity if the divisor is positive, and towards positive infinity if the divisor is negative. Note that all three definitions are identical if we restrict our attention to nonnegative dividends and divisors.

In versions 1.x, 2.x, and 3.x of the Prelude, the EuclideanRing Int instance used truncating division. As of 4.x, the EuclideanRing Int instance uses Euclidean division. Additional functions quot and rem are supplied if truncating division is desired.

Members

  • div :: a -> a -> a
  • mod :: a -> a -> a

Instances

#Ord

class (Eq a) <= Ord a  where

The Ord type class represents types which support comparisons with a total order.

Ord instances should satisfy the laws of total orderings:

  • Reflexivity: a <= a
  • Antisymmetry: if a <= b and b <= a then a = b
  • Transitivity: if a <= b and b <= c then a <= c

Members

Instances

#Semiring

class Semiring a  where

The Semiring class is for types that support an addition and multiplication operation.

Instances must satisfy the following laws:

  • Commutative monoid under addition:
    • Associativity: (a + b) + c = a + (b + c)
    • Identity: zero + a = a + zero = a
    • Commutative: a + b = b + a
  • Monoid under multiplication:
    • Associativity: (a * b) * c = a * (b * c)
    • Identity: one * a = a * one = a
  • Multiplication distributes over addition:
    • Left distributivity: a * (b + c) = (a * b) + (a * c)
    • Right distributivity: (a + b) * c = (a * c) + (b * c)
  • Annihilation: zero * a = a * zero = zero

Note: The Number and Int types are not fully law abiding members of this class hierarchy due to the potential for arithmetic overflows, and in the case of Number, the presence of NaN and Infinity values. The behaviour is unspecified in these cases.

Members

Instances

#Show

class Show a  where

The Show type class represents those types which can be converted into a human-readable String representation.

While not required, it is recommended that for any expression x, the string show x be executable PureScript code which evaluates to the same value as the expression x.

Members

Instances

#conj

conj :: forall a. HeytingAlgebra a => a -> a -> a

#disj

disj :: forall a. HeytingAlgebra a => a -> a -> a

#identity

identity :: forall t a. Category a => a t t

#map

map :: forall a b f. Functor f => (a -> b) -> f a -> f b

#not

not :: forall a. HeytingAlgebra a => a -> a

#otherwise

otherwise :: Boolean

An alias for true, which can be useful in guard clauses:

max x y | x >= y    = x
        | otherwise = y

#notEq

notEq :: forall a. Eq a => a -> a -> Boolean

notEq tests whether one value is not equal to another. Shorthand for not (eq x y).

#min

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

Take the minimum of two values. If they are considered equal, the first argument is chosen.

#max

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

Take the maximum of two values. If they are considered equal, the first argument is chosen.

#lcm

lcm :: forall a. Eq a => EuclideanRing a => a -> a -> a

The least common multiple of two values.

#gcd

gcd :: forall a. Eq a => EuclideanRing a => a -> a -> a

The greatest common divisor of two values.

#flip

flip :: forall a b c. (a -> b -> c) -> b -> a -> c

Flips the order of the arguments to a function of two arguments.

flip const 1 2 = const 2 1 = 2

#const

const :: forall a b. a -> b -> a

Returns its first argument and ignores its second.

const 1 "hello" = 1

#comparing

comparing :: forall a b. Ord b => (a -> b) -> (a -> a -> Ordering)

Compares two values by mapping them to a type with an Ord instance.

#(||)

Operator alias for Data.HeytingAlgebra.disj (right-associative / precedence 2)

#(>>>)

Operator alias for Control.Semigroupoid.composeFlipped (right-associative / precedence 9)

#(>=)

Operator alias for Data.Ord.greaterThanOrEq (left-associative / precedence 4)

#(>)

Operator alias for Data.Ord.greaterThan (left-associative / precedence 4)

#(==)

Operator alias for Data.Eq.eq (non-associative / precedence 4)

#(<>)

Operator alias for Data.Semigroup.append (right-associative / precedence 5)

#(<=)

Operator alias for Data.Ord.lessThanOrEq (left-associative / precedence 4)

#(<<<)

Operator alias for Control.Semigroupoid.compose (right-associative / precedence 9)

#(<$>)

Operator alias for Data.Functor.map (left-associative / precedence 4)

#(<)

Operator alias for Data.Ord.lessThan (left-associative / precedence 4)

#(/=)

Operator alias for Data.Eq.notEq (non-associative / precedence 4)

#(/)

Operator alias for Data.EuclideanRing.div (left-associative / precedence 7)

#(-)

Operator alias for Data.Ring.sub (left-associative / precedence 6)

#(+)

Operator alias for Data.Semiring.add (left-associative / precedence 6)

#(*)

Operator alias for Data.Semiring.mul (left-associative / precedence 7)

#(&&)

Operator alias for Data.HeytingAlgebra.conj (right-associative / precedence 3)

#($)

Operator alias for Data.Function.apply (right-associative / precedence 0)

Applies a function to an argument: the reverse of (#).

length $ groupBy productCategory $ filter isInStock $ products

is equivalent to:

length (groupBy productCategory (filter isInStock products))

Or another alternative equivalent, applying chain of composed functions to a value:

length <<< groupBy productCategory <<< filter isInStock $ products

#(#)

Operator alias for Data.Function.applyFlipped (left-associative / precedence 1)

Applies an argument to a function: the reverse of ($).

products # filter isInStock # groupBy productCategory # length

is equivalent to:

length (groupBy productCategory (filter isInStock products))

Or another alternative equivalent, applying a value to a chain of composed functions:

products # filter isInStock >>> groupBy productCategory >>> length

Modules