Built-In Functions

The Creamiest, Surgariest Part

C&S comes packaged with a few useful functions right off the bat. You don't need to import them or reference them on some kind of global C&S object. They are just available for you to use whenever you want.

Following is a list of all built-in functions as well as descriptions of how to use them and what you can expect them to return.

apply fun [, argsArray]

apply fn => 2 + 2 #=> 4
      
      apply fn x => x + 2, [2] #=> 4
      

Invokes fun and returns the result. If argsArray is provided, passes those arguments to the function when invoked.

aritize fun, arity

iterate list => iterate list, []
      iterate [], accum => accum
      iterate [h|t], accum => iterate t, accum << h
      
      # Only allow users to call `iterate`
      # with 1 argument
      export aritize iterate, 1
      

Returns a new function that can only be called with the number of arguments provided as arity. If the function is called with any other number of arguments, an error will be thrown.

arrayToTuple arr

arrayToTuple [ 1, 2, 3 ] #=> {{ 1, 2, 3 }}
      

Creates a new tuple from items in an array.

cache fun

foo = cache fn x => x
      
      foo 4 #=> 4
      
      foo 6 #=> 4
      

The cache function takes a function as its argument and returns a new function that will cache its result after being executed once. Having been cached, you can call this function as many times as you want and it will immediately return the cached value without having to execute the original function again.

Cached functions may be "reset" by calling the decache bif.

create klass [, ...constructorArgs]

create Date
      #=> Fri Sep 09 2016 17:00:43 GMT-0400 (EDT)
      
      create Error, "This is error text."
      #=> Error: This is error text(…)
      

Creates a new instance of klass by passing constructorArgs to the constructor. Returns the new class instance.

dangerouslyMutate key, value, collection

location.href #=> 'http://example.com'
      
      dangerouslyMutate 'href', '/foo', location
      
      location.href #=> 'http://example.com/foo'
      

Breaks the functionalism paradigm by mutating an existing object instead of creating a new one. This is necessary for certain JavaScript techniques such as updating location.href.

dataType data

dataType 'hello' #=> 'string'
      
      dataType 3.4 #=> 'number'
      
      dataType NaN #=> 'nan'
      
      dataType null #=> 'null'
      
      dataType [1, 2, 3] #=> 'array'
      
      dataType /^foo$/ #=> 'regexp'
      
      dataType <div></div> #=> 'htmlelement'
      
      dataType (create Date) #=> 'date'
      
      dataType undefined #=> 'undefined'
      
      dataType OK #=> 'atom'
      
      dataType (spawn fn => 'hello') #=> 'process'
      
      dataType fn => 'hello' #=> 'function'
      
      dataType {foo: 'bar'} #=> 'object'
      
      dataType {{ x, y }} #=> 'tuple'
      

Intelligently and reasonably assesses data types and returns a string identifying the type of data. Possible return values include:

debug message

debug 'Something was weird'
      #=> undefined
      

A shortcut for JavaScript's console.debug(message). Attempts to default to console.log if console.debug does not exist. If the console object does not exists, fails silently.

decache fun

foo = cache fn x => x
      foo 4 #=> 4
      foo 6 #=> 4
      
      decache foo
      foo 6 #=>
      

The cache function caches a function such that it stores its result having been executed one time. Calling decache on that same function will reset it such that it will cache itself again on the next execution.

die message

die 'This app is not working'
      #=> undefined
      

A shortcut for JavaScript's throw new Error(message).

dom selector

dom '#my-div' #=> HTMLElement
      
      dom 'div' #=> HTMLElement
      

Locates and returns a single DOM element identified by selector. If no element was found, returns null.

domArray selector

domArray '#my-div'
      #=> [HTMLElement]
      
      domArray 'div'
      #=> [HTMLElement, HTMLElement, HTMLElement]
      

Locates and returns a real array of DOM elements identified by selector. If no element was found, returns an empty array.

eql a, b

eql 4, 4 #=> true
      
      eql 4, "4" #=> false
      
      eql [1, 2, 3], [1, 2, 3] #=> true
      
      eql [1, 2, 3], [2, 3, 1] #=> false
      
      eql [1, 2, {a: 'b'}], [1, 2, {a: 'b'}] #=> true
      

Determines whether a and b are deep equal by strict comparison and returns either true or false.



