|
ContentsSide effectsEven if the functional programming style prefers writing pure functions without side-effects, sometimes side-effects are the reason for running the function in the first place. This section lists the most important functions with side-effects that are defined in the SCL standard library. Printingprint :: Show a => a -> <Proc> () (Prelude) Prints the given value in the console. printString :: String -> <Proc> () (Prelude) Prints the given string to the console. printError :: String -> <Proc> () (Prelude) Prints an error message to the console. printingToFile :: String -> <b> a -> <b> a (Prelude)
Referencesref :: a -> <Proc> Ref a (Prelude) Creates a new reference with the given initial value. getRef :: Ref a -> <Proc> a (Prelude) Returns the current value of the reference. (:=) :: Ref a -> a -> <Proc> () (Prelude) Sets a new value for the reference. Mutable arraysdata T a (ArrayList) Type of lists. new :: () -> <Proc> ArrayList.T a (ArrayList) Constructs a new list. add :: ArrayList.T a -> a -> <Proc> () (ArrayList) Adds an element to the list. remove :: ArrayList.T a -> Integer -> <Proc> a (ArrayList) Removes the i:th element of the list and returns it. Indexing starts from zero. get :: ArrayList.T a -> Integer -> <Proc> a (ArrayList) Gets the i:th element of the list. Indexing starts from zero. length :: ArrayList.T a -> <Proc> Integer (ArrayList) The current length of the list. contains :: ArrayList.T a -> a -> <Proc> Boolean (ArrayList) Returns iter :: (a -> <b> ()) -> ArrayList.T a -> <Proc,b> () (ArrayList) Iterates through the list. The elements added during the iteration are also iterated. The length is re-read before every step, so appending to the list from inside the
function extends the iteration instead of failing or being ignored. That makes
this usable as a worklist loop, and also means the iteration does not terminate if
the function always appends. Example:
The for :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> () (ArrayList)
mapInPlace :: (a -> <b> a) -> ArrayList.T a -> <Proc,b> ArrayList.T a (ArrayList) Replaces every element of the list by the result of applying the element to the given function. The given list is mutated and the very same list is returned; the result is not a copy, so the return value can be ignored. Unlike Examples:
Only the two original elements are mapped here, even though the list grows to four:
popUntilEmpty :: ArrayList.T a -> (a -> <b> ()) -> <Proc,b> () (ArrayList) Pops the last element of the list until list becomes empty. Elements are consumed from the end of the list, so this is a LIFO worklist drain, not a front-to-back traversal: the function is called with the elements in reverse order and the list is left empty. Because the last element is re-read on every step, the function may push more work onto the list. Note the argument order, which is the reverse of Example:
Mutable maps and setsIn addition to mutable arrays, SCL provides mutable map and set structures: data T a b (MMap) A mutable map from keys of type data T a (MSet) A mutable set of values of type Both support the usual insert/lookup/remove operations. When the mutable phase is
complete, convert to an immutable value with Escaping side-effectsrunProc :: <Proc,b> a -> <b> a (Builtin)
Transactions and effect conversion
Effects propagate upward: if a function calls another function with effect Monad sequencing with
|