get key, collection

get 2, ['a', 'b', 'c', 'd'] #=> 'c'
      
      get 'foo', {foo: 'bar', baz: 'quux'} #=> 'bar'
      
      get FOO, {FOO: 'bar'} #=> 'bar'
      

Retrieves an element identified by key from collection and returns the element.

head list

head [1, 2, 3] #=> 1
      
      head [1] #=> 1
      
      head [] #=> undefined
      

Returns the first item in a list or undefined if there are no items.

instanceof data, constructor

instanceof {}, Object #=> true
      
      instanceof 4, Object #=> false
      

Calls JavaScript's instanceof statement and returns the result, either true or false.

kill process

process = spawn fn => log "I'm alive!"
      
      kill process
      

Terminates the process. Returns undefined.

lang key, value

lang 'use.react', false #=> undefined
      

Sets thread-level configuration options for the language. For example, lang 'use.react', false will prevent trying to use React when compiling JSX-like syntax.

Note that these are runtime configuration options. They are not hoisted and may be used anywhere within your code. Each thread in your application will also have access to its own set of configuration options so setting an option within a child process will not affect code running in the host thread.

Returns undefined.

Current Config Options

use.react - {Boolean} Defaults to true. Determines whether C&S will try to use React.js when compiling JSX-like syntax.

This list is expected to be expanded.

last list

last [1, 2, 3] #=> 3
      
      last [1] #=> 1
      
      last [] #=> undefined
      

Returns the last item in a list or undefined if there are no items.

lead list

lead [1, 2, 3] #=> [1, 2]
      
      lead [1] #=> []
      
      lead [] #=> []
      

Returns a new array of all but the last item in list, or an empty array if there are no items or only 1 item.

log message

log 'This app is great!' #=> undefined
      

A shortcut for JavaScript's console.log(message). If the console object does not exists, fails silently.

random array

random [1, 2, 3] #=> 2
      
      random [1, 2, 3] #=> 1
      

Selects a random item from array and returns the item.

range from, through

range 1, 10
      #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      

Creates an array containing a range of numbers from from through through.

receive fun

receive match
        {{ OK, msg }}  => doSomethingWith msg
        {{ ERR, msg }} => throw (create Error, msg)
      

Registers a handler for dealing with messages that come in from a separate process. Returns undefined.

remove key, collection

remove 'foo', {foo: 'bar', baz: 'quux'}
      #=> {baz: 'quux'}
      
      remove 1, ['a', 'b', 'c']
      #=> ['a', 'c']
      

Creates a shallow clone of collection not including the item identified by key.

reply message

reply {{ OK, 'This is my message.' }}
      

Sends message from a child process to an owner process. Returns undefined.

send process, message

process = spawn fn =>
        receive fn msg =>
          console.log 'I got', msg
      
      send process, 'hello'
      
      #=> Logs: "I got hello"
      

Sends message to process. Returns undefined.

spawn fun

process = spawn fn => console.log "I'm alive!"
      

Creates a new operating system process out of fun.

tail list

tail [1, 2, 3] #=> [2, 3]
      
      tail [1] #=> []
      
      tail [] #=> []
      

list {Array|Tuple|String}: An list type.

Returns a new array of all but the first item in list, or an empty array if there are no items or only 1 item.

throw err

throw (create Error, 'This is an error message')
      

Throws an error.

tupleToArray tuple

tupleToArray {{ 1, 2, 3 }} #=> [1, 2, 3]
      

Creates a new array from items in a tuple.

tupleToObject tuple, fun

tupleToObject {{ fun1, fun2 }}, fn item =>
        item.name
      
      #=> {'fun1': fun1, 'fun2': fun2}
      

Creates an object from a tuple where fun is used to determine how to construct key names for each item in the tuple. If fun is not provided, indices will be used as object keys.

update key, value, collection

obj = {foo: 'bar', baz: 'quux'}
      
      update 'foo', 'Billy', obj
      #=> {foo: 'Billy', baz: 'quux'}
      

Creates a shallow clone of collection wherein the value for key has been updated to value.

warn message

warn 'Something was weird' #=> undefined
      

A shortcut for JavaScript's console.warn(message). Attempts to default to console.log if console.warn does not exist. If the console object does not exists, fails silently